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.

Archive /

Our old forums are still available as a read-only archive.

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

Menu Children - Do they have access to methods in their page_controllers?


Go to End


4 Posts   1160 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

11 September 2008 at 5:50pm

Hiya,

I've got an interesting one.

Our client is using an external IP library to determine WHERE a user is coming from.

Some of the content changes depending on what this location is. This is working fine on these three page types:

Page
RegionalPage (extends page, with one additional dropdown to select which region is allowed to see it)

The problem I am having is how to filter the navigation based on the chosen value in the dropdown filed on the RegionalPage type.

Here is the HTML for the menus:

<div id="main-nav">
    <ul id="nav">
      <% control Menu(1) %>
        <li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a>
        <% if Children %>
          <ul>
          <% control Children %>
          	<li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a></li>
          <% end_control %> 
          </ul>
        <% end_if %> 
        </li>
      <% end_control %>  
    </ul>
  </div>

What I need to be able to do is under the <% control Children %> control, I need to be able to check if that particular child is allowed to be displayed in the menu.

I have tried doing this a number of ways, but the most logical for me seems to be to add a new method to the page controllers for both page types like so:

function RegionMatch() {
        // Code to check ip location in a cookie and this page's current region setting
	return true; // DEBUG - always return true
}

And adjust my template to this:

<div id="main-nav">
    <ul id="nav">
      <% control Menu(1) %>
        <li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a>
        <% if Children %>
          <ul>
          <% control Children %>
          	<% if RegionMatch %>
          		<li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a></li>
            <% end_if %>
          <% end_control %> 
          </ul>
        <% end_if %> 
        </li>
      <% end_control %>  
    </ul>
  </div>

When I do this however, that function is never being called (as it is not returning true), none of the Children appear despite the default return value being true for now.

Any ideas on how this could be achieved? Either this way or another.

Cheers
Aaron

Avatar
Fuzz10

Community Member, 791 Posts

11 September 2008 at 7:28pm

I can think of 2 options :
1> Try putting RegionMatch in the (inherited) page class instead of in the page-controller

or

2> instead of using Children, write your own function to return the pages you need , based on the user region (By using for example Dataobject::get)

good luck !

Avatar
Double-A-Ron

Community Member, 607 Posts

12 September 2008 at 9:12am

Edited: 12/09/2008 9:19am

Champion Fuzz,

#1 was the key, although I had to put it in the Parent Page, not the Child RegionalPage. Obviously alot more logic involved in doing exactly what I wanted to do here, but here is the basic code if it helps anyone later:

Page.php

class Page extends SiteTree {

	function NavAllowed() {
			
			// Logic for deciding whether a link should be shown or not goes here

			if($condition_1 === $condition_2) {
				return true;
			} else {
				return false;
			}
		}
	}
   
       function getCMSFields() {
		$fields = parent::getCMSFields();
		return $fields;
	}
	
}

Page.ss

<div id="main-nav">
    <ul id="nav">
      <% control Menu(1) %>
        <li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a>
        <% if Children %>
          <ul>
          
          <% control Children %>
	     <% if NavAllowed%>
               <li><a href="$Link" title="Go to the &quot;{$Title}&quot; page" class="$LinkingMode"><span>$MenuTitle</span></a></li>
             <% end_if %>
          <% end_control %> 
          
          </ul>
        <% end_if %>  
        </li>
      <% end_control %>  
    </ul>
  </div>

From within the Parent Page class, I was able to do an if statement on $this->ClassName, and if that was the child page type I was after (RegionalPage), I would then do my logic. From here I had access to the field on that custom page type that set the allowed region. (Which surprised me, I thought this would all have to go in the Child page class).

All working fine. :-)

Cheers mate. Great start to my Friday!

Aaron

Avatar
Fuzz10

Community Member, 791 Posts

12 September 2008 at 7:18pm

You owe me a cup of coffee if I ever get around to visiting your beautiful country.

Haha , ;-)