Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » List Publication Years in Menu

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

Page: 1
Go to End
Author Topic: 756 Views
  • timwjohn
    Avatar
    Community Member
    98 Posts

    List Publication Years in Menu Link to this post

    Hi All,

    I have a news section in a site. What I would like to do in my template when viewing either the NewsList or individual News pages is replace my level 2 menu with a list of unique publication years ranging from the first article to the present.

    My idea was to override the Menu method in my NewsList controller and change the output if $level == 2.

    Could somebody help me out with the code that fetches the list of years and how I would make those links display all articles in a selected year when clicked.

    Much appreciated!

    Tim

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: List Publication Years in Menu Link to this post

    There is a GroupBy function which can be used, it helps is you sort your DataObject in the function which gets it otherwise you
    'll get it in ID order

    <% control GetNewsList.GroupedBy(Year) %>
    $Year
    <% control Children %>
    $Title
    <% end_control %>
    <% end_control %>

    This assumes GetNewsList() returns a list of News Items sorted by Date and that either you have a field called $Year or a getYear() function on your News Item DataObject. You really don't even need to override the Menu function just wrap the whole thing in a <% if Level(2) %>

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: List Publication Years in Menu Link to this post

    Managed to lose this thread and just find it again! Thanks @zenmonkey for letting me know about this very useful functionality.

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