Jump to:

22992 Posts in 11842 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Resetting related pages in TreeDropDown

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

Page: 1 2
Go to End
Author Topic: 2813 Views
  • LesC
    Avatar
    Community Member
    70 Posts

    Resetting related pages in TreeDropDown Link to this post

    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

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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?

  • Relic Viper
    Avatar
    Community Member
    1 Post

    Re: Resetting related pages in TreeDropDown Link to this post

    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. ...

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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.

  • Sylar2010
    Avatar
    Community Member
    3 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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...

  • edski
    Avatar
    Community Member
    12 Posts

    Re: Resetting related pages in TreeDropDown Link to this post

    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!

    2813 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.