0

Figured this question would be asked before, but don't see it.

I have a views exposed filter with a Text Field as one of the filters. I'd like to display this filter as a Select List instead of the standard text input or autocomplete.

Any advice on how to?

2 Answers 2

5

Okay, figured out a solid solution using hook_form_alter.

function MODULENAME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    
    if ($form['#id'] == 'EXPOSED-FORM-ID') {
    
        $nids = \Drupal::entityQuery('node')->condition('type','CONTENTTYPE')->execute();
        $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
        $options = ['' => 'All'];
    
        foreach($nodes as $nid => $node) {
          $value = $node->get('FIELD_NAME')->getString();
    
          if (isset($value)) {
            $options[$value] = $value;
          }
        }
    
        if (isset($form['FIELD_NAME_value'])) {
          $form['FIELD_NAME_value']['#type'] = 'select';
          $form['FIELD_NAME_value']['#options'] = $options;
          $form['FIELD_NAME_value']['#size'] = 1;
        }
      }
    }
    
}
3
  • Thanks Yuckle :)
    – Matt
    May 13, 2022 at 11:11
  • Consider using the more specific hook MODULENAME_form_views_exposed_form_alter() May 26, 2023 at 18:16
  • @Yuckle thanks but try to reference the original source of the code - drupal.org/paid-services/2012-04-26/…
    – Ojchris
    Apr 10 at 16:59
1

You can Implement using hook_form_alter

//Note: This will apply to all Views (thoes having same textfield in exposed filter).

function moduleName_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, &$form_id) {
  if ($form_id == 'views_exposed_form') {
    
    // Replace your TextField Name with 'field1'
    $form['field1'] = array(
      '#type' => 'select',  // Converting to select list
      '#options' => array(0 => t('<any>'), 20 => '*', 40 => '**', 60 => '***', 80 => '****', 100 => '*****'),  // your options/array
      '#default_value' => 0,
      '#title' => t('The average vote is more than'),
    );
  }
}

if you want to implement for specific view only then try following code.


function moduleName_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, &$form_id) {
  if ($form_id == 'views_exposed_form') {

    $view = $form_state->get('view');
//    \Drupal::logger('$view')->notice('<pre><code>' . print_r($view, TRUE) . '</code></pre>' );
    
    if ($view->id() == 'your_view_name') {

      // Replace your TextField Name with 'field1'
      $form['field1'] = array(
        '#type' => 'select', // Converting to select list
        '#options' => array(0 => t('<any>'), 20 => '*', 40 => '**', 60 => '***', 80 => '****', 100 => '*****'), // your options/array
        '#default_value' => 0,
        '#title' => t('The average vote is more than'),
      );
    }
  }
}

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.