Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Change the text of Dropdown field in ModelAdmin Filter

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

Page: 1
Go to End
Author Topic: 281 Views
  • Vix
    Avatar
    Community Member
    25 Posts

    Change the text of Dropdown field in ModelAdmin Filter Link to this post

    Silverstripe 3.1.3

    Hoping this is a simple one but I have some dataobjects being managed through ModelAdmin. One of the fields is simply a day name field which is being stored as an Enum field with options 1 - 7.

    In the filter on the left hand side the dropdown shows the values 1 - 7, but I would like it to read 'Monday, Tuesday etc'. How do I change this?

    I only have the field storing it as a number as I want to be able to sort in the correct week order, so open to any better ways of doing this.

    public static $db = array (
             'Day' => 'Enum("1,2,3,4,5,6,7")',

    );

    public static $summary_fields = array(
             'Term.Title' => 'Term',
             'DayName' => 'Day',
             'Course.Title' => 'Class',
             'StartTime' => 'Start',
             'EndTime' => 'End',
             'Studio' => 'Studio'
       );   
    private static $searchable_fields = array(
    'Day',
    'Studio',
        'Instructor.Name' => 'PartialMatchFilter',
        'Term.Title' => 'PartialMatchFilter',
        'Course.Title' => 'PartialMatchFilter'
    );

    public function DayName(){
          $day_number = $this->Day;
          switch($day_number){
             case 1:
             $day = 'Monday';
             break;
             case 2:
             $day = 'Tuesday';
             break;
             case 3:
             $day = 'Wednesday';
             break;
             case 4:
             $day = 'Thursday';
             break;
             case 5:
             $day = 'Friday';
             break;
             case 6:
             $day = 'Saturday';
             break;
             case 7:
             $day = 'Sunday';
             break;
          }
          return $day;
       }

  • MJA
    Avatar
    Community Member
    15 Posts

    Re: Change the text of Dropdown field in ModelAdmin Filter Link to this post

    Hi Vix

    As it happens I had a very similar problem a few months back
    The trick here is very simple, if a little counter-intuitive

    First: do not specify Day in the $searchable_fields array, as this will build its dropdown list from the enum vaules

    Second: insert the Day dropdown your self in your ModelAdmin class
    You will need to overload getSearchContext() and getList()

    Refer to the model admin docs for more info (http://doc.silverstripe.org/framework/en/reference/modeladmin#results-customization)

    In getSearchContext() you insert the dropdown field into the search form -- this works exactly like adding a field in getCMSFields() or on a custom form
    You can then specify your own selection list
    Hint: DropdownField expects the selection list as an associative array -- e.g. array(1 => 'Monday', 2=>'Tuesday', 3 => 'Wednesday', etc)

    Then use getList() to amend the list of selected items for the display grid

    One point, the docs imply that in getList() you specify the selection like this:
    $list->filter('Day' => $params['Day']);
    This does not work (and it cost me a couple of hours worth of head scratching and searching the API docs to work out why)

    The filter method does not alter the the existing list, it returns a new filtered list, so use:
    $list = $list->filter('Day' => $params['Day']);

    Hope that helps
    Mike Armstrong
    (Silvertoad Ltd)

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