You are here

class Tid in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid
  2. 9 core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid

Taxonomy tid default argument.

Plugin annotation


@ViewsArgumentDefault(
  id = "taxonomy_tid",
  title = @Translation("Taxonomy term ID from URL")
)

Hierarchy

  • class \Drupal\taxonomy\Plugin\views\argument_default\Tid extends \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase implements CacheableDependencyInterface

Expanded class hierarchy of Tid

File

core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php, line 23

Namespace

Drupal\taxonomy\Plugin\views\argument_default
View source
class Tid extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface
   */
  protected $vocabularyStorage;

  /**
   * Constructs a new Tid instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   *   The vocabulary storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, VocabularyStorageInterface $vocabulary_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->routeMatch = $route_match;
    $this->vocabularyStorage = $vocabulary_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('current_route_match'), $container
      ->get('entity_type.manager')
      ->getStorage('taxonomy_vocabulary'));
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['term_page'] = [
      'default' => TRUE,
    ];
    $options['node'] = [
      'default' => FALSE,
    ];
    $options['anyall'] = [
      'default' => ',',
    ];
    $options['limit'] = [
      'default' => FALSE,
    ];
    $options['vids'] = [
      'default' => [],
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['term_page'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Load default filter from term page'),
      '#default_value' => $this->options['term_page'],
    ];
    $form['node'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Load default filter from node page, that's good for related taxonomy blocks"),
      '#default_value' => $this->options['node'],
    ];
    $form['limit'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Limit terms by vocabulary'),
      '#default_value' => $this->options['limit'],
      '#states' => [
        'visible' => [
          ':input[name="options[argument_default][taxonomy_tid][node]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $options = [];
    $vocabularies = $this->vocabularyStorage
      ->loadMultiple();
    foreach ($vocabularies as $voc) {
      $options[$voc
        ->id()] = $voc
        ->label();
    }
    $form['vids'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Vocabularies'),
      '#options' => $options,
      '#default_value' => $this->options['vids'],
      '#states' => [
        'visible' => [
          ':input[name="options[argument_default][taxonomy_tid][limit]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="options[argument_default][taxonomy_tid][node]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['anyall'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Multiple-value handling'),
      '#default_value' => $this->options['anyall'],
      '#options' => [
        ',' => $this
          ->t('Filter to items that share all terms'),
        '+' => $this
          ->t('Filter to items that share any term'),
      ],
      '#states' => [
        'visible' => [
          ':input[name="options[argument_default][taxonomy_tid][node]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state, &$options = []) {

    // Filter unselected items so we don't unnecessarily store giant arrays.
    $options['vids'] = array_filter($options['vids']);
  }

  /**
   * {@inheritdoc}
   */
  public function getArgument() {

    // Load default argument from taxonomy page.
    if (!empty($this->options['term_page'])) {
      if (($taxonomy_term = $this->routeMatch
        ->getParameter('taxonomy_term')) && $taxonomy_term instanceof TermInterface) {
        return $taxonomy_term
          ->id();
      }
    }

    // Load default argument from node.
    if (!empty($this->options['node'])) {

      // Just check, if a node could be detected.
      if (($node = $this->routeMatch
        ->getParameter('node')) && $node instanceof NodeInterface) {
        $taxonomy = [];
        foreach ($node
          ->getFieldDefinitions() as $field) {
          if ($field
            ->getType() == 'entity_reference' && $field
            ->getSetting('target_type') == 'taxonomy_term') {
            $taxonomy_terms = $node->{$field
              ->getName()}
              ->referencedEntities();

            /** @var \Drupal\taxonomy\TermInterface $taxonomy_term */
            foreach ($taxonomy_terms as $taxonomy_term) {
              $taxonomy[$taxonomy_term
                ->id()] = $taxonomy_term
                ->bundle();
            }
          }
        }
        if (!empty($this->options['limit'])) {
          $tids = [];

          // filter by vocabulary
          foreach ($taxonomy as $tid => $vocab) {
            if (!empty($this->options['vids'][$vocab])) {
              $tids[] = $tid;
            }
          }
          return implode($this->options['anyall'], $tids);
        }
        else {
          return implode($this->options['anyall'], array_keys($taxonomy));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return [
      'url',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    foreach ($this->vocabularyStorage
      ->loadMultiple(array_keys($this->options['vids'])) as $vocabulary) {
      $dependencies[$vocabulary
        ->getConfigDependencyKey()][] = $vocabulary
        ->getConfigDependencyName();
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyInterface::getCacheTags public function The cache tags associated with this object. 14
Tid::$routeMatch protected property The route match.
Tid::$vocabularyStorage protected property The vocabulary storage.
Tid::buildOptionsForm public function
Tid::calculateDependencies public function
Tid::create public static function
Tid::defineOptions protected function
Tid::getArgument public function
Tid::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
Tid::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
Tid::submitOptionsForm public function
Tid::__construct public function Constructs a new Tid instance.