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: 6850 Views
  • RS26
    Avatar
    Community Member
    11 Posts

    Choosing a URL from sitetree Link to this post

    I am trying to setup a way to output a URL from a dropdown containing the locals site's pages (sitetree). The dropdown works and outputs an id, which is fine, but I cannot seem to select from the database with this ID correctly.

    I added the sitetree dropdown to a tab (works fine)

       $fields->addFieldToTab('Root.Content.Link', new TreeDropdownField("NewLink", "Page to link to", "SiteTree"));

    Since the sitetree dropdown returns a number, I used an Int field

       static $db = array(
          'NewLink' => 'Int'
       );

    I created a LinkFromID function in the controller class (As discussed in a previous thread)

    class HomePage_Controller extends Page_Controller {   
       function LinkFromID($id){
          $thisPage = DataObject::get_by_id("Page",$id);
          return $thisPage;
       }
    }

    When I pass a number to the LinkFromID function, a page shows... but when I attempt to use the NewLink variable, I get an error saying that the variable is not a number.

    Works:

       <% control LinkFromID(5) %>
          $Link
       <% end_control %>

    Don't work:

       <% control LinkFromID(NewLink) %>
          $Link
       <% end_control %>

    Am I passing the variable incorrectly? The variable can be output, and it shows a number. I even attempted to cast the variable as an Int and that doesn't work. Any help would be much appreciated!

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: Choosing a URL from sitetree Link to this post

    You don't have to provide the page with the NewLink ID from the template - it already knows it. So you could probably skip the param:

    function LinkFromID(){
    $thisPage = DataObject::get_by_id("Page", (int)$this->NewLink);
    return $thisPage;
    }

    then:

    <% control LinkFromID %>
    $Link
    <% end_control %>

    Didn't test it, but at least the ID should work...

  • RS26
    Avatar
    Community Member
    11 Posts

    Re: Choosing a URL from sitetree Link to this post

    But how does silverstripe know which variable I'm trying to change into an ID with passing something like this:

    <% control LinkFromID(NewLink) %>
    $Link
    <% end_control %>

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: Choosing a URL from sitetree Link to this post

    Ok, see if I got this correctly... You've build a Homepage type that has an extra db field called NewLink. In the CMS, when you create your homepage, you can select another Page to link to from a dropdown. This page's ID is then saved into the NewLink field of your Homepage.

    If this is what you're trying to do, then, in your PageController, the NewLink value can be accessed as $this->NewLink, and my suggestion should work - similar to the post you refer to. Have you tried yet? You probably wouldn't even need to use the control structure in your template, you could:

    <a href="$LinkFromID.Link">Page to link to</a>


    or

    <a href="$LinkFromID.Link">$LinkFromID.Title</a>

    Thinking... there's easy way as well, where you'd have your treedropdown return the urlsegment of the page to link to, instead of the ID. You'd have to define your NewLink field as a Varchar. You could then do something like <a href="$NewLink/">...</a> This would create a relative link based on the url segement only...

    [Edit - sorry, typo, replaced $NewLink by $LinkFromID

  • RS26
    Avatar
    Community Member
    11 Posts

    Re: Choosing a URL from sitetree Link to this post

    Looks like that worked... I also saw the same solution being used on ssbits.com

    I am still kinda confused on how to pass variable to the control functions, but at least this much is working for now.

    cheers!

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: Choosing a URL from sitetree Link to this post

    If I understand correctly, you just can't...

  • RS26
    Avatar
    Community Member
    11 Posts

    Re: Choosing a URL from sitetree Link to this post

    Well that sucks.

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Choosing a URL from sitetree Link to this post

    The better way to set up this feature is to make NewLink as $has_one relationship relationship rather than a $db field. Think about it this way: you're not really trying to store a number, you're trying to store a reference to another page in the SiteTree.

    $db = array(
    // your other fields can go here
    );
    $has_one = array(
    "RelatedPage" => "SiteTree";
    );

    This will do two things:

    * Set up a RelatedPageID field that you set.
    * Set up RelatedPage() method that returns the linked page.

    In my example above, I've called it "RelatedPage" rather than "NewLink", because you're not really referencing just the link of the page, you're referencing the entire page, including it's title, etc.

    That way you can put $RelatedPage.Link into your page, without needing to define a custom function. In fact, you can access all of the variables of the related page, so you could to this if you wanted:

    <% if RelatedPage %>
    <h2>Related</h2>
    <p><a href="$RelatedPage.Link">$RelatedPage.Title></p>
    <% end_if %>

    If you've already set up NewLink in your code, and you have content that you want to migrate, run these queries.

    UPDATE Page SET RelatedPageID = NewLink;
    UPDATE Page_Live SET RelatedPageID = NewLink;
    UPDATE Page_versions SET RelatedPageID = NewLink;

    6850 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.