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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Show dataobjects in dropdown menu


Go to End


5 Posts   1089 Views

Avatar
Bereusei

Community Member, 96 Posts

20 December 2012 at 12:17am

Edited: 20/12/2012 12:22am

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?

Avatar
zenmonkey

Community Member, 545 Posts

20 December 2012 at 6:02am

Edited: 20/12/2012 6:02am

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 %>

Avatar
Bereusei

Community Member, 96 Posts

20 December 2012 at 8:05am

Edited: 20/12/2012 8:09am

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) %>?

Avatar
zenmonkey

Community Member, 545 Posts

20 December 2012 at 9:40am

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.

Avatar
Bereusei

Community Member, 96 Posts

20 December 2012 at 9:35pm

Works great, thanks. So simple...