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


Go to End
Reply


30 Posts   6941 Views

Avatar
RS26

Community Member, 11 Posts

19 June 2009 at 2:48am

Edited: 20/06/2009 2:31am

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 [url=http://www.silverstripe.org/archive/show/26072]previous thread[/url])

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!

Avatar
martimiz

Forum Moderator, 1098 Posts

26 June 2009 at 7:57am

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

Avatar
RS26

Community Member, 11 Posts

26 June 2009 at 12:35pm

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

Avatar
martimiz

Forum Moderator, 1098 Posts

26 June 2009 at 7:11pm

Edited: 26/06/2009 7:15pm

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

Avatar
RS26

Community Member, 11 Posts

26 June 2009 at 11:26pm

Looks like that worked... I also saw the same solution being used on [url=http://www.ssbits.com/create-a-static-sidebar]ssbits.com[/url]

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

cheers!

Avatar
martimiz

Forum Moderator, 1098 Posts

27 June 2009 at 3:12am

If I understand correctly, you just can't... :-(

Avatar
RS26

Community Member, 11 Posts

27 June 2009 at 3:16am

Well that sucks.

Avatar
Sam

Administrator, 685 Posts

27 June 2009 at 11:20am

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;

Go to Top