You are here

views_dependent_filter.module in Views Dependent Filters 8

views_dependent_filters.module Provides a Views exposed filter which makes other filters depend on values in yet further filters for their visiblity and processing. For example: if the 'node type' filter is set to 'article', show a filter for a field that is only present on articles.

File

views_dependent_filter.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Unicode;

/**
 * @file views_dependent_filters.module
 * Provides a Views exposed filter which makes other filters depend on values
 * in yet further filters for their visiblity and processing.
 * For example: if the 'node type' filter is set to 'article', show a filter for
 * a field that is only present on articles.
 */

/**
 * After build form processor for the views exposed form.
 */
function views_dependent_filters_exposed_form_after_build(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  // We may have multiple dependency info arrays from more than one copies
  // of the views_dependent_filters_handler_filter_dependent handler.
  foreach ($form_state->dependent_exposed_filters as $dependency_info) {
    $form_dependency = array();
    foreach ($dependency_info['controllers'] as $filter_id => $controller_values) {
      $identifier = $dependency_info['identifiers'][$filter_id];

      // Regular form.
      foreach ($controller_values as $controller_value) {
        $part = ":input[name= {$filter_id}]";
        $form_dependency[$part]['value'] = $controller_value;

        // Converting in array for multivalue fields
        $form_dependency_array[] = $form_dependency;
      }
    }

    // Set the dependency on each form element as required.
    foreach ($dependency_info['dependents'] as $dependent_filter_id) {
      $identifier = $dependency_info['identifiers'][$dependent_filter_id];
      $form[$identifier]['#states']['visible'] = $form_dependency_array;
    }
  }
  return $form;
}

/**
 * Convert a string to an HTML id matching one made with drupal_html_id().
 *
 * We can't simply call drupal_html_id() because that only returns unique
 * ids; this is intended for when the ID already exists and we want to recreate
 * it from the original input.
 */
function views_dependent_filters_recreate_html_id($id) {
  $id = strtr(Unicode::strtolower($id), array(
    ' ' => '-',
    '_' => '-',
    '[' => '-',
    ']' => '',
  ));

  // As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
  // only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
  // colons (":"), and periods ("."). We strip out any character not in that
  // list. Note that the CSS spec doesn't allow colons or periods in identifiers
  // (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
  // characters as well.
  $id = preg_replace('/[^A-Za-z0-9\\-_]/', '', $id);

  // Removing multiple consecutive hyphens.
  $id = preg_replace('/\\-+/', '-', $id);
  return $id;
}

Functions

Namesort descending Description
views_dependent_filters_exposed_form_after_build After build form processor for the views exposed form.
views_dependent_filters_recreate_html_id Convert a string to an HTML id matching one made with drupal_html_id().