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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Unexpected output after putting SiteTree into TreeMultiSelectField


Go to End


4 Posts   1681 Views

Avatar
LinseyM

Community Member, 99 Posts

25 October 2010 at 3:28am

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;
}

Avatar
Willr

Forum Moderator, 5523 Posts

25 October 2010 at 3:13pm

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.

Avatar
LinseyM

Community Member, 99 Posts

25 October 2010 at 11:52pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

26 October 2010 at 12:21pm

Edited: 26/10/2010 12:22pm

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!