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

Definition of views_handler_field_taxonomy.

File

modules/taxonomy/views_handler_field_taxonomy.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_taxonomy.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows linking to a taxonomy
  8. * term.
  9. *
  10. * @ingroup views_field_handlers
  11. */
  12. class views_handler_field_taxonomy extends views_handler_field {
  13. /**
  14. * Constructor to provide additional field to add.
  15. *
  16. * This constructer assumes the taxonomy_term_data table. If using another
  17. * table, we'll need to be more specific.
  18. */
  19. function construct() {
  20. parent::construct();
  21. $this->additional_fields['vid'] = 'vid';
  22. $this->additional_fields['tid'] = 'tid';
  23. $this->additional_fields['vocabulary_machine_name'] = array(
  24. 'table' => 'taxonomy_vocabulary',
  25. 'field' => 'machine_name',
  26. );
  27. }
  28. function option_definition() {
  29. $options = parent::option_definition();
  30. $options['link_to_taxonomy'] = array('default' => FALSE, 'bool' => TRUE);
  31. $options['convert_spaces'] = array('default' => FALSE, 'bool' => TRUE);
  32. return $options;
  33. }
  34. /**
  35. * Provide link to taxonomy option
  36. */
  37. function options_form(&$form, &$form_state) {
  38. $form['link_to_taxonomy'] = array(
  39. '#title' => t('Link this field to its taxonomy term page'),
  40. '#description' => t("Enable to override this field's links."),
  41. '#type' => 'checkbox',
  42. '#default_value' => !empty($this->options['link_to_taxonomy']),
  43. );
  44. $form['convert_spaces'] = array(
  45. '#title' => t('Convert spaces in term names to hyphens'),
  46. '#description' => t('This allows links to work with Views taxonomy term arguments.'),
  47. '#type' => 'checkbox',
  48. '#default_value' => !empty($this->options['convert_spaces']),
  49. );
  50. parent::options_form($form, $form_state);
  51. }
  52. /**
  53. * Render whatever the data is as a link to the taxonomy.
  54. *
  55. * Data should be made XSS safe prior to calling this function.
  56. */
  57. function render_link($data, $values) {
  58. $tid = $this->get_value($values, 'tid');
  59. if (!empty($this->options['link_to_taxonomy']) && !empty($tid) && $data !== NULL && $data !== '') {
  60. $term = new stdClass();
  61. $term->tid = $tid;
  62. $term->vid = $this->get_value($values, 'vid');
  63. $term->vocabulary_machine_name = $values->{$this->aliases['vocabulary_machine_name']};
  64. $this->options['alter']['make_link'] = TRUE;
  65. $uri = entity_uri('taxonomy_term', $term);
  66. $this->options['alter']['path'] = $uri['path'];
  67. }
  68. if (!empty($this->options['convert_spaces'])) {
  69. $data = str_replace(' ', '-', $data);
  70. }
  71. return $data;
  72. }
  73. function render($values) {
  74. $value = $this->get_value($values);
  75. return $this->render_link($this->sanitize_value($value), $values);
  76. }
  77. }