You are here

trait CshsTaxonomyIndex in Client-side Hierarchical Select 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/views/filter/CshsTaxonomyIndex.php \Drupal\cshs\Plugin\views\filter\CshsTaxonomyIndex
  2. 8.2 src/Plugin/views/filter/CshsTaxonomyIndex.php \Drupal\cshs\Plugin\views\filter\CshsTaxonomyIndex

The common implementation for the `CshsTaxonomyIndex` plugin.

Hierarchy

File

src/Plugin/views/filter/CshsTaxonomyIndex.php, line 17

Namespace

Drupal\cshs\Plugin\views\filter
View source
trait CshsTaxonomyIndex {
  use CshsOptionsFromHelper;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    \assert($instance instanceof self);
    $instance->entityRepository = $container
      ->get('entity.repository');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) : void {
    $options['value'] = isset($options['value']) ? (array) $options['value'] : [];
    parent::init($view, $display, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function defineOptions() : array {
    $options = parent::defineOptions();
    foreach (static::defaultSettings() + [
      'type' => CshsElement::ID,
    ] as $option => $value) {
      $options[$option] = [
        'default' => $value,
      ];
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) : void {
    parent::buildExtraOptionsForm($form, $form_state);
    $form['type']['#options'] += [
      CshsElement::ID => $this
        ->t('Client-side Hierarchical Select'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildExposeForm(&$form, FormStateInterface $form_state) : void {
    parent::buildExposeForm($form, $form_state);
    if (CshsElement::ID === $this->options['type']) {

      // Disable the "multiple" option in the exposed form settings.
      $form['expose']['multiple']['#access'] = FALSE;

      /* @noinspection AdditionOperationOnArraysInspection */
      $form += $this
        ->settingsForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function valueForm(&$form, FormStateInterface $form_state) : void {
    parent::valueForm($form, $form_state);
    if ($this->options['limit'] && empty($this
      ->getVocabularies())) {
      $form['markup'] = [
        '#type' => 'item',
        '#markup' => $this
          ->t('An invalid vocabulary is selected. Please change it in the options.'),
      ];
    }
    elseif (CshsElement::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() : array {
    return $this->options;
  }

  /**
   * {@inheritdoc}
   */
  public function getSetting($key) {
    return $this->options[$key] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  protected function getVocabularyStorage() : VocabularyStorageInterface {
    return $this->vocabularyStorage;
  }

  /**
   * {@inheritdoc}
   */
  protected function getTermStorage() : TermStorageInterface {
    return $this->termStorage;
  }

  /**
   * {@inheritdoc}
   */
  public function getVocabulariesIds() : array {
    return [
      $this->options['vid'],
    ];
  }

  /**
   * Returns the views filter configuration schema.
   *
   * @return array
   *   The config schema, provided in addition to the parent implementation.
   *
   * @see taxonomy.views.schema.yml
   * @see \cshs_config_schema_info_alter()
   */
  public static function getConfigSchema() : array {
    return [
      'save_lineage' => [
        'type' => 'boolean',
        'label' => 'Save lineage',
      ],
      'force_deepest' => [
        'type' => 'boolean',
        'label' => 'Force selection of deepest level',
      ],
      'parent' => [
        'type' => 'integer',
        'label' => 'Parent',
      ],
      'level_labels' => [
        'type' => 'string',
        'label' => 'Labels per hierarchy-level',
      ],
      'hierarchy_depth' => [
        'type' => 'integer',
        'label' => 'Hierarchy depth',
      ],
      'required_depth' => [
        'type' => 'integer',
        'label' => 'Required depth',
      ],
      'none_label' => [
        'type' => 'string',
        'label' => 'The "no selection" label',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CshsOptionsFromHelper::defaultSettings public static function Defines the default settings for this plugin.
CshsOptionsFromHelper::formElement public function Returns the form for a single widget.
CshsOptionsFromHelper::getOptions private function Returns the list of options for `cshs` element.
CshsOptionsFromHelper::getTranslatedLevelLabels private function Returns the translated labels with escaped markup.
CshsOptionsFromHelper::getTranslatedNoneLabel private function Returns the translated label for the "no selection" option.
CshsOptionsFromHelper::getTranslatedValue private function Returns the translated label.
CshsOptionsFromHelper::getVocabularies public function Returns the taxonomy vocabularies to work with.
CshsOptionsFromHelper::settingsForm public function Returns a form to configure settings.
CshsOptionsFromHelper::settingsSummary public function Returns a short summary for the settings.
CshsOptionsFromHelper::validateSettingsForm public function Validates the settings form.
CshsTaxonomyIndex::buildExposeForm public function
CshsTaxonomyIndex::buildExtraOptionsForm public function
CshsTaxonomyIndex::create public static function
CshsTaxonomyIndex::defineOptions public function
CshsTaxonomyIndex::getConfigSchema public static function Returns the views filter configuration schema.
CshsTaxonomyIndex::getSetting public function Returns the value of a setting, or its default value if absent. Overrides CshsOptionsFromHelper::getSetting
CshsTaxonomyIndex::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides CshsOptionsFromHelper::getSettings
CshsTaxonomyIndex::getTermStorage protected function Returns storage of the "taxonomy_term" entities. Overrides TaxonomyStorages::getTermStorage
CshsTaxonomyIndex::getVocabulariesIds public function Returns the list of taxonomy vocabularies IDs to work with. Overrides CshsOptionsFromHelper::getVocabulariesIds
CshsTaxonomyIndex::getVocabularyStorage protected function Returns storage of the "taxonomy_vocabulary" entities. Overrides TaxonomyStorages::getVocabularyStorage
CshsTaxonomyIndex::init public function
CshsTaxonomyIndex::valueForm public function
TaxonomyStorages::$entityRepository protected property An instance of the "entity.repository" service.
TaxonomyStorages::$entityTypeManager protected property An instance of the "entity_type.manager" service.
TaxonomyStorages::$needsTranslatedContent private property The state of whether content translation should be used.
TaxonomyStorages::getStorage private function Returns the entity storage.
TaxonomyStorages::getTranslationFromContext protected function Returns the entity translation to be used in the given context.
TaxonomyStorages::needsTranslatedContent protected function Returns the state of whether content translation needed.