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

Choosing a URL from sitetree


Reply


30 Posts   6934 Views

Avatar
steve_nyhof

Community Member, 224 Posts

22 February 2010 at 1:15pm

Edited: 22/02/2010 1:16pm

I'm trying to do something similar, only I want the site tree names "URLSegment", not the ID. I want to select from the available pages.

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

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField("DivertURL", "Select the page..."));

Please help - Thank you,
Steve

Avatar
banal

Community Member, 901 Posts

22 February 2010 at 7:41pm

Don't know if I really understood what you're trying to do.. but this should get the URLSegment of the Page you selected:

$this->RelatedPage()->URLSegment;

or in template:

$RelatedPage.URLSegment

Avatar
steve_nyhof

Community Member, 224 Posts

23 February 2010 at 1:39am

I'm sure that will be part of it. Right now with the code above, I am getting the Security tree "Administrators".

I do not want the ID, but that I am sure where your code comes in. I fist need to get the sitetree for the pages, any further help and code details would be much appreciated.

Steve

Avatar
banal

Community Member, 901 Posts

23 February 2010 at 1:58am

Yes, you got to tell the TreeDropdownField what kind of Objects it should get. In your case the TreeDropdownField should be constructed like this:

new TreeDropdownField('RelatedPageID', 'Select the page...', 'SiteTree');

Per default this will create a TreeDropdownField with all SiteTree Objects. It will display the Title of the Page. If you'd rather have the URLSegment displayed, use the following:

new TreeDropdownField('RelatedPageID', 'Select the page...', 'SiteTree', 'ID', 'URLSegment');

For further information about the parameters read the code-documentation in sapphire/forms/TreeDropdownField.php

Avatar
steve_nyhof

Community Member, 224 Posts

24 February 2010 at 12:00pm

Edited: 24/02/2010 12:31pm

Hi Banel,

Thank you for your help. I do have it showing the correct list, and I changed the Title to MenuTitle. All working.

However, when the value is written, it is looking at the ID, and not the URLSegment.

What I am doing is setting a cookie using the URLSegment as it's name to make it unique, but it is also how the page is called from the cookie - by it's name. Therefore the ID opens up an error page.

I might just be missing something in the code and I will do some digging in the sapphire/forms/TreeDropdownField.php files.

Any further guidance will be appreciated.
Steve

EDIT: Maybe this is all working, but I need something to call the URLSegment in the template using the ID??

Avatar
steve_nyhof

Community Member, 224 Posts

24 February 2010 at 1:24pm

I got it all working! Thank you for your code - just needed to readjust.

I am not using the 'RelatedPageID' => 'Int', because I am not trying to form a connection as much as setting a name into the Page table.

I believe I am making a connection with "DivertURLTwo" and "URLSegment" between Page and Sitetree. Not sure if that is ok, but the URLSegment is unique I believe.

Then my code...

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

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField('DivertURLTwo', 'Select the page...', 'SiteTree', 'URLSegment', 'MenuTitle'));

This shows the MenuTitle in the dropdown once saved and puts the URLSegment into the cookie, and calls the right page upon returning to the site page.

I do understand that if the user changes the name of the page prior to a visitor returning that they will receive an error page.

So, again, thank you
Steve

Avatar
steve_nyhof

Community Member, 224 Posts

24 February 2010 at 2:18pm

Edited: 24/02/2010 3:25pm

Jumped the gun!

Here is my two code lines in Page.php...

public static $db = array(
'DivertURL' => 'HTMLText',
'DivertURLTwo' => 'HTMLText'

);

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField('DivertURL', 'Select Second Offer Page:', 'SiteTree', 'URLSegment', 'MenuTitle'));

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField('DivertURLTwo', 'Select Offer Ended Page:', 'SiteTree', 'URLSegment', 'MenuTitle'));

The DivertURLTwo is working great.

The DivertURL is saving the value (URLSegment) to the field fine, It is even showing up on the list when you select the dropdown menu - but the dropdown is blank - empty. Everything seems to be doing the job, just not showing me my choice next time. I even cleared the cookies and history - still not showing up. When I click on the down arrow, the value fills in. What's up with that?

Any ideas what I am missing?
Steve

Avatar
banal

Community Member, 901 Posts

24 February 2010 at 8:27pm

Hi Steve

You should change your Fields from HTMLText to Varchar. HTMLText is there to store huge text-data. Maybe it even adds some extra markup which could cause your problem...
If that's not the issue, it could be a limitation with the TreeDropdownField JavaScript. Maybe it tries to convert to numeric values or something.

If it still doesn't work after changing HTMLText to Varchar, I suggest you do the following:
- Setup a has_one relation for both of your pages, but keep the DivertURL fields.
- Use the relation ID for your dropdowns (as discussed in previous posts)
- implement a onBeforeWrite method, where you get the URLSegment of the pages and store it in the designated Varchar fields.