Using hook_form_views_exposed_form_alter to change the exposed filter from text field to select list (for node reference)

      // Start building out the options for our select list
    $options = [];
    $nodes = componentList(3);
    // Push titles into select list
    foreach ($nodes as $node) {
      $options[$node->id()] = $node->getTitle();
    // Start building out our new form element
    $field_name = 'field_binding_target_id';
    $form[$field_name]['#type'] = 'select';
    $form[$field_name]['#multiple'] = FALSE;
    // Specify the empty option for our select list
    $form[$field_name]['#empty_option'] = t('- Any -');
    // Add the $options from above to our select list
    $form[$field_name]['#options'] = $options;

All works very well on the first load of the page, but when the view is reloaded (after first filtering attemtp) the exposed form changes back to text field.

enter image description here

Is there any method of adding exposed filter for node reference as select list other then hook_form_views_exposed_form_alter that will make the it compatible with Ajax

PS. I need use Ajax in order to display the exposed filters in block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy