Jump to:

23576 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Choosing a URL from sitetree

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 3 4
Go to End
Author Topic: 6851 Views
  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Choosing a URL from sitetree Link to this post

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

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Choosing a URL from sitetree Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Choosing a URL from sitetree Link to this post

    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.

    6851 Views
Page: 1 2 3 4
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.