Commit 68944146 authored by Sergii Bondarenko's avatar Sergii Bondarenko Committed by Sergii Bondarenko
Browse files

Issue #2710283 by BR0kEN: Adapting views filter

parent 46241dbb
Loading
Loading
Loading
Loading
+90 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Definition of shs_handler_filter_term_node_tid.
 */

namespace Drupal\cshs\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid;

/**
 * Filter by term id (including selection by client-side hierarchical select).
 * Filter by term id.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("cshs_node_tid")
 */
class CshsNodeTid extends TaxonomyIndexTid {

  const ID = 'cshs';

  /**
   * {@inheritdoc}
   */
class cshs_handler_filter_term_node_tid extends views_handler_filter_term_node_tid {
  public function adminSummary() {
    // Set up $this->value_options for the parent summary.
    $this->valueOptions = [];

    if ('All' === $this->value) {
      $this->value = NULL;
    }

  function option_definition() {
    $options = parent::option_definition();
    return parent::adminSummary();
  }

  /**
   * {@inheritdoc}
   */
  public function defineOptions() {
    $options = parent::defineOptions();

    $options['type'] = array('default' => 'cshs');
    $options['type'] = ['default' => static::ID];

    return $options;
  }

  function extra_options_form(&$form, &$form_state) {
    parent::extra_options_form($form, $form_state);
  /**
   * {@inheritdoc}
   */
  public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildExtraOptionsForm($form, $form_state);

    $form['type']['#options'] += array(
      'cshs' => t('Client-side hierarchical select'),
    );
    $form['type']['#options'] += [
      static::ID => t('Client-side hierarchical select'),
    ];
  }

  function value_form(&$form, &$form_state) {
    global $language;
  /**
   * {@inheritdoc}
   */
  public function valueForm(&$form, FormStateInterface $form_state) {
    parent::valueForm($form, $form_state);

    parent::value_form($form, $form_state);
    $vocabulary = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_vocabulary')
      ->load($this->options['vocabulary']);

    $multiple = $this->options['expose']['multiple'];
    $vocabulary = taxonomy_vocabulary_machine_name_load($this->options['vocabulary']);
    if (empty($vocabulary) && $this->options['limit']) {
      $form['markup'] = array(
      $form['markup'] = [
        '#markup' => '<div class="form-item">' . t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
      );
      ];

      return;
    }

    if ($this->options['type'] == 'cshs' && $this->options['exposed']) {
      $identifier = $this->options['expose']['identifier'];

      $form['value']['#type'] = 'cshs';
    if (static::ID === $this->options['type'] && $this->options['exposed']) {
      $form['value']['#type'] = $this->options['type'];
      //cshs_prepare_form_element($form['value']);

      // TODO disable the option in the views settings to allow multiple selections.

      // TODO test if default value works...

    }

    if (empty($form_state['exposed'])) {
      // Retain the helper option.
      $this->helper->options_form($form, $form_state);
    }
  }

  function admin_summary() {
    // Set up $this->value_options for the parent summary.
    $this->value_options = array();

    if ($this->value == 'All') {
      $this->value = NULL;
      $this->helper->buildOptionsForm($form, $form_state);
    }

    return parent::admin_summary();
  }

}