You are here

trait CshsTaxonomyIndex in Client-side Hierarchical Select 8

Same name and namespace in other branches
  1. 8.3 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

Common implementation for "CshsTaxonomyIndex" plugin.

Hierarchy

File

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

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;
      $form += $this
        ->settingsForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function valueForm(&$form, FormStateInterface $form_state) : void {
    parent::valueForm($form, $form_state);
    if ($this->options['limit'] && $this
      ->getVocabulary() === NULL) {
      $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 getVocabulary() : ?VocabularyInterface {
    return $this->vocabularyStorage
      ->load($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',
      ],
    ];
  }

}

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 Collects the options.
CshsOptionsFromHelper::getTranslatedLevelLabels private function Returns translated labels with escaped markup.
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::getVocabulary public function Returns the taxonomy vocabulary to work with. Overrides CshsOptionsFromHelper::getVocabulary
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.