3

Trying to change the label of a views exposed date filter based on Year granularity which is a select list. I have given date range to the filter thus it does not generate the "#options" array.

It creates a dropdown with year values and a "-Year" to display all values.

I want to rename this "-Year" label. Could not find any options within the view to change this label. So is there any hook or alter that can be used here ?

enter image description here

Yes i am using Better exposed filter and trying to use Date filter based on Content Post Date. After applying BEF i get a select list to the exposed date filter. I need to change the "-Year" label inside this dropdown. tried using hook_form_alter but not able to search this label and change it.

enter image description here

Attached a screenshot of the settings page for the Date filter. And under BEF settings I selected the Default select list option. Could not find any way to rename the "-Year" label.

4
  • It will be helpful if you can put a screen shot for your filter configuration, also make sure if there is configuration to do at better exposed filter level. I doubt why label can't be changed from the view itself, still if want to try it with the code hook_form_alter() should help you as exposed filter is also a form.
    – arpitr
    Aug 24, 2015 at 19:47
  • I have attached the screenshots and updated my question. Aug 25, 2015 at 6:43
  • How did you add your dropdown? Try to look in Date filter settings there should be settings for your dropdown if it Aug 25, 2015 at 8:01
  • Updated the question with settings page snapshot. Aug 25, 2015 at 9:30

2 Answers 2

1

Looks like this is something we have already on drupal issue-queue as https://www.drupal.org/node/2296607.

I was expecting the options to be alterable by any hook, unfortunately could not find any.

I explored the code in date module, and found the elements are processed by a function as date_parts_element, under which it appending '-' before the label.

Now, while the following will fix your issue but I would nor recommend it to you until it is approved by community. The hack is

In your date_api_elements.inc file(date/date_api) inside function date_parts_element add this line drupal_alter('date_process_element', $sub_element); in the bottom just before return $element; so it looks as

    ..... 
    if ($element['#date_label_position'] == 'within') {
      $sub_element['ampm']['#options'] = array('' => '-' . $label) + $sub_element['ampm']['#options'];
    }
  }
  drupal_alter('date_process_element', $sub_element);
  return $sub_element;
}

This will allow any custom module to alter the elements of date filter. Now in your custom module let's say named as my_module implement the hook we have added as following

/**
  * Implements hook_date_process_element_alter.
 */
function my_module_views_date_process_element_alter(&$element) {
  foreach($element['year']['#options'] as $key=>$value) {
    if($value == '-Year') {
        $element['year']['#options'][$key] = 'Year';// Or whatever you like to give.
    }
  }
}

I will be adding the same suggestion on drupal issue page.

Update

Indeed no patch required, date module provides the hooks out of the box, got to know from update mentioned issue feeds, didn't try the cpde myself but looks promising

function MYMODULE_date_select_process_alter(&$element, &$form_state, $context) {
  // Tweak the -Year filter to remove the -
  if (empty($element['year'])) return;
  foreach ($element['year']['#options'] as $key => $value) {
    if ($value == '-Year') {
      $element['year']['#options'][$key] = 'Select Year';
      break;
    }
  }
}
1
  • 1
    Thanks for your answer. Until it gets approved my the community wont use this hack and will prefer changing it by jQuery instead. Aug 26, 2015 at 8:55
0

You can use the following code in the template.php file

if ($form_id == "views_exposed_form"){
$form['your field_value']['#options']['All'] = t('your word');// overrides <All> on the dropdown }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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