View source
<?php
namespace Drupal\shs\Plugin\views\filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid;
class ShsTaxonomyIndexTid extends TaxonomyIndexTid {
public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildExtraOptionsForm($form, $form_state);
$form['type']['#options']['shs'] = $this
->t('Simple hierarchical select');
}
protected function valueForm(&$form, FormStateInterface $form_state) {
$vocabulary = $this->vocabularyStorage
->load($this->options['vid']);
if (empty($vocabulary) && $this->options['limit']) {
$form['markup'] = [
'#markup' => '<div class="js-form-item form-item">' . $this
->t('An invalid vocabulary is selected. Please change it in the options.') . '</div>',
];
return;
}
parent::valueForm($form, $form_state);
if ('shs' !== $this->options['type'] || !$form_state
->get('exposed')) {
return;
}
$settings_additional = [
'required' => $this->options['expose']['required'],
'multiple' => $this->options['expose']['multiple'],
'anyLabel' => t('- Any -'),
'anyValue' => 'All',
'addNewLabel' => t('Add another item'),
];
$bundle = $vocabulary
->id();
$parents = [
[
[
'parent' => 0,
'defaultValue' => $settings_additional['anyValue'],
],
],
];
$identifier = $this->options['expose']['identifier'];
$default_value = (array) $this->value;
if (empty($default_value)) {
$exposed_input = isset($this->view
->getExposedInput()[$identifier]) ? $this->view
->getExposedInput()[$identifier] : [];
if ($exposed_input) {
$default_value = (array) $exposed_input;
}
}
$options = empty($form['value']['#options']) ? [] : $form['value']['#options'];
if (!empty($this->options['expose']['reduce'])) {
$options = $this
->reduceValueOptions($options);
if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
$default_value = [];
}
}
if (empty($this->options['expose']['multiple'])) {
if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
$default_value = 'All';
}
elseif (empty($default_value)) {
$keys = array_keys($options);
$default_value = array_shift($keys);
}
elseif ($default_value == [
'',
]) {
$default_value = 'All';
}
else {
$copy = $default_value;
$default_value = array_shift($copy);
}
}
if (!empty($default_value)) {
$widget_defaults = \Drupal::service('shs.widget_defaults');
$parents = $widget_defaults
->getParentDefaults($default_value, $settings_additional, 'taxonomy_term');
}
$settings_shs = [
'settings' => $settings_additional,
'bundle' => $bundle,
'baseUrl' => 'shs-term-data',
'cardinality' => $this->options['expose']['multiple'] ? -1 : 1,
'parents' => $parents,
'defaultValue' => $default_value,
];
$field_name = isset($this->definition['field_name']) ? $this->definition['field_name'] : $this->realField;
$hooks = [
'shs_js_settings',
"shs_{$field_name}_js_settings",
"shs_{$this->view->id()}__{$identifier}_js_settings",
"shs_{$this->view->id()}__{$field_name}_js_settings",
"shs_{$this->view->id()}__{$this->view->current_display}__{$identifier}_js_settings",
"shs_{$this->view->id()}__{$this->view->current_display}__{$field_name}_js_settings",
];
\Drupal::moduleHandler()
->alter($hooks, $settings_shs, $bundle, $field_name);
$context = [
'settings' => $settings_shs,
'object' => $this,
];
$form['value'] += [
'#shs' => $settings_shs,
];
if (empty($form['value']['#attributes'])) {
$form['value']['#attributes'] = [];
}
$form['value']['#attributes'] = array_merge($form['value']['#attributes'], [
'class' => [
'shs-enabled',
],
]);
if (empty($form['value']['#attached'])) {
$form['value']['#attached'] = [];
}
$form['value']['#attached'] = array_merge($form['value']['#attached'], [
'library' => [
'shs/shs.form',
],
]);
$element_key = Html::getUniqueId(sprintf('shs-%s', $field_name));
$form['value']['#attributes'] = array_merge($form['value']['#attributes'], [
'data-shs-selector' => $element_key,
]);
$form['value']['#shs'] += [
'classes' => shs_get_class_definitions($field_name, $context),
];
$form['value']['#attached'] = array_merge($form['value']['#attached'], [
'drupalSettings' => [
'shs' => [
$element_key => $form['value']['#shs'],
],
],
]);
}
}