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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Referencing Page control variables from within a dataobject?


Go to End


6 Posts   3185 Views

Avatar
Erin

Community Member, 26 Posts

2 September 2010 at 4:41pm

Edited: 08/09/2010 6:19am

I'm trying to build a 2-level nav where the 2nd level link is comprised of the URL from the 1st level and an anchor identifier placed by a dataobject.
In short,

            <% control Menu(1) %>
               	<ul class="$URLSegment $LinkOrSection">
                <li><a href="$Link" class="$Title" title="$Title">$Title<!--[if IE 7]><!--></a><!--<![endif]-->
                       	<div class="link_sub">      
                       	       	<ul class="sub">
					<% if Meals %>
                               			<% control Meals %>
                                   			<% control ItemCategories %>
			                                        <li><a href="$Link#$Name" title="$Name">$Name</a></li>
                        			        <% end_control %>
						<% end_control %>

I can pull $Name from ItemCategories, but can't find a way to grab $Link from the Menu(1) controller. Do I need to write a custom method just to pull $Link from the page related to the dataobject?

Avatar
Erin

Community Member, 26 Posts

8 September 2010 at 6:19am

Changed the topic to one that I think might make more sense.

Avatar
Martijn

Community Member, 271 Posts

8 September 2010 at 8:08am

Try $Top.Link

Avatar
Erin

Community Member, 26 Posts

10 September 2010 at 2:46am

Unfortunately, this just gives every 2nd level link a reference to the current page, not the parent page in the menu.

<% control Menu(1) %>
   <% if LinkOrSection="link" %>
    	<ul class="$URLSegment $LinkOrSection">
                <li><a href="$Link" class="$Title" title="$Title">$Title<!--[if IE 7]><!--></a><!--<![endif]-->
                       <!--[if lte IE 6]><table><tr><td><![endif]-->				
                       	<div class="link_sub">      
                       	<ul class="sub">
			<% if Meals %>
                               <% control Meals %>
                                      <% control ItemCategories %>
                                             <li><a href="$Top.Link" title="$Name">$Name</a></li>
                                   <% end_control %>
			<% end_control %>

Avatar
Erin

Community Member, 26 Posts

17 September 2010 at 2:51pm

I think I'm getting closer. They key, near as I can see it, is to write a function for the ItemCategory class that will find the ID of the parent DataObject Meal (Menu), then find the parent Page (Menu), then - from that - get the URLSegment. This is as far as I've gotten:

	function MenuItemLink(){
		$MealsID = $this->MealsID;
		$Menu = DataObject::get('Menu', "MealsID = '{$MealsID}'");
        }

But $Menu is returned as a DataObjectSet. Shouldn't it just be 1 DataObject?

Avatar
TotalNet

Community Member, 181 Posts

17 September 2010 at 3:40pm

DataObject::get will always return a DataObjectSet even if there's just one DO in it.
Given your code you could use DataObject::get_by_id("Menu", $MealsID) or DataObject::get_one() if you wanted to add more to the query.
hth