Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Menu Children - Do they have access to methods in their page_controllers?

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

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

Page: 1
Go to End
Author Topic: 840 Views
  • Double-A-Ron
    Avatar
    Community Member
    603 Posts

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

    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

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Menu Children - Do they have access to methods in their page_controllers? Link to this post

    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 !

  • Double-A-Ron
    Avatar
    Community Member
    603 Posts

    Re: Menu Children - Do they have access to methods in their page_controllers? Link to this post

    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

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Menu Children - Do they have access to methods in their page_controllers? Link to this post

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

    Haha , ;-)

    840 Views
Page: 1
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.