5 * Definition of views_handler_field_taxonomy.
9 * Field handler to provide simple renderer that allows linking to a taxonomy
12 * @ingroup views_field_handlers
14 class views_handler_field_taxonomy extends views_handler_field {
16 * Constructor to provide additional field to add.
18 * This constructer assumes the taxonomy_term_data table. If using another
19 * table, we'll need to be more specific.
21 function 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',
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);
40 * Provide link to taxonomy option
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']),
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']),
55 parent::options_form($form, $form_state);
59 * Render whatever the data is as a link to the taxonomy.
61 * Data should be made XSS safe prior to calling this function.
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();
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']);
76 $this->options['alter']['path'] = $uri['path'];
79 if (!empty($this->options['convert_spaces'])) {
80 $data = str_replace(' ', '-', $data);
86 function render($values) {
87 $value = $this->get_value($values);
88 return $this->render_link($this->sanitize_value($value), $values);