views_handler_field_term_node_tid.inc

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

Definition of views_handler_field_term_node_tid.

File

modules/taxonomy/views_handler_field_term_node_tid.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_term_node_tid.
  5. */
  6. /**
  7. * Field handler to display all taxonomy terms of a node.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_term_node_tid extends views_handler_field_prerender_list {
  12. function init(&$view, &$options) {
  13. parent::init($view, $options);
  14. // @todo: Wouldn't it be possible to use $this->base_table and no if here?
  15. if ($view->base_table == 'node_revision') {
  16. $this->additional_fields['nid'] = array('table' => 'node_revision', 'field' => 'nid');
  17. }
  18. else {
  19. $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
  20. }
  21. // Convert legacy vids option to machine name vocabularies.
  22. if (!empty($this->options['vids'])) {
  23. $vocabularies = taxonomy_get_vocabularies();
  24. foreach ($this->options['vids'] as $vid) {
  25. if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
  26. $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
  27. }
  28. }
  29. }
  30. }
  31. function option_definition() {
  32. $options = parent::option_definition();
  33. $options['link_to_taxonomy'] = array('default' => TRUE, 'bool' => TRUE);
  34. $options['limit'] = array('default' => FALSE, 'bool' => TRUE);
  35. $options['vocabularies'] = array('default' => array());
  36. return $options;
  37. }
  38. /**
  39. * Provide "link to term" option.
  40. */
  41. function options_form(&$form, &$form_state) {
  42. $form['link_to_taxonomy'] = array(
  43. '#title' => t('Link this field to its term page'),
  44. '#type' => 'checkbox',
  45. '#default_value' => !empty($this->options['link_to_taxonomy']),
  46. );
  47. $form['limit'] = array(
  48. '#type' => 'checkbox',
  49. '#title' => t('Limit terms by vocabulary'),
  50. '#default_value'=> $this->options['limit'],
  51. );
  52. $options = array();
  53. $vocabularies = taxonomy_get_vocabularies();
  54. foreach ($vocabularies as $voc) {
  55. $options[$voc->machine_name] = check_plain($voc->name);
  56. }
  57. $form['vocabularies'] = array(
  58. '#prefix' => '<div><div id="edit-options-vocabularies">',
  59. '#suffix' => '</div></div>',
  60. '#type' => 'checkboxes',
  61. '#title' => t('Vocabularies'),
  62. '#options' => $options,
  63. '#default_value' => $this->options['vocabularies'],
  64. '#dependency' => array('edit-options-limit' => array(TRUE)),
  65. );
  66. parent::options_form($form, $form_state);
  67. }
  68. /**
  69. * Add this term to the query
  70. */
  71. function query() {
  72. $this->add_additional_fields();
  73. }
  74. function pre_render(&$values) {
  75. $this->field_alias = $this->aliases['nid'];
  76. $nids = array();
  77. foreach ($values as $result) {
  78. if (!empty($result->{$this->aliases['nid']})) {
  79. $nids[] = $result->{$this->aliases['nid']};
  80. }
  81. }
  82. if ($nids) {
  83. $query = db_select('taxonomy_term_data', 'td');
  84. $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
  85. $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  86. $query->fields('td');
  87. $query->addField('tn', 'nid', 'node_nid');
  88. $query->addField('tv', 'name', 'vocabulary');
  89. $query->addField('tv', 'machine_name', 'vocabulary_machine_name');
  90. $query->orderby('td.weight');
  91. $query->orderby('td.name');
  92. $query->condition('tn.nid', $nids);
  93. $query->addTag('term_access');
  94. $vocabs = array_filter($this->options['vocabularies']);
  95. if (!empty($this->options['limit']) && !empty($vocabs)) {
  96. $query->condition('tv.machine_name', $vocabs);
  97. }
  98. $result = $query->execute();
  99. foreach ($result as $term) {
  100. $this->items[$term->node_nid][$term->tid]['name'] = check_plain($term->name);
  101. $this->items[$term->node_nid][$term->tid]['tid'] = $term->tid;
  102. $this->items[$term->node_nid][$term->tid]['vocabulary_machine_name'] = check_plain($term->vocabulary_machine_name);
  103. $this->items[$term->node_nid][$term->tid]['vocabulary'] = check_plain($term->vocabulary);
  104. if (!empty($this->options['link_to_taxonomy'])) {
  105. $this->items[$term->node_nid][$term->tid]['make_link'] = TRUE;
  106. $this->items[$term->node_nid][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
  107. }
  108. }
  109. }
  110. }
  111. function render_item($count, $item) {
  112. return $item['name'];
  113. }
  114. function document_self_tokens(&$tokens) {
  115. $tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
  116. $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
  117. $tokens['[' . $this->options['id'] . '-vocabulary-machine-name' . ']'] = t('The machine name for the vocabulary the term belongs to.');
  118. $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
  119. }
  120. function add_self_tokens(&$tokens, $item) {
  121. foreach(array('tid', 'name', 'vocabulary_machine_name', 'vocabulary') as $token) {
  122. // Replace _ with - for the vocabulary machine name.
  123. $tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token). ']'] = isset($item[$token]) ? $item[$token] : '';
  124. }
  125. }
  126. }