1
votes

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.

1
Seems like you'd be better served by creating a custom getter on your member object, e.g. getGraduationYearRange, and just return that string to the template. <h3>$GraduationYearRange</h3> - UncleCheese
I updated my code to what I think is what you are referring to but it does not seem to work. I'm not sure why, though? - Dejsa Cocan
Try <% loop $GroupedMembers.GroupedBy(GraduationYearRange) %> in your template. - 3dgoo
It sort of works but it just shows the same year over and over for the start year. i.e. 2016-2016, 2016-2015, 2016-2014, etc - Dejsa Cocan

1 Answers

3
votes

I think I may have solved it! Here is what I changed regarding the getGraduationYearRange function:

public function getGraduationYearRange(){
   $gradYear = $this->GraduationYear;
   $startYear = $gradYear - 1;
   return $startYear." - ".$gradYear;
}

I also did as 3dgoo suggested and placed GraduationYearRange into the GroupedBy function. These changes seemed to have solved the issue as I see the correct year ranges now.

i.e 2015-2016, 2014-2015, 2013-2014, etc.

Thanks for all your help, though :)