function Tid::getArgument

Same name in other branches
  1. 9 core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid::getArgument()
  2. 8.9.x core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid::getArgument()
  3. 10 core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid::getArgument()

Overrides ArgumentDefaultPluginBase::getArgument

File

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

Class

Tid
Taxonomy tid default argument.

Namespace

Drupal\taxonomy\Plugin\views\argument_default

Code

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));
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.