Question

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:

  • B
    • Blog
  • C
    • CMS Workflow
    • Custom Translations
  • D
    • Database Plumber
    • ...

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

Was it helpful?

Solution

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 %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top