Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Unexpected output after putting SiteTree into TreeMultiSelectField

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

Page: 1
Go to End
Author Topic: 1102 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    Unexpected output after putting SiteTree into TreeMultiSelectField Link to this post

    Hi there,

    I've been trying to add a dropdown to my CMS that allows the user to create a link to a page on the site. Rather than expect them to "hardcode" (copy & paste in) the URL of their chosen page, I had loaded the siteTree into a TreeMultiSelectField.

    I've put the main extract of the code at the base of my post.

    It "works" in the CMS so far as that all the site pages load into the dropdown, you can select one and then save it.

    However, when I then call the item on the template ($ChooseURL1) the output I get back is as follows:

    <ul id="Menu1">
    <li onclick="location.href = this.getElementsByTagName('a')[0].href"><a href="">Budgeting and Planning</a></li>
    </ul>

    However, what I actually want is the URL to the chosen page. (Crossed fingers and tried "$ChooseURL1.URL" but it didn't work!)

    Hoping someone can help. Many thanks x

    on HomePage.php:
    ...
       static $db = array(
          'ServiceTitle1' => 'Text',
          'ServiceCopy1' => 'Text',
          'UseIcon1' => "Enum('Globe,Whiteboard,Other', '')"
       );
       
       static $has_one = array(
       );
       
       public static $many_many = array(
          'ChooseURL1' => 'SiteTree'
       );

    ...

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.ServiceBoxes', new TextField('ServiceTitle1'), '');
          $fields->addFieldToTab('Root.Content.ServiceBoxes', new TextField('ServiceCopy1'), '');
          $fields->addFieldToTab('Root.Content.ServiceBoxes', new TreeMultiSelectField('ChooseURL1','Choose page,'SiteTree'));
          $fields -> addFieldtoTab('Root.Content.ServiceBoxes', new DropdownField('UseIcon1', 'Select icon', $this->dbObject('UseIcon1')->enumValues()));
          
          return $fields;
       }

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Unexpected output after putting SiteTree into TreeMultiSelectField Link to this post

    Since ChooseURL1 is a set of sitetree objects (many many relational) you cannot simply print out $ChooseURL1 in the template. You should loop over the actual elements and print out what you want. For example:

    <% if ChooseURL1 %>
    <% control ChooseURL1 %>
    <a href="$Link">$Title</a>
    <% end_control %>
    <% end_if %>

    If you wish to only select 1 url (rather than multiple) then you will need to change the ChooseURL1 to a has_one relationship to sitetree and use a TreeDropdownField instead of a multiselect field.

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Unexpected output after putting SiteTree into TreeMultiSelectField Link to this post

    Hi Will,

    Thanks for the prompt reply - much appreciated.

    I got it working by implementing your suggestion:

    <% if ChooseURL1 %>
    <% control ChooseURL1 %>
    <a href="$Link">$Title</a>
    <% end_control %>
    <% end_if %>

    However as it would be better for the user to select only one page from the tree, I tried to change it to a has-one relationship and use TreeDropdownField. However I found that although it all implemented in the CMS, when I selected a field and did a Save & Publish on the page, the dropdown did not remember what was selected and went back to "choose..."

    Also, in the site, the conrtrol $Link, or $ChooseURL1.Link (I tried both) repeatedly returned "/" and Title function returned nothing.

    I even used a different name in case there was some confusion, eg ChoosePage1 as a has one. Still no change.

    Not sure what I was doing wrong, so I've stuck with multiselect and many-many array until I can work it out.

    Thanks again ;-)

    Linsey

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Unexpected output after putting SiteTree into TreeMultiSelectField Link to this post

    A trick with the has_one fields that has caught me out many a time is you need to refer to the FieldID rather than just the Field. For example in your case if you have a field called ChooseURL like

    static $has_one = array('ChooseURL' => 'SiteTree');

    Then you need to have a TreeDropdownField like below..

    new TreeDropdownField('ChooseURLID', .....);

    After changing that and running a dev/build you should be rocking!

    1102 Views
Page: 1
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.