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.

Template Questions /

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

List Publication Years in Menu


Go to End


3 Posts   1653 Views

Avatar
timwjohn

Community Member, 98 Posts

14 June 2011 at 7:39am

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

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 7:33am

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

Avatar
timwjohn

Community Member, 98 Posts

12 October 2011 at 2:02am

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