4

I am using Drupal 7.21 and views 3.7. I have date module installed. In one of my views, I want to give drop-down year as an exposed filter and it is working fine. I do have choices as -Any- 2012 2013 But I want to remove -Any- and sort the items, so it will be like 2013 2012 I checked Required in exposed filter option without success and also used BEF module but there is not any option to do so. I also saw some blog and issue suggesting using hook__form_views_exposed_form_alter hook. I used that as http://eureka.ykyuen.info/2012/11/22/drupal-7-customize-the-exposed-filter-selection-list/ this blog suggested but in my form option there isn't even any options listed not as this blog said.

Can anyone help me to get the exposed filter I want to show? Thank you

6

You should implement hook_form_alter on your exposed filters form (if you have devel enabled then use dpm to find the form's name). Using the form alter sort (or just revert) the date's array and remove the '-any-'. By the way to remove the '-any-' you may also use CSS which will allow you to quickly bring it back in the future if you'll want to.

I edited a similar code I used in the past according to your need, so you will need something like this:

function mymodule_form_alter(&$form, &$form_state, $form_id) {

  switch ($form_id) {
    case 'views_exposed_form':
      if( !empty($form['#id']) && $form['#id'] == 'views-exposed-form-myview') {
        // This is the myview view

      // Remove the start date label
      if( !empty($form['field_sw_date_value']['min']['#date_label_position']) )
        $form['field_date_value']['min']['#date_label_position'] = 'none';

      // Remove the end date label
      if( !empty($form['field_sw_date_value']['max']['#date_label_position']) )
        $form['field_date_value']['max']['#date_label_position'] = 'none';
      break;
    }
}
  • Thank you @Rotem. I tried that but my problem is there is not any #options element. I am using date field from date module. My array for that field Array ( [#tree] => 1 [value] => Array ( [#title] => [#type] => date_select [#size] => 20 [#default_value] => 2013 [#date_format] => Y [#date_label_position] => within [#date_year_range] => -1:+0 [#process] => Array ( [0] => date_select_element_process ) [#prefix] => [#suffix] => ) [#element_validate] => Array ( [0] => date_views_select_validate ) ) – cissharp Aug 28 '13 at 13:38
  • Hi, I have updated the code on my original answer with a code I have just tested (works on my DEV). Reference Look at the Date API form elements, and in the relevant part: #date_label_position Handling option for date part labels, like 'Year', 'Month', and 'Day', can be 'above' the date part, 'within' it, or 'none', default is 'above' . The 'within' option shows the label as the first option in a select list or the default value for an empty textfield, taking up less screen space. (From the date_api_elements.inc file that comes with the Date module) – rreiss Aug 28 '13 at 18:44
  • I already tried that and it will just hide the -All- item but there will be blank item which works as -All-. I think I need to go with some css/js tweaks to remove -All- which I didn't want to. Thank you for your help. – cissharp Aug 28 '13 at 19:14
  • Why not using css for this? Have you tried this? I never tried it but I think it may help if CSS isn't an option – rreiss Aug 28 '13 at 19:19
  • Because css is client side rendering and not a good practice to hide element using css when there is the possibility to not list it at all from server side. Anyway using the code you provided (the latest one) and css can be good option. – cissharp Aug 28 '13 at 19:25
2

Although altering with hook_form_alter is correct if the exposed filter values are not created dynamicaly there is no need to alter the form.

  1. In exposed filter "Configure filter criterion:" check "Required".
  2. Select "one of" and highlight all the items you want user to select (if they are created dynamicaly this wil not work!)
  3. Check "Limit list to selected items".

But, in my opinion if you remove the -Any- value it is bad for th UX and you will need to add another default option as selected.

  • Thank you. But as it is date field I do not have option to select 'one of' as they are created dynamically, – cissharp Aug 28 '13 at 13:40
  • I have this option checked, and the list of items highlighted, and still I'm getting -Any-. – user1359 Oct 10 '13 at 16:36
  • 1
    Removing -Any- is not bad for UX if your use case requires there to be no -Any-. It depends on what you are trying to achieve. It might not be valid for users to select -Any-. I would actually say that having -Any- on a field that is required can be bad UX because it can be confusing on some cases. – rooby Apr 15 '14 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.