Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Show dataobjects in dropdown menu

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

Page: 1
Go to End
Author Topic: 467 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    Show dataobjects in dropdown menu Link to this post

    He guys,

    I have a dropdown menu and want to show some dataobject elements in the submenu in SS2.4, like this way:

    === Menu1 (Page) === Menu2 (Page) === Menu3 (Page)
    ------Submenu1 (menuelement extends DataObject)
    ------Submenu2 (menuelement extends DataObject)

    class Menu1Page extends Page{
    static $has_many = array( "Submenus" => "Submenu" );
    [Code for the DataObject Manager]
    }

    class Submenu extends DataObject{
    static $db = array(
    "Title" => 'Varchar(255)'
    );
    }

    html:
    <ul>
    <% control Menu(1) %>
    <li>
    <a href="$Link">Submenu</a>
    <% if Children %>
    <ul class="submenu">[ code for submenu ]</ul>
    <% end_if %>

    <% if Submenus %>
    <% control Submenus %>
    <ul class="submenu"><li>$Title</li></ul>
    <% end_control %>
    <% end_if %>
    </li>
    <% end_control %>
    </ul>

    I can´t figure out, how to get the DataObjects in that menu. I think you must set the dataobject in the menu-function, but I don´t know how.
    Can anyone give me a hint?

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Show dataobjects in dropdown menu Link to this post

    I assume this is all front end naviagtion, not into DropdownFIelds, but I'm not sure what you're trying to acheive? Are the the SubMenu items Pages or Anchors, right now its just a text field. Calling Children in the template will pull Child pages. If you want to pull the values of the Submenus, you should call

    <% if Submenus %>
    <ul class="submenu">
    <% control Submenus %>
    <li>$Title</li>
    <% end_control %>
    </ul>
    <% end_if %>

    But those items won't link to anything.
    If you want to link to child pages you should do

    <% if Children %>
    <ul class="submenu">
    <% control Children %>
    <li><a href="$Link">$Title</a></li>
    <% end_control %>
    </ul>
    <% end_if %>

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Show dataobjects in dropdown menu Link to this post

    Yes, it is all front end navigation.
    My sketch is a little bit misleading. The submenu items are anchors to an article or something (DataObject).

    Your first solution, was also my first idea, but it seems not to work.
    The <ul class="submenu"> does not appear, if the MenuPage has Submenus (extends DataObject).

    Are you sure, that <% control Submenus %> would work within <% control Menu(1) %>?

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: Show dataobjects in dropdown menu Link to this post

    Yeah You're right Menu returns a nested array and not a dataobject set. I forgot. I use Menu so rarely becuase I find my navigation is rarely built on the tree. You could create a function in the Page_Controller that returns all Pages that have a ParentID = 0 and use that build your menu.

    public function MyMenu() {
    $pages=DataObject::get("SiteTree", "ParentID = 0 AND ShowInMenus = 1");
    if($pages){
    return $pages;
    else {
    return false'
    }
    }

    That way since its return a DataObejctSet, you'll access to all methods on the Object

    Please note that's off the top of my head, can't remember id SIteTree will return draft pages or not.

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Show dataobjects in dropdown menu Link to this post

    Works great, thanks. So simple...

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