7

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.

4

The "All" key contains an translatable object, so it works as before, you just need to do something like:

$form['field_your_field']['#options']['All'] = t('Text you want');
  • Where do I place this line? In theme-settings.php? – 0711master Nov 12 '18 at 0:50
4

Better Exposed Filters works for me well in my Drupal 8. The only thing what you have to do is follow this path.

Better Exposed Filters > Settings > More Options > Rewrite results.

Then you have to replace the any text using this format in Drupal 8. I think what you missed is this one. Replace using this token

- Any -|your replacement
  • Correct, but there is currently a patch for an issue causing an error if you add any replacements. – Duncanmoo Jul 20 '17 at 11:02
  • Unfortunately this is not possible for grouped date fields – 0711master Nov 12 '18 at 0:39
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

@user61857 It's pointed me in the right direction, thanks for your help. The merge filter didn't work for me, not sure what was going wrong so I used the simpler option.

      {% 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 %}

happy drupaling

  • This solution works for me best because Better Exposed Filters doesn't have a rewrite option for grouped date fields. – 0711master Nov 13 '18 at 16:11
0

When you click on Better exposed Filters: Settings, then click on "More Options For ...", there will be a dropdown called "Rewrite Filter Options". In there, add: - Any -|All

or change it to anything you need it to be. This works.

0

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 %}
0

Maybe you dont want to install the "Better exposed Filters" module, and maybe you don't want to code your template function.

In this case you can go with either:

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.