Views Exposed Filters in Drupal 8

Created on 18 September 2018, about 6 years ago
Updated 17 July 2023, over 1 year ago

After a day fighting with this and finally getting it working I'd like to share an example and a patch necessary to get this to work at a basic level.

First, create a view with an exposed filter of type taxonomy term (not with depth). Several of the below fields may not be necessary for you.

function YOUR_MODULE_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage('view');
  if ($form_id == 'views_exposed_form' && $view['view']->id()=='YOUR_VIEW_ID') {
        $form['tid'] = array(
            '#field_parents' => array(),
            '#type' => 'checkbox_tree',
            '#vocabularies' => array("YOUR_VOCABULARY" => taxonomy_vocabulary_load('YOUR_VOCABULARY')),
            '#max_choices' => 999,
            '#select_parents' => false,
            '#leaves_only' => false,
            '#value_key' => 'target_id',
            '#max_depth' => 0,
            '#start_minimized' => TRUE,
            '#element_validate' => array(array('Drupal\term_reference_tree\Plugin\Field\FieldWidget\TermReferenceTree', 'validateTermReferenceTreeElement')),
            '#after_build' => array(array('Drupal\term_reference_tree\Plugin\Field\FieldWidget\TermReferenceTree', 'afterBuild')),
            '#field_name' => 'tid', 
            '#parents' => array('tid'),
            '#tree' => true,
            '#input' => true,
            '#process' => array(array('Drupal\term_reference_tree\Element\CheckboxTree', 'processCheckboxTree')),
            '#pre_render' => array(array('Drupal\term_reference_tree\Element\CheckboxTree', 'preRenderCompositeFormElement')),
            '#attached' => array('library' => (array('term_reference_tree/term_reference_tree_js', 'term_reference_tree/term_reference_tree_css'))),
            '#theme' => 'checkbox_tree',
            '#array_parents' => array('tid', 'widget'),

            '#attributes' => array('data-drupal-selector' => 'edit-tid'),
            '#value' => array(1 => '1'),
            '#filter_view' => '',
            '#token_display' => ''
        );
  }

Then apply the patch below:

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Lann

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The patch and hook implementation works (it even works with the latest release). But I see some issues with theming on Drupal 10 and Olivero theme, see the screenshot.

    It can be fixed with the CSS like this:

    .form-type-boolean {
      display: inline-block;
    }
    
    ul.term-reference-tree-level {
      list-style-type: none;
    }
    
  • We applied this method to the filter of Search API view and have found the following issue. If the "Allow multiple selections" option of the filter is not selected then we got white screen and the following error message in logs:
    "Error: Cannot create references to/from string offsets in Drupal\Component\Utility\NestedArray::setValue() (line 155 of /home3/servicg6/public_html/d2/core/lib/Drupal/Component/Utility/NestedArray.php)."
    But if the "Allow multiple selections" option is checked then everything ok. More over if after rebuilding of the caches unset this option the filter continue to works without problem.

  • πŸ‡§πŸ‡ͺBelgium Belba

    Thanks to the people above for getting this working.

    Followed the instructions above and had a well-functioning term reference tree for months. Recently I updated my website to version 10.1 and suddenly the tree no longer works, the exposed filters disappear. Is there anyone experiencing the same problem? Are there changes to be made to the code?

Production build 0.71.5 2024