Jump to:

3459 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Menu with alternating pages if logged in or not?

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

Page: 1 2
Go to End
Author Topic: 4128 Views
  • banal
    Avatar
    Community Member
    901 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Ouch. That is indeed a rather inflexible design. I for one wouldn't care too much about the design when I'm admin, since for me it would be more important to actually see the pages when I'm logged in, so that I can verify the contents. But to each his own I guess

    Additionally, you could check the return value of:

    Director::get_site_mode();

    If in the CMS, you should get 'cms' as return value, 'site' otherwise.
    Example code:

    class AnonPage extends Page {
       public function canView($member = null) {
          
          if(Director::get_site_mode() == 'cms'){
             return parent::canView($member);
          }
          
          if(!$member || !(is_a($member, 'Member')) || is_numeric($member))
             $member = Member::currentUser();
             
          return $member ? false : true;
       }
       
       function can($type) {
          return $type == 'view' ? $this->canView() : parent::can($type);
       }
    }

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Okay maybe I'm way of here, but doesn't a combination <% if CurrentMember %> in the templates and using the Who Can View This Page in the Behavior section accomplish this? Thats how I'm controlling different menus for Logged in Users and General Public. But may this too basic for what you really need.

    Here is my Navigation.ss

    <ul class="grid_9 topnav">
       <% if CurrentMember %>
       <% control Menu(1) %>
       <% if Title != "Home" %>   
       <li>
          <a href="$Link">$MenuTitle.XML</a>
          <% if Children %>   
          <ul class="subnav">
             <% control Children %>
             <% if Children %>
             <li>
                <a href="$Link">$MenuTitle.XML</a>
                <ul class="subSubnav">
                <% control Children %>
                   <li><a href="$Link">$MenuTitle.XML</a></li>
                <% end_control %>
                </ul>                           
             </li>
             <% else %>
             <li><a href="$Link">$MenuTitle.XML</a></li>
             <% end_if %>
             
             <% end_control %>
          </ul>
          <% end_if %>
       </li>
       <% end_if %>   
       <% end_control %>
       <% else %>
       <% control Menu(1) %>
       <% if Title != "Home" %>   
       <li>
          <a href="$Link">$MenuTitle.XML</a>
          <% if Children %>   
          <ul class="subnav">
             <% control Children %>
             <% if Children %>
             <li>
                <a href="$Link">$MenuTitle.XML</a>
                <ul class="subSubnav">
                <% control Children %>
                   <li><a href="$Link">$MenuTitle.XML</a></li>
                <% end_control %>
                </ul>                           
             </li>
             <% else %>
             <li><a href="$Link">$MenuTitle.XML</a></li>
             <% end_if %>
             
             <% end_control %>
          </ul>
          <% end_if %>
       </li>
       <% end_if %>   
       <% end_control %>
       <li><a href="where-to-buy/">Where to Buy</a></li>
       <% end_if %>
    </ul>

    Sorry I don't indent my Controls and and If Statements if only to try and keep the output code cleaner.

    But logged in Users Get different Menu options Than the General Public.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Hello,

    I'm encountering a similar problem to what was discussed here - can anyone suggest a way of determining if someone is viewing the CMS or the front-end of a website? Director::get_site_mode() previously sounded perfect but it is deprecated so I'm not sure what I can do instead.

    I have tried to use canView to hide a certain page based on a set of conditions but I want it to always be editable in the CMS even if the logged in Admin doesn't meet those conditions. However - I would like those conditions to still apply to the Admin when viewing the front-end site and hide the page if they're not met.

    Really appreciate any help anyone can offer.

    Thanks

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