You are here

function Tid::get_argument in Views (for Drupal 7) 8.3

Return the default argument.

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

Overrides ArgumentDefaultPluginBase::get_argument

File

lib/Views/taxonomy/Plugin/views/argument_default/Tid.php, line 115
Definition of Views\taxonomy\Plugin\views\argument_default\Tid.

Class

Tid
Taxonomy tid default argument.

Namespace

Views\taxonomy\Plugin\views\argument_default

Code

function get_argument() {

  // Load default argument from taxonomy page.
  if (!empty($this->options['term_page'])) {
    if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
      return arg(2);
    }
  }

  // Load default argument from node.
  if (!empty($this->options['node'])) {
    foreach (range(1, 3) as $i) {
      $node = menu_get_object('node', $i);
      if (!empty($node)) {
        break;
      }
    }

    // Just check, if a node could be detected.
    if ($node) {
      $taxonomy = array();
      $fields = field_info_instances('node', $node->type);
      foreach ($fields as $name => $info) {
        $field_info = field_info_field($name);
        if ($field_info['type'] == 'taxonomy_term_reference') {
          $items = field_get_items('node', $node, $name);
          if (is_array($items)) {
            foreach ($items as $item) {
              $taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
            }
          }
        }
      }
      if (!empty($this->options['limit'])) {
        $tids = array();

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

  // If the current page is a view that takes tid as an argument,
  // find the tid argument and return it.
  $views_page = views_get_page_view();
  if ($views_page && isset($views_page->argument['tid'])) {
    return $views_page->argument['tid']->argument;
  }
}