Projet

Général

Profil

Paste
Télécharger (5,33 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_plugin_argument_default_taxonomy_tid.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_argument_default_taxonomy_tid.
6
 */
7

    
8
/**
9
 * Taxonomy tid default argument.
10
 */
11
class views_plugin_argument_default_taxonomy_tid extends views_plugin_argument_default {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function init(&$view, &$argument, $options) {
17
    parent::init($view, $argument, $options);
18

    
19
    // Convert legacy vids option to machine name vocabularies.
20
    if (!empty($this->options['vids'])) {
21
      $vocabularies = taxonomy_get_vocabularies();
22
      foreach ($this->options['vids'] as $vid) {
23
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
24
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
25
        }
26
      }
27
    }
28
  }
29

    
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function option_definition() {
34
    $options = parent::option_definition();
35

    
36
    $options['term_page'] = array('default' => TRUE, 'bool' => TRUE);
37
    $options['node'] = array('default' => FALSE, 'bool' => TRUE);
38
    $options['anyall'] = array('default' => ',');
39
    $options['limit'] = array('default' => FALSE, 'bool' => TRUE);
40
    $options['vocabularies'] = array('default' => array());
41

    
42
    return $options;
43
  }
44

    
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function options_form(&$form, &$form_state) {
49
    $form['term_page'] = array(
50
      '#type' => 'checkbox',
51
      '#title' => t('Load default filter from term page'),
52
      '#default_value' => $this->options['term_page'],
53
    );
54
    $form['node'] = array(
55
      '#type' => 'checkbox',
56
      '#title' => t('Load default filter from node page, that\'s good for related taxonomy blocks'),
57
      '#default_value' => $this->options['node'],
58
    );
59

    
60
    $form['limit'] = array(
61
      '#type' => 'checkbox',
62
      '#title' => t('Limit terms by vocabulary'),
63
      '#default_value'=> $this->options['limit'],
64
      '#process' => array('form_process_checkbox', 'ctools_dependent_process'),
65
      '#dependency' => array(
66
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
67
      ),
68
    );
69

    
70
    $options = array();
71
    $vocabularies = taxonomy_get_vocabularies();
72
    foreach ($vocabularies as $voc) {
73
      $options[$voc->machine_name] = check_plain($voc->name);
74
    }
75

    
76
    $form['vocabularies'] = array(
77
      '#prefix' => '<div><div id="edit-options-vids">',
78
      '#suffix' => '</div></div>',
79
      '#type' => 'checkboxes',
80
      '#title' => t('Vocabularies'),
81
      '#options' => $options,
82
      '#default_value' => $this->options['vocabularies'],
83
      '#process' => array('form_process_checkboxes', 'ctools_dependent_process'),
84
      '#dependency' => array(
85
        'edit-options-argument-default-taxonomy-tid-limit' => array(1),
86
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
87
      ),
88
    );
89

    
90
    $form['anyall'] = array(
91
      '#type' => 'radios',
92
      '#title' => t('Multiple-value handling'),
93
      '#default_value'=> $this->options['anyall'],
94
      '#process' => array('form_process_radios', 'ctools_dependent_process'),
95
      '#options' => array(
96
        ',' => t('Filter to items that share all terms'),
97
        '+' => t('Filter to items that share any term'),
98
      ),
99
      '#dependency' => array(
100
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
101
      ),
102
    );
103
  }
104

    
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function options_submit(&$form, &$form_state, &$options = array()) {
109
    // Filter unselected items so we don't unnecessarily store giant arrays.
110
    $options['vocabularies'] = array_filter($options['vocabularies']);
111
  }
112

    
113
  /**
114
   * {@inheritdoc}
115
   */
116
  public function get_argument() {
117
    // Load default argument from taxonomy page.
118
    if (!empty($this->options['term_page'])) {
119
      if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
120
        return arg(2);
121
      }
122
    }
123
    // Load default argument from node.
124
    if (!empty($this->options['node'])) {
125
      foreach (range(1, 3) as $i) {
126
        $node = menu_get_object('node', $i);
127
        if (!empty($node)) {
128
          break;
129
        }
130
      }
131
      // Just check, if a node could be detected.
132
      if ($node) {
133
        $taxonomy = array();
134
        $fields = field_info_instances('node', $node->type);
135
        foreach ($fields as $name => $info) {
136
          $field_info = field_info_field($name);
137
          if ($field_info['type'] == 'taxonomy_term_reference') {
138
            $items = field_get_items('node', $node, $name);
139
            if (is_array($items)) {
140
              foreach ($items as $item) {
141
                $taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
142
              }
143
            }
144
          }
145
        }
146
        if (!empty($this->options['limit'])) {
147
          $tids = array();
148
          // filter by vocabulary
149
          foreach ($taxonomy as $tid => $vocab) {
150
            if (!empty($this->options['vocabularies'][$vocab])) {
151
              $tids[] = $tid;
152
            }
153
          }
154
          return implode($this->options['anyall'], $tids);
155
        }
156
        // Return all tids.
157
        else {
158
          return implode($this->options['anyall'], array_keys($taxonomy));
159
        }
160
      }
161
    }
162

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

    
171
}