views_handler_field_taxonomy.inc

  1. 3.x modules/taxonomy/views_handler_field_taxonomy.inc
  2. 2.x modules/taxonomy/views_handler_field_taxonomy.inc

File

modules/taxonomy/views_handler_field_taxonomy.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide simple renderer that allows linking to a taxonomy
  4. * term.
  5. */
  6. class views_handler_field_taxonomy extends views_handler_field {
  7. /**
  8. * Constructor to provide additional field to add.
  9. *
  10. * This constructer assumes the term_data table. If using another
  11. * table, we'll need to be more specific.
  12. */
  13. function construct() {
  14. parent::construct();
  15. $this->additional_fields['vid'] = 'vid';
  16. $this->additional_fields['tid'] = 'tid';
  17. }
  18. function option_definition() {
  19. $options = parent::option_definition();
  20. $options['link_to_taxonomy'] = array('default' => FALSE);
  21. return $options;
  22. }
  23. /**
  24. * Provide link to taxonomy option
  25. */
  26. function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $form['link_to_taxonomy'] = array(
  29. '#title' => t('Link this field to its taxonomy term page'),
  30. '#description' => t('This will override any other link you have set.'),
  31. '#type' => 'checkbox',
  32. '#default_value' => !empty($this->options['link_to_taxonomy']),
  33. );
  34. }
  35. /**
  36. * Render whatever the data is as a link to the taxonomy.
  37. *
  38. * Data should be made XSS safe prior to calling this function.
  39. */
  40. function render_link($data, $values) {
  41. if (!empty($this->options['link_to_taxonomy']) && !empty($values->{$this->aliases['tid']}) && $data !== NULL && $data !== '') {
  42. $term = new stdClass();
  43. $term->tid = $values->{$this->aliases['tid']};
  44. $term->vid = $values->{$this->aliases['vid']};
  45. $this->options['alter']['make_link'] = TRUE;
  46. $this->options['alter']['path'] = taxonomy_term_path($term);
  47. }
  48. return $data;
  49. }
  50. function render($values) {
  51. return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  52. }
  53. }