#684608 by defr, dereine: Provide a default argument plugin for taxonomy tids
[project/views.git] / modules / taxonomy / views_plugin_argument_default_taxonomy_tid.inc
1 <?php
2 /**
3 * @file
4 * Taxonomy tid default argument.
5 */
6
7 class views_plugin_argument_default_taxonomy_tid extends views_plugin_argument_default {
8 var $option_name = 'default_taxonomy_tid';
9 function option_definition() {
10 $options = parent::option_definition();
11
12 $options[$this->options_name . '_term_page'] = array('default' => TRUE);
13 $options[$this->options_name . '_node'] = array('default' => FALSE);
14 $options[$this->options_name . '_limit'] = array('default' => FALSE);
15 $options[$this->options_name . '_vids'] = array('default' => array());
16
17 return $options;
18 }
19
20 function argument_form(&$form, &$form_state) {
21 $form[$this->option_name . '_term_page'] = array(
22 '#type' => 'checkbox',
23 '#title' => t('Load default argument from term page'),
24 '#default_value' => $this->argument->options[$this->option_name . '_term_page'],
25 '#process' => array('views_process_dependency'),
26 '#dependency' => array(
27 'radio:options[default_action]' => array('default'),
28 'radio:options[default_argument_type]' => array($this->id)
29 ),
30 '#dependency_count' => 2,
31 );
32 $form[$this->option_name . '_node'] = array(
33 '#type' => 'checkbox',
34 '#title' => t('Load default argument from node page, thats good for related taxonomy blocks'),
35 '#default_value' => $this->argument->options[$this->option_name . '_node'],
36 '#process' => array('views_process_dependency'),
37 '#dependency' => array(
38 'radio:options[default_action]' => array('default'),
39 'radio:options[default_argument_type]' => array($this->id)
40 ),
41 '#dependency_count' => 2,
42 );
43
44 $form[$this->option_name . '_limit'] = array(
45 '#type' => 'checkbox',
46 '#title' => t('Limit terms by vocabulary'),
47 '#default_value'=> $this->argument->options[$this->option_name . '_limit'],
48 '#process' => array('views_process_dependency'),
49 '#dependency' => array(
50 'radio:options[default_action]' => array('default'),
51 'radio:options[default_argument_type]' => array($this->id),
52 'edit-options-default-taxonomy-tid-node' => array(1),
53 ),
54 '#dependency_count' => 3,
55 );
56
57 $options = array();
58 $vocabularies = taxonomy_get_vocabularies();
59 foreach ($vocabularies as $voc) {
60 $options[$voc->vid] = check_plain($voc->name);
61 }
62
63 $form[$this->option_name . '_vids'] = array(
64 '#prefix' => '<div><div id="edit-options-default-taxonomy-tid-vids-wrapper">',
65 '#suffix' => '</div></div>',
66 '#type' => 'checkboxes',
67 '#title' => t('Vocabularies'),
68 '#description' => t('If you wish to limit terms for specific vocabularies, check them; if none are checked, all terms will be included.'),
69 '#options' => $options,
70 '#default_value' => isset($this->argument->options[$this->option_name . '_vids']) ? $this->argument->options[$this->option_name . '_vids'] : array(),
71 '#process' => array('expand_checkboxes', 'views_process_dependency'),
72 '#dependency' => array(
73 'radio:options[default_action]' => array('default'),
74 'radio:options[default_argument_type]' => array($this->id),
75 'edit-options-default-taxonomy-tid-limit' => array(1),
76 'edit-options-default-taxonomy-tid-node' => array(1),
77 ),
78 '#dependency_count' => 4,
79 );
80 }
81
82 function get_argument() {
83 // Load default argument from taxonomy page.
84 if (!empty($this->argument->options[$this->option_name . '_term_page'])) {
85 if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
86 return arg(2);
87 }
88 }
89 // Load default argument from node.
90 if (!empty($this->argument->options[$this->option_name . '_node'])) {
91 foreach (range(1, 3) as $i) {
92 $node = menu_get_object('node', $i);
93 if (!empty($node)) {
94 break;
95 }
96 }
97 // Just check, if a node could be detected.
98 if ($node) {
99 if (!empty($this->argument->options[$this->option_name . '_limit'])) {
100 $tids = array();
101 // Filter by vid.
102 foreach ($node->taxonomy as $tid => $term) {
103 if (!empty($this->argument->options[$this->option_name . '_vids'][$term->vid])) {
104 $tids[] = $tid;
105 }
106 }
107 return implode(",", $tids);
108 }
109 // Return all tids.
110 else {
111 return implode(",", array_keys($node->taxonomy));
112 }
113 }
114 }
115
116 // If the current page is a view that takes tid as an argument,
117 // find the tid argument and return it.
118 $views_page = views_get_page_view();
119 if ($views_page && isset($views_page->view->argument['tid'])) {
120 return $views_page->view->argument['tid']->argument;
121 }
122 }
123 }
124