1

When I create a view (block) and create an exposed filter I have the choice of a Filter type to expose between

  • Single filter and
  • Grouped filters

enter image description here

If I choose the Grouped filters I am presented with an option to chose the Widget type

  • Radios or
  • Select

enter image description here

But if I chose the default Filter type the form lacks the Widget type option.

I cannot think of any reason why the regular exposed filters shouldn't have this widget type option to chose from.

Can anyone tell me ...

  • Is there a reason for this?
  • Is there any patch that I didn't find until now that fixes this/add's the feature?

FYI:

There is a hard coded switch in the Drupal core which automatically changes the widget type from checkboxes/radios to select based on the number of options regardless of the Widget type settings in the views form (1-4 options: radios, 5+: select). That issue is patched here: Grouped exposed filter with "Allow multiple selections" ignores widget setting

Edit

I don't want the alpha version of Better Exposed Filters module like suggested by some.

  • 3
    Have you installed Better Exposed Filters? – Clive Oct 1 at 11:05
  • 1
    @Clive No I didn't. And I won't as long it is an alpha version. – yunzen Oct 1 at 11:18
  • 3
    @yunzen I'd suggest using Better Exposed Filters just like the 43,662 other sites using it :) – Jdrupal Oct 4 at 18:55
  • What kind of field are you dealing with? Is it a list field, an entity reference field or some other kind of field? – Jigarius Oct 7 at 13:49
  • 1
    @Jigarius It's both list fields and entity reference (Taxonomy term) – yunzen Oct 8 at 6:45
0
+50

There are two questions asked in the OP:

  • What is the reason for not being able to choose the widget type for an exposed filter?

    Space: It seems that Views decide that for you, in the module's wisdom. I find this is a good enough argument, the rest can be handled as wished by a custom filter.

    // Exposed filter: use a select box to save space.

    Source: https://git.drupalcode.org/project/drupal/blob/8.8.x/core/modules/views/src/Plugin/views/filter/BooleanOperator.php#L156

  • Is there any patch that I didn't find until now that fixes this/add's the feature?

    None that I know off, but, this is not something that needs patching I believe; all additional wishes can be handled by a custom views filter plugin.

Good luck in any case!

  • I don't accept the decisions Drupal make's for you. If I can choose the widget type in one place, but can't choose in the other place, which happens to be just one step off. And then IMHO it's part of the philosophy of Drupal that you can do as much as possible through UI. So I don't understand. – yunzen Oct 11 at 7:17
2

I can see two possible solutions to this problem:

Solution 1: Better Exposed Filters

Install the Better Exposed Filters module. There are over 31,227 sites using it at the moment. Don't let the fact that it is in alpha hold you back unless your organization has a policy preventing you from using alpha releases.

Solution 2: Custom Code

If you cannot use Better Exposed Filters for some reason, then write a hook_form_FORM_ID_alter() to modify your views exposed form the way you want.

Sample code

Create a hook_form_FORM_ID_alter() in a custom module. In the example below, the module name is custom_general.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function custom_general_form_views_exposed_form_alter(
  &$form,
  FormStateInterface $form_state,
  $form_id
) {
  $view_data = $form_state->getStorage('view');

  // Content administration.
  if ($view_data['view']->id() === 'VIEW-ID') {
    // Analyze the structure of your form with kint().
    // Say, the field that matters to you is "field_foobar".
    // kint($form); exit;

    if (isset($form['field_foobar']) {
      $form['field_foobar']['#type'] = 'select';
      $form['field_foobar']['#multiple'] = FALSE;
      $form['field_foobar']['#size'] = 1;
      // TODO: Do other things as necessary.
    }
  }
}
  • How would that custom code look like? – leymannx Oct 6 at 16:52
  • 1
    I mean you know that OP won't use alpha, as they said in the comments under their question. Maybe they have fixed minimum stability in their composer.json, maybe they have company-wide guidelines, whatever. So just stating that they can use custom code maybe is not sufficient here. – leymannx Oct 6 at 18:31
  • Added sample code. – Jigarius Oct 10 at 13:49

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.