views_handler_relationship_node_term_data.inc

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

Definition of views_handler_relationship_node_term_data.

File

modules/taxonomy/views_handler_relationship_node_term_data.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_relationship_node_term_data.
  5. */
  6. /**
  7. * Relationship handler to return the taxonomy terms of nodes.
  8. *
  9. * @ingroup views_relationship_handlers
  10. */
  11. class views_handler_relationship_node_term_data extends views_handler_relationship {
  12. function init(&$view, &$options) {
  13. parent::init($view, $options);
  14. // Convert legacy vids option to machine name vocabularies.
  15. if (!empty($this->options['vids'])) {
  16. $vocabularies = taxonomy_get_vocabularies();
  17. foreach ($this->options['vids'] as $vid) {
  18. if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
  19. $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
  20. }
  21. }
  22. }
  23. }
  24. function option_definition() {
  25. $options = parent::option_definition();
  26. $options['vocabularies'] = array('default' => array());
  27. return $options;
  28. }
  29. function options_form(&$form, &$form_state) {
  30. $vocabularies = taxonomy_get_vocabularies();
  31. $options = array();
  32. foreach ($vocabularies as $voc) {
  33. $options[$voc->machine_name] = check_plain($voc->name);
  34. }
  35. $form['vocabularies'] = array(
  36. '#type' => 'checkboxes',
  37. '#title' => t('Vocabularies'),
  38. '#options' => $options,
  39. '#default_value' => $this->options['vocabularies'],
  40. '#description' => t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
  41. );
  42. parent::options_form($form, $form_state);
  43. }
  44. /**
  45. * Called to implement a relationship in a query.
  46. */
  47. function query() {
  48. $this->ensure_my_table();
  49. $def = $this->definition;
  50. $def['table'] = 'taxonomy_term_data';
  51. if (!array_filter($this->options['vocabularies'])) {
  52. $taxonomy_index = $this->query->add_table('taxonomy_index', $this->relationship);
  53. $def['left_table'] = $taxonomy_index;
  54. $def['left_field'] = 'tid';
  55. $def['field'] = 'tid';
  56. $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
  57. }
  58. else {
  59. // If vocabularies are supplied join a subselect instead
  60. $def['left_table'] = $this->table_alias;
  61. $def['left_field'] = 'nid';
  62. $def['field'] = 'nid';
  63. $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
  64. $query = db_select('taxonomy_term_data', 'td');
  65. $query->addJoin($def['type'], 'taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  66. $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
  67. $query->condition('tv.machine_name', array_filter($this->options['vocabularies']));
  68. if (empty($this->query->options['disable_sql_rewrite'])) {
  69. $query->addTag('term_access');
  70. }
  71. $query->fields('td');
  72. $query->fields('tn', array('nid'));
  73. $def['table formula'] = $query;
  74. }
  75. $join = new views_join();
  76. $join->definition = $def;
  77. $join->construct();
  78. $join->adjusted = TRUE;
  79. // use a short alias for this:
  80. $alias = $def['table'] . '_' . $this->table;
  81. $this->alias = $this->query->add_relationship($alias, $join, 'taxonomy_term_data', $this->relationship);
  82. }
  83. }