I have a page on a SilverStripe site that lists out groups of graduates by year like so using the GroupedList feature:
Current (I don't display the year range for this group)
- Perry Buchanan
- Nichole Rice
- Noelle Phillips
- (etc)
2016
- Josiah Griffith
- Mathew Wolf
- Juliana Hunt
- (etc)
2015
- Jamal Graham
- Elias Leon
- (etc)
Here is the GroupedList function:
public function getGroupedMembers() {
$groupedList = GroupedList::create(
Member::get()
->filter("IsCurrent", False)
->sort("GraduationYear", "DESC")
);
return $groupedList;
}
And how it renders on the template:
<% loop $getGroupedMembers.GroupedBy(GraduationYear) %>
<div class="row">
<h3>$GraduationYearRange</h3>
<% loop $Children %>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3 members-list">
<p class="member-name">$Name</p><br/>
</div>
<% end_loop %>
</div>
<% end_loop %>
And so on. However, I want to show the start and graduation year, so I have 2015-2016, 2014-2015, 2013-2014 and so forth. I created a function called getGraduationYearRange which is located in my Member data object class:
<?php
class Member extends DataObject
{
private static $db = array(
'Name' => 'Varchar(250)',
'IsCurrent' => 'Boolean',
'GraduationYear' => 'varchar(4)',
'Bio' => 'HTMLText',
);
private static $has_one = array(
'MemberImage' => 'Image',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", new CheckboxField ("IsCurrent", "Current Member?"), "Bio");
$fields->addFieldToTab("Root.Main", new TextField("GraduationYear", "Graduation Year", '', 4), "Bio");
return $fields;
}
public function getGraduationYearRange(){
$startYear = date('Y', strtotime("-1 year", strtotime($this->GraduationYear)));
$gradYear = $this->GraduationYear;
echo $gradYear." - ".$startYear;
return $gradYear." - ".$startYear;
}
}
class MemberAdmin extends ModelAdmin {
private static $managed_models = 'Member';
private static $url_segment = 'Member';
private static $menu_title = 'Members';
}
The getGraduationYearRange function does not appear to work. It returns nothing in the template. The echo statement doesn't return anything either.
getGraduationYearRange, and just return that string to the template. <h3>$GraduationYearRange</h3> - UncleCheese<% loop $GroupedMembers.GroupedBy(GraduationYearRange) %>in your template. - 3dgoo