11

I have exposed filters in a view as select lists. How can I change the text for the default filter option of "- Any -"? In previous versions it was suggested to use a preprocess function like this:

if($form['#id'] == 'views-exposed-form') {
      foreach ($form['FIELDNAME']['#options'] as $key => &$option) {
        if ($key === 'All') {
          $option = 'All';
        }
      }
    }

But in Drupal 8, instead of a modifiable key/value, it is stuck inside a protected class.

protected string -> string(7) "- Any -"

How can I modify this?

Note: I also tried Better Exposed Filters, but it is unable to rewrite the "- Any -" option.

1

7 Answers 7

9

Better Exposed Filters works for me well in my Drupal 8. On Better Exposed Filters > Settings > More Options > Rewrite results, replace - Any - with the text you want, following this format: - Any -|your replacement.

2
  • Correct, but there is currently a patch for an issue causing an error if you add any replacements.
    – Duncanmoo
    Commented Jul 20, 2017 at 11:02
  • Unfortunately this is not possible for grouped date fields
    – 0711master
    Commented Nov 12, 2018 at 0:39
7

The All key contains an translatable object, so it works as before. You just need to use code similar to the following one.

$form['field_your_field']['#options']['All'] = t('Text you want');
2
  • 1
    Where do I place this line? In theme-settings.php?
    – 0711master
    Commented Nov 12, 2018 at 0:50
  • In the hook_form_alter() function in your .theme file or custom module Commented Feb 26, 2021 at 4:32
2

I figured out a way to do this at the theme level. You could change '- Any -' to a particular value or some other variable.

Copy select.html.twig to your theme. Modify the template to check if option.label equals '- Any -' and if so change it to whatever you'd like. In the code below it is set to the value for the filter's label.

{% elseif option.type == 'option' %}
  {# custom if statement #}
  {% if option.label == '- Any -' %} 
    {% set option = option|merge({ 'label': element['#title'] }) %}
  {%  endif %}
  <option value="{{ option.value }}"{{ option.selected ? ' selected="selected"' }}>{{ option.label }}</option>
{% endif %}
1

As an option, you can use select.html.twig in your custom theme with the following code.

{% spaceless %}
  {% if input_group %}
    <div class="input-group">
  {% endif %}

  {% if prefix %}
    {{ prefix }}
  {% endif %}

  {# Browsers do not recognize pseudo :after selectors, we must create a wrapper
   # around the select element to style it properly.
   # @see http://stackoverflow.com/q/21103542
   #}
  {% if not attributes.offsetExists('multiple') %}
  <div class="select-wrapper">
  {% endif %}
  {% set classes = ['form-control'] %}
  <select{{ attributes.addClass(classes) }}>
    {% for option in options %}
      {% if option.type == 'optgroup' %}
        <optgroup label="{{ option.label }}">
          {% for sub_option in option.options %}
            <option
              value="{{ sub_option.value }}"{{ sub_option.selected ? ' selected="selected"' }}>{{ sub_option.label }}</option>
          {% endfor %}
        </optgroup>
      {% elseif option.type == 'option' %}
        {# custom if statement #}
        {% if option.label == '- Any -' %}
          {% set option = option|merge({ 'label': '-All-' }) %}
        {% endif %}
        <option value="{{ option.value }}"{{ option.selected ? ' selected="selected"' }}>{{ option.label }}</option>
      {% endif %}
    {% endfor %}
  </select>
  {% if not attributes.offsetExists('multiple') %}
  </div>
  {% endif %}

  {% if suffix %}
    {{ suffix }}
  {% endif %}

  {% if input_group %}
    </div>
  {% endif %}
{% endspaceless %}
1

You can put it in your themename.theme file in your theme directory. This worked for me.

/**
 * Implements hook_form_alter().
 */
function nameofwebtheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'views_exposed_form' && $form['#id']=='views-exposed-form-what-s-on-page-1') {
    $form['field_date_range_value']['#options']['All'] = t('Text you want');
  }
}
1

The other answer pointed me in the right direction.
The merge filter didn't work for me. I don't know what I was doing wrong, so I used a simpler solution.

{% elseif option.type == 'option' %}
  {# custom if statement #}
  {% if option.label == '- Any -' %}
    <option value="{{ option.value }}"{{ option.selected ? ' selected="selected"' }}>- All -</option>
  {% else %}
    <option value="{{ option.value }}"{{ option.selected ? ' selected="selected"' }}>{{ option.label }}</option>
  {%  endif %}
{% endif %}
1
  • This solution works for me best because Better Exposed Filters doesn't have a rewrite option for grouped date fields.
    – 0711master
    Commented Nov 13, 2018 at 16:11
0

When you click on Better exposed Filters: Settings, you will see a Rewrite filter options drop-down under More options for....

Add - Any -|All or change it to anything you need. This works.

Your Answer

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

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