Projet

Général

Profil

Paste
Télécharger (2,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_field_taxonomy.inc @ 7547bb19

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

    
32
  function option_definition() {
33
    $options = parent::option_definition();
34
    $options['link_to_taxonomy'] = array('default' => FALSE, 'bool' => TRUE);
35
    $options['convert_spaces'] = array('default' => FALSE, 'bool' => TRUE);
36
    return $options;
37
  }
38

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

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

    
79
    if (!empty($this->options['convert_spaces'])) {
80
      $data = str_replace(' ', '-', $data);
81
    }
82

    
83
    return $data;
84
  }
85

    
86
  function render($values) {
87
    $value = $this->get_value($values);
88
    return $this->render_link($this->sanitize_value($value), $values);
89
  }
90
}