Reputation: 1950
Silverstripe: I want to have my menus to have its list sorted and grouped by alphabetically. I have followed every step in this documentation in using the GroupBy method her and to no avail.
Basically I want my subpages to appear as follows in the menu:
What is actually happening after following doing what is in the documentation is that i get an empty list, basically the $GroupedModules.GroupedBy(TitleFirstLetter) is not working.
The thing I think I may be missing here, is the creating of the Module class, I don't know where to create it, should it exist in the Page.php? Also, should be named Module?
Thanks in advance
Upvotes: 1
Views: 661
Reputation: 15794
The documentation you linked to shows how to do a group list of dataobjects (Module in their example). You want do a grouped list of pages, so you need to alter the code a little to fit your needs.
Page.php
class Page extends SiteTree {
// ...
public function getTitleFirstLetter() {
return $this->Title[0];
}
public function getGroupedChildren() {
return GroupedList::create($this->Children()->sort('Title'));
}
}
Your template
<% loop $GroupedChildren.GroupedBy(TitleFirstLetter) %>
<h3>$TitleFirstLetter</h3>
<ul>
<% loop $Children %>
<li>$Title</li>
<% end_loop %>
</ul>
<% end_loop %>
Upvotes: 3