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

File

modules/taxonomy/views_handler_field_term_node_tid.inc
View source
  1. <?php
  2. /**
  3. * Field handler for terms.
  4. */
  5. class views_handler_field_term_node_tid extends views_handler_field_prerender_list {
  6. function init(&$view, $options) {
  7. parent::init($view, $options);
  8. if ($view->base_table == 'node_revisions') {
  9. $this->additional_fields['vid'] = array('table' => 'node_revisions', 'field' => 'vid');
  10. }
  11. else {
  12. $this->additional_fields['vid'] = array('table' => 'node', 'field' => 'vid');
  13. }
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['link_to_taxonomy'] = array('default' => TRUE);
  18. $options['limit'] = array('default' => FALSE);
  19. $options['vids'] = array('default' => array());
  20. return $options;
  21. }
  22. /**
  23. * Provide "link to term" option.
  24. */
  25. function options_form(&$form, &$form_state) {
  26. parent::options_form($form, $form_state);
  27. $form['link_to_taxonomy'] = array(
  28. '#title' => t('Link this field to its term page'),
  29. '#type' => 'checkbox',
  30. '#default_value' => !empty($this->options['link_to_taxonomy']),
  31. );
  32. $form['limit'] = array(
  33. '#type' => 'checkbox',
  34. '#title' => t('Limit terms by vocabulary'),
  35. '#default_value'=> $this->options['limit'],
  36. );
  37. $options = array();
  38. $vocabularies = taxonomy_get_vocabularies();
  39. foreach ($vocabularies as $voc) {
  40. $options[$voc->vid] = check_plain($voc->name);
  41. }
  42. $form['vids'] = array(
  43. '#prefix' => '<div><div id="edit-options-vids">',
  44. '#suffix' => '</div></div>',
  45. '#type' => 'checkboxes',
  46. '#title' => t('Vocabularies'),
  47. '#options' => $options,
  48. '#default_value' => $this->options['vids'],
  49. '#process' => array('expand_checkboxes', 'views_process_dependency'),
  50. '#dependency' => array('edit-options-limit' => array(TRUE)),
  51. );
  52. }
  53. /**
  54. * Add this term to the query
  55. */
  56. function query() {
  57. $this->add_additional_fields();
  58. }
  59. function pre_render($values) {
  60. $this->field_alias = $this->aliases['vid'];
  61. $vids = array();
  62. foreach ($values as $result) {
  63. if (!empty($result->{$this->aliases['vid']})) {
  64. $vids[] = $result->{$this->aliases['vid']};
  65. }
  66. }
  67. if ($vids) {
  68. $voc = '';
  69. $voc_ids = array_filter($this->options['vids']);
  70. if (!empty($this->options['limit']) && !empty($voc_ids)) {
  71. $voc = " AND td.vid IN (" . implode(', ', array_keys($voc_ids)) . ")";
  72. }
  73. $result = db_query("SELECT tn.vid AS node_vid, td.*, v.name as vocabulary FROM {term_data} td INNER JOIN {term_node} tn ON td.tid = tn.tid INNER JOIN {vocabulary} v ON v.vid = td.vid WHERE tn.vid IN (" . implode(', ', $vids) . ")$voc ORDER BY td.weight, td.name");
  74. while ($term = db_fetch_object($result)) {
  75. $this->items[$term->node_vid][$term->tid]['name'] = check_plain($term->name);
  76. $this->items[$term->node_vid][$term->tid]['tid'] = $term->tid;
  77. $this->items[$term->node_vid][$term->tid]['vid'] = $term->vid;
  78. $this->items[$term->node_vid][$term->tid]['vocabulary'] = check_plain($term->vocabulary);
  79. if (!empty($this->options['link_to_taxonomy'])) {
  80. $this->items[$term->node_vid][$term->tid]['make_link'] = TRUE;
  81. $this->items[$term->node_vid][$term->tid]['path'] = taxonomy_term_path($term);
  82. }
  83. }
  84. }
  85. }
  86. function render_item($count, $item) {
  87. return $item['name'];
  88. }
  89. function document_self_tokens(&$tokens) {
  90. $tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
  91. $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
  92. $tokens['[' . $this->options['id'] . '-vid' . ']'] = t('The vocabulary ID for the vocabulary the term belongs to.');
  93. $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
  94. }
  95. function add_self_tokens(&$tokens, $item) {
  96. $tokens['[' . $this->options['id'] . '-tid' . ']'] = $item['tid'];
  97. $tokens['[' . $this->options['id'] . '-name' . ']'] = $item['name'];
  98. $tokens['[' . $this->options['id'] . '-vid' . ']'] = $item['vid'];
  99. $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = $item['vocabulary'];
  100. }
  101. }