Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Resetting related pages in TreeDropDown


Go to End
Reply


9 Posts   3129 Views

Avatar
LesC

Community Member, 70 Posts

27 March 2009 at 1:15am

Hi Folks,

I've set up a tab that allows my content editors to select up to 3 related pages for any page implemented with a TreeDropDownField like so:

public static $has_one = array(
'RelatedLink1' => 'SiteTree',
'RelatedLink2' => 'SiteTree',
'RelatedLink3' => 'SiteTree'
);

public function getCMSFields() {
$fields = parent::getCMSFields();

$treedropdownfield1 = new TreeDropdownField("RelatedLink1ID", "Choose a page that wish to link to:", "SiteTree");
$treedropdownfield2 = new TreeDropdownField("RelatedLink2ID", "Choose a page that wish to link to:", "SiteTree");
$treedropdownfield3 = new TreeDropdownField("RelatedLink3ID", "Choose a page that wish to link to:", "SiteTree");

$fields->addFieldToTab("Root.Content.RelatedPages", $treedropdownfield1);
$fields->addFieldToTab("Root.Content.RelatedPages", $treedropdownfield2);
$fields->addFieldToTab("Root.Content.RelatedPages", $treedropdownfield3);

return $fields;
}

But when they've selected a page, there's no way to reset it to 'no page'.

Does anyone know how to get round this?

Cheers

L

Avatar
LesC

Community Member, 70 Posts

31 March 2009 at 12:33am

Ok,

I've had a poke around in the class for the Treedropdown, and can't see how I would add an extra element to 'default' to nothing.

Can anyone suggest another way to implement a 'related pages' function without using the Treedropdown?

Or has anyone out there already worked out how to override the output of the function?

Cheers

Avatar
LesC

Community Member, 70 Posts

1 April 2009 at 11:35pm

Mmmm,

No answers eh? Is this a bug or a feature request?

Could one of the devs point me towards what I should do next to get someone to add this to the next revision of SS?

I'm guessing that more people than just me would find this useful!

L

Avatar
schellmax

Community Member, 126 Posts

2 April 2009 at 12:20am

interesting question - i've been dealing with this problem too in the past and didn't find a solution.
i'd also be interested in a way to restrict the dropdown using another root, so only descendants of a certain page are shown.
maybe someone else can help here?

Avatar
Relic Viper

Community Member, 1 Post

22 April 2010 at 2:08am

Edited: 22/04/2010 3:41am

A year later ...

and any answers?

#--- edit

Found it.

add the following in sapphire\core\model\Hierarchy.php
public function getChildrenAsUL

if ($rootCall) {
$output .= "<li id='selector-ParentID-0'><a rel='0'>none</a></li>";
}

between

$output = "<ul$attributes>\n";

and

foreach($children as $child) {
if(!$limitToMarked || $child->isMarked()) {
$foundAChild = true;

should find out soon if this is conflicting with anything else. ...

Avatar
schellmax

Community Member, 126 Posts

23 April 2010 at 8:33pm

thanks for this fix, should do the trick.
in case anyone's interested, as for my question in my last post, how to set another root for the dropdown, if found there's a setTreeBaseID method for treedropdownfield.

Avatar
Sylar2010

Community Member, 3 Posts

20 January 2011 at 5:36am

Very nice! However, if I am not mistaken, I left the <li id=''> to BLANK... Working better for me:

$output = "<ul$attributes>\n";
      if ($rootCall) {
      $output .= "<li id=''><a rel='0'>None</a></li>";
      }

since adding "selector-ParentID-0" to the ID makes it display "selector-ParentID-0" in the URL segment.

If anyone tries it, let us know how that works out for you...

Avatar
edski

Community Member, 12 Posts

2 April 2011 at 2:21am

Edited: 02/04/2011 2:23am

Hi all,

I'm not sure where I found this (struggling to re-find in Google), but someone's written a useful workaround.

1. Create a file "OptionalTreeDropdownField.php" in mysite/code
2. Replace TreeDropdownField with OptionalTreeDropdownField in your mysite / php file, e.g.

class Page extends SiteTree {

   public static $has_one = array(
      "SelectRelatedPage" => "SiteTree"      
   );

...

function getCMSFields() {
   $fields = parent::getCMSFields();
...

      $fields->addFieldToTab('Root.Content.RelatedPage', new OptionalTreeDropdownField("SelectRelatedPageID","Select related page","SiteTree"));      

...

return $fields;

Attachment didn't want to upload. Here's the code to put into OptionalTreeDropdownField.php :

<?php
/**
* TreeDropdown-like field that gives you a tree of items including an empty field, using ajax.
* Author: Marijn Kampf www.exadium.com
* Date: 24 Nov 2009
* Version: 2.0
* Revision date: 17 June 2010
* Changes: Updated to work with SilverStripe 2.4, tree function added.
*/
class OptionalTreeDropdownField extends TreeDropdownField {
private static $postTree = '</ul>';

/**
* Helper function to return the header (rather than defining same line twice).
*/
function preTree() {
return '<ul class="tree"><li id="" class="l"><a>' . _t('OptionalTreeDropdownField.NONE', "(None)", PR_MEDIUM, 'Non selected value of a dropdown') . '</a>';
}

/**
* Return the site tree
* For version 2.3 and earlier
*/
function gettree() {
echo $this->preTree();
parent::gettree();
echo $this->postTree;
}

/**
* Get the whole tree of a part of the tree via an AJAX request with empty / none item prepended.
*
* @param SS_HTTPRequest $request
* @return string
* for version 2.4 and later
*/
function tree($request) {
return $this->preTree() . parent::tree($request) . $this->postTree;
}
}

Hope that helps!

Go to Top