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.

Customising the CMS /

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

Change the text of Dropdown field in ModelAdmin Filter


Go to End


2 Posts   2783 Views

Avatar
Vix

Community Member, 60 Posts

12 March 2014 at 5:43pm

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;
	}

Avatar
MJA

Community Member, 21 Posts

14 March 2014 at 11:48pm

Edited: 15/03/2014 6:31am

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)