21489 Posts in 5783 Topics by 2622 members
| Go to End | Next > | |
| Author | Topic: | 4807 Views |
-
Re: Choosing a URL from sitetree

22 February 2010 at 1:15pm Last edited: 22 February 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 -
Re: Choosing a URL from sitetree

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
-
Re: Choosing a URL from sitetree

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
-
Re: Choosing a URL from sitetree

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
-
Re: Choosing a URL from sitetree

24 February 2010 at 12:00pm Last edited: 24 February 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.
SteveEDIT: Maybe this is all working, but I need something to call the URLSegment in the template using the ID??
-
Re: Choosing a URL from sitetree

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 -
Re: Choosing a URL from sitetree

24 February 2010 at 2:18pm Last edited: 24 February 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 -
Re: Choosing a URL from sitetree

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.
| 4807 Views | ||
| Go to Top | Next > |


