diff --git a/cshs.views.inc b/cshs.views.inc index f7600b0..e7f08ec 100644 --- a/cshs.views.inc +++ b/cshs.views.inc @@ -6,12 +6,12 @@ */ use Drupal\cshs\Plugin\views\filter\CshsTaxonomyIndexTid; +use Drupal\cshs\Plugin\views\filter\CshsTaxonomyIndexTidDepth; /** * Implements hook_views_plugins_filter_alter(). */ function cshs_views_plugins_filter_alter(array &$plugins) { - foreach (['taxonomy_index_tid', 'taxonomy_index_tid_depth'] as $plugin) { - $plugins[$plugin]['class'] = CshsTaxonomyIndexTid::class; - } + $plugins['taxonomy_index_tid']['class'] = CshsTaxonomyIndexTid::class; + $plugins['taxonomy_index_tid_depth']['class'] = CshsTaxonomyIndexTidDepth::class; } diff --git a/src/Plugin/views/filter/CshsTaxonomyIndexTidDepth.php b/src/Plugin/views/filter/CshsTaxonomyIndexTidDepth.php new file mode 100644 index 0000000..bf1bdf8 --- /dev/null +++ b/src/Plugin/views/filter/CshsTaxonomyIndexTidDepth.php @@ -0,0 +1,166 @@ +getStorage('taxonomy_vocabulary'), + $entity_type_manager->getStorage('taxonomy_term') + ); + + $this->entityRepository = $entity_repository; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('entity.repository') + ); + } + + /** + * {@inheritdoc} + */ + public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { + $options['value'] = isset($options['value']) ? (array) $options['value'] : []; + + parent::init($view, $display, $options); + } + + /** + * {@inheritdoc} + */ + public function defineOptions() { + $options = parent::defineOptions(); + + foreach (static::defaultSettings() + ['type' => static::ID] as $option => $value) { + $options[$option] = ['default' => $value]; + } + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildExtraOptionsForm($form, $form_state); + + $form['type']['#options'] += [ + static::ID => $this->t('Client-side hierarchical select'), + ]; + } + + /** + * {@inheritdoc} + */ + public function buildExposeForm(&$form, FormStateInterface $form_state) { + parent::buildExposeForm($form, $form_state); + + if (static::ID === $this->options['type']) { + // Disable the "multiple" option in the exposed form settings. + $form['expose']['multiple']['#access'] = FALSE; + $form += $this->settingsForm($form, $form_state); + } + } + + /** + * {@inheritdoc} + */ + public function valueForm(&$form, FormStateInterface $form_state) { + parent::valueForm($form, $form_state); + + if (empty($this->getVocabulary()) && $this->options['limit']) { + $form['markup'] = [ + '#type' => 'item', + '#markup' => $this->t('An invalid vocabulary is selected. Please change it in the options.'), + ]; + } + elseif (static::ID === $this->options['type']) { + $form['value'] = array_merge($form['value'], $this->formElement(), [ + '#multiple' => FALSE, + '#default_value' => (array) $form['value']['#default_value'], + ]); + } + } + + /** + * {@inheritdoc} + */ + public function getSettings() { + return $this->options; + } + + /** + * {@inheritdoc} + */ + public function getSetting($key) { + return isset($this->options[$key]) ? $this->options[$key] : NULL; + } + + /** + * {@inheritdoc} + */ + protected function getVocabularyStorage() { + return $this->vocabularyStorage; + } + + /** + * {@inheritdoc} + */ + protected function getTermStorage() { + return $this->termStorage; + } + + /** + * {@inheritdoc} + */ + public function getVocabulary() { + return $this->vocabularyStorage->load($this->options['vid']); + } + +}