Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Field handler to provide simple renderer that allows linking to a taxonomy
10
 * term.
11
 *
12
 * @ingroup views_field_handlers
13
 */
14
class views_handler_field_taxonomy extends views_handler_field {
15

    
16
  /**
17
   * Constructor to provide additional field to add.
18
   *
19
   * This constructer assumes the taxonomy_term_data table. If using another
20
   * table, we'll need to be more specific.
21
   */
22
  public function construct() {
23
    parent::construct();
24
    $this->additional_fields['vid'] = 'vid';
25
    $this->additional_fields['tid'] = 'tid';
26
    $this->additional_fields['name'] = 'name';
27
    $this->additional_fields['vocabulary_machine_name'] = array(
28
      'table' => 'taxonomy_vocabulary',
29
      'field' => 'machine_name',
30
    );
31
  }
32

    
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function option_definition() {
37
    $options = parent::option_definition();
38
    $options['link_to_taxonomy'] = array('default' => FALSE, 'bool' => TRUE);
39
    $options['convert_spaces'] = array('default' => FALSE, 'bool' => TRUE);
40
    return $options;
41
  }
42

    
43
  /**
44
   * Provide link to taxonomy option.
45
   */
46
  public function options_form(&$form, &$form_state) {
47
    $form['link_to_taxonomy'] = array(
48
      '#title' => t('Link this field to its taxonomy term page'),
49
      '#description' => t("Enable to override this field's links."),
50
      '#type' => 'checkbox',
51
      '#default_value' => !empty($this->options['link_to_taxonomy']),
52
    );
53
    $form['convert_spaces'] = array(
54
      '#title' => t('Convert spaces in term names to hyphens'),
55
      '#description' => t('This allows links to work with Views taxonomy term arguments.'),
56
      '#type' => 'checkbox',
57
      '#default_value' => !empty($this->options['convert_spaces']),
58
    );
59
    parent::options_form($form, $form_state);
60
  }
61

    
62
  /**
63
   * Render whatever the data is as a link to the taxonomy.
64
   *
65
   * Data should be made XSS safe prior to calling this function.
66
   */
67
  public function render_link($data, $values) {
68
    $tid = $this->get_value($values, 'tid');
69
    if (!empty($this->options['link_to_taxonomy']) && !empty($tid) && $data !== NULL && $data !== '') {
70
      $term = new stdClass();
71
      $term->tid = $tid;
72
      $term->vid = $this->get_value($values, 'vid');
73
      $term->name = $this->get_value($values, 'name');
74
      $term->vocabulary_machine_name = $values->{$this->aliases['vocabulary_machine_name']};
75
      $this->options['alter']['make_link'] = TRUE;
76
      $uri = entity_uri('taxonomy_term', $term);
77
      if (isset($uri['options'])) {
78
        $this->options['alter'] = array_merge($this->options['alter'], $uri['options']);
79
      }
80
      $this->options['alter']['path'] = $uri['path'];
81
      // If entity_uri() returned an options array, use it.
82
      if (isset($uri['options'])) {
83
        $this->options['alter'] = $uri['options'] + $this->options['alter'];
84
      }
85
    }
86

    
87
    if (!empty($this->options['convert_spaces'])) {
88
      $data = str_replace(' ', '-', $data);
89
    }
90

    
91
    return $data;
92
  }
93

    
94
  /**
95
   * {@inheritdoc}
96
   */
97
  public function render($values) {
98
    $value = $this->get_value($values);
99
    return $this->render_link($this->sanitize_value($value), $values);
100
  }
101

    
102
}