You are here

public function Tid::getArgument in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php \Drupal\taxonomy\Plugin\views\argument_default\Tid::getArgument()

Return the default argument.

This needs to be overridden by every default argument handler to properly do what is needed.

Overrides ArgumentDefaultPluginBase::getArgument

File

core/modules/taxonomy/src/Plugin/views/argument_default/Tid.php, line 185
Contains \Drupal\taxonomy\Plugin\views\argument_default\Tid.

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 = array();
      foreach ($node
        ->getFieldDefinitions() as $field) {
        if ($field
          ->getType() == 'entity_reference' && $field
          ->getSetting('target_type') == 'taxonomy_term') {
          foreach ($node
            ->get($field
            ->getName()) as $item) {
            if (($handler_settings = $field
              ->getSetting('handler_settings')) && isset($handler_settings['target_bundles'])) {
              $taxonomy[$item->target_id] = reset($handler_settings['target_bundles']);
            }
          }
        }
      }
      if (!empty($this->options['limit'])) {
        $tids = array();

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