You are here

function views_arg_parent_term_plugin_argument_default_parent_taxonomy_tid::get_argument in Views arg parent term 7

Method to get/set views' default arguments.

Overrides views_plugin_argument_default::get_argument

File

includes/views_plugin_argument_default_parent_taxonomy_tid.inc, line 154
Definition of views_arg_parent_term_plugin_argument_default_parent_taxonomy_tid.

Class

views_arg_parent_term_plugin_argument_default_parent_taxonomy_tid
Parent Taxonomy tid default argument.

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))) {
      $parents = !empty($this->options['root_term_page']) ? taxonomy_get_parents_all(arg(2)) : taxonomy_get_parents(arg(2));
      $taxonomy = array();
      foreach ($parents as $parent) {
        $taxonomy[] = $parent->tid;
      }
      return implode($this->options['term_page_anyall'], $taxonomy);
    }
  }

  // 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) {
              $parents = !empty($this->options['root_parent']) ? taxonomy_get_parents_all($item['tid']) : taxonomy_get_parents($item['tid']);
              foreach ($parents as $parent) {
                $taxonomy[$parent->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;
  }
}