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

Views' relationship handlers.

File

modules/taxonomy/views_handler_relationship_node_term_data.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Views' relationship handlers.
  5. */
  6. class views_handler_relationship_node_term_data extends views_handler_relationship {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['vids'] = array('default' => array());
  10. return $options;
  11. }
  12. /**
  13. * Default options form that provides the label widget that all fields
  14. * should have.
  15. */
  16. function options_form(&$form, &$form_state) {
  17. parent::options_form($form, $form_state);
  18. $vocabularies = taxonomy_get_vocabularies();
  19. $options = array();
  20. foreach ($vocabularies as $voc) {
  21. $options[$voc->vid] = check_plain($voc->name);
  22. }
  23. $form['vids'] = array(
  24. '#type' => 'checkboxes',
  25. '#title' => t('Vocabularies'),
  26. '#options' => $options,
  27. '#default_value' => $this->options['vids'],
  28. '#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.'),
  29. );
  30. }
  31. /**
  32. * Called to implement a relationship in a query.
  33. */
  34. function query() {
  35. $this->ensure_my_table();
  36. $def = $this->definition;
  37. $def['table'] = 'term_data';
  38. if (!empty($this->options['required']) || !array_filter($this->options['vids'])) {
  39. $term_node = $this->query->add_table('term_node', $this->relationship);
  40. $def['left_table'] = $term_node;
  41. $def['left_field'] = 'tid';
  42. $def['field'] = 'tid';
  43. if (!empty($this->options['required'])) {
  44. $def['type'] = 'INNER';
  45. }
  46. }
  47. else {
  48. // If the join is optional, join a subselect that will emulate term_data table instead
  49. $def['left_table'] = $this->table_alias;
  50. $def['left_field'] = 'vid';
  51. $def['field'] = 'revision';
  52. // fapi ensures vids are safe here.
  53. $vids = implode(', ', array_filter($this->options['vids']));
  54. $def['table formula'] = "(SELECT td.*, tn.vid AS revision FROM {term_data} td INNER JOIN {term_node} tn ON tn.tid = td.tid WHERE td.vid IN ($vids))";
  55. }
  56. $join = new views_join();
  57. $join->definition = $def;
  58. $join->construct();
  59. $join->adjusted = TRUE;
  60. // use a short alias for this:
  61. $alias = $def['table'] . '_' . $this->table;
  62. $this->alias = $this->query->add_relationship($alias, $join, 'term_data', $this->relationship);
  63. }
  64. }