Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_plugin_argument_default_taxonomy_tid.inc @ 6eb8d15f

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
  function init(&$view, &$argument, $options) {
13
    parent::init($view, $argument, $options);
14

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

    
26
  function option_definition() {
27
    $options = parent::option_definition();
28

    
29
    $options['term_page'] = array('default' => TRUE, 'bool' => TRUE);
30
    $options['node'] = array('default' => FALSE, 'bool' => TRUE);
31
    $options['anyall'] = array('default' => ',');
32
    $options['limit'] = array('default' => FALSE, 'bool' => TRUE);
33
    $options['vocabularies'] = array('default' => array());
34

    
35
    return $options;
36
  }
37

    
38
  function options_form(&$form, &$form_state) {
39
    $form['term_page'] = array(
40
      '#type' => 'checkbox',
41
      '#title' => t('Load default filter from term page'),
42
      '#default_value' => $this->options['term_page'],
43
    );
44
    $form['node'] = array(
45
      '#type' => 'checkbox',
46
      '#title' => t('Load default filter from node page, that\'s good for related taxonomy blocks'),
47
      '#default_value' => $this->options['node'],
48
    );
49

    
50
    $form['limit'] = array(
51
      '#type' => 'checkbox',
52
      '#title' => t('Limit terms by vocabulary'),
53
      '#default_value'=> $this->options['limit'],
54
      '#process' => array('form_process_checkbox', 'ctools_dependent_process'),
55
      '#dependency' => array(
56
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
57
      ),
58
    );
59

    
60
    $options = array();
61
    $vocabularies = taxonomy_get_vocabularies();
62
    foreach ($vocabularies as $voc) {
63
      $options[$voc->machine_name] = check_plain($voc->name);
64
    }
65

    
66
    $form['vocabularies'] = array(
67
      '#prefix' => '<div><div id="edit-options-vids">',
68
      '#suffix' => '</div></div>',
69
      '#type' => 'checkboxes',
70
      '#title' => t('Vocabularies'),
71
      '#options' => $options,
72
      '#default_value' => $this->options['vocabularies'],
73
      '#process' => array('form_process_checkboxes', 'ctools_dependent_process'),
74
      '#dependency' => array(
75
        'edit-options-argument-default-taxonomy-tid-limit' => array(1),
76
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
77
      ),
78
    );
79

    
80
    $form['anyall'] = array(
81
      '#type' => 'radios',
82
      '#title' => t('Multiple-value handling'),
83
      '#default_value'=> $this->options['anyall'],
84
      '#process' => array('form_process_radios', 'ctools_dependent_process'),
85
      '#options' => array(
86
        ',' => t('Filter to items that share all terms'),
87
        '+' => t('Filter to items that share any term'),
88
      ),
89
      '#dependency' => array(
90
        'edit-options-argument-default-taxonomy-tid-node' => array(1),
91
      ),
92
    );
93
  }
94

    
95
  function options_submit(&$form, &$form_state, &$options = array()) {
96
    // Filter unselected items so we don't unnecessarily store giant arrays.
97
    $options['vocabularies'] = array_filter($options['vocabularies']);
98
  }
99

    
100
  function get_argument() {
101
    // Load default argument from taxonomy page.
102
    if (!empty($this->options['term_page'])) {
103
      if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
104
        return arg(2);
105
      }
106
    }
107
    // Load default argument from node.
108
    if (!empty($this->options['node'])) {
109
      foreach (range(1, 3) as $i) {
110
        $node = menu_get_object('node', $i);
111
        if (!empty($node)) {
112
          break;
113
        }
114
      }
115
      // Just check, if a node could be detected.
116
      if ($node) {
117
        $taxonomy = array();
118
        $fields = field_info_instances('node', $node->type);
119
        foreach ($fields as $name => $info) {
120
          $field_info = field_info_field($name);
121
          if ($field_info['type'] == 'taxonomy_term_reference') {
122
            $items = field_get_items('node', $node, $name);
123
            if (is_array($items)) {
124
              foreach ($items as $item) {
125
                $taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
126
              }
127
            }
128
          }
129
        }
130
        if (!empty($this->options['limit'])) {
131
          $tids = array();
132
          // filter by vocabulary
133
          foreach ($taxonomy as $tid => $vocab) {
134
            if (!empty($this->options['vocabularies'][$vocab])) {
135
              $tids[] = $tid;
136
            }
137
          }
138
          return implode($this->options['anyall'], $tids);
139
        }
140
        // Return all tids.
141
        else {
142
          return implode($this->options['anyall'], array_keys($taxonomy));
143
        }
144
      }
145
    }
146

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