views_handler_relationship.inc

  1. 3.x handlers/views_handler_relationship.inc
  2. 2.x handlers/views_handler_relationship.inc

Views' relationship handlers.

File

handlers/views_handler_relationship.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Views' relationship handlers.
  5. */
  6. /**
  7. * @defgroup views_relationship_handlers Views' relationship handlers
  8. * @{
  9. * Handlers to tell Views how to create alternate relationships.
  10. */
  11. /**
  12. * Simple relationship handler that allows a new version of the primary table
  13. * to be linked in.
  14. *
  15. * The base relationship handler can only handle a single join. Some relationships
  16. * are more complex and might require chains of joins; for those, you must
  17. * utilize a custom relationship handler.
  18. *
  19. * Definition items:
  20. * - base: The new base table this relationship will be adding. This does not
  21. * have to be a declared base table, but if there are no tables that
  22. * utilize this base table, it won't be very effective.
  23. * - base field: The field to use in the relationship; if left out this will be
  24. * assumed to be the primary field.
  25. * - relationship table: The actual table this relationship operates against.
  26. * This is analogous to using a 'table' override.
  27. * - relationship field: The actual field this relationship operates against.
  28. * This is analogous to using a 'real field' override.
  29. * - label: The default label to provide for this relationship, which is
  30. * shown in parentheses next to any field/sort/filter/argument that uses
  31. * the relationship.
  32. */
  33. class views_handler_relationship extends views_handler {
  34. /**
  35. * Init handler to let relationships live on tables other than
  36. * the table they operate on.
  37. */
  38. function init(&$view, $options) {
  39. parent::init($view, $options);
  40. if (isset($this->definition['relationship table'])) {
  41. $this->table = $this->definition['relationship table'];
  42. }
  43. if (isset($this->definition['relationship field'])) {
  44. $this->field = $this->definition['relationship field'];
  45. }
  46. }
  47. /**
  48. * Get this field's label.
  49. */
  50. function label() {
  51. if (!isset($this->options['label'])) {
  52. return $this->ui_name();
  53. }
  54. return $this->options['label'];
  55. }
  56. function option_definition() {
  57. $options = parent::option_definition();
  58. $label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['field'];
  59. $options['label'] = array('default' => $label, 'translatable' => TRUE);
  60. $options['required'] = array('default' => FALSE);
  61. return $options;
  62. }
  63. /**
  64. * Default options form that provides the label widget that all fields
  65. * should have.
  66. */
  67. function options_form(&$form, &$form_state) {
  68. $form['label'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('Label'),
  71. '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
  72. '#description' => t('The label for this relationship that will be displayed only administratively.'),
  73. );
  74. $form['required'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Require this relationship'),
  77. '#description' => t('If required, items that do not contain this relationship will not appear.'),
  78. '#default_value' => !empty($this->options['required']),
  79. );
  80. }
  81. /**
  82. * Called to implement a relationship in a query.
  83. */
  84. function query() {
  85. // Figure out what base table this relationship brings to the party.
  86. $table_data = views_fetch_data($this->definition['base']);
  87. $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
  88. $this->ensure_my_table();
  89. $def = $this->definition;
  90. $def['table'] = $this->definition['base'];
  91. $def['field'] = $base_field;
  92. $def['left_table'] = $this->table_alias;
  93. $def['left_field'] = $this->field;
  94. if (!empty($this->options['required'])) {
  95. $def['type'] = 'INNER';
  96. }
  97. if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
  98. $join = new $def['join_handler'];
  99. }
  100. else {
  101. $join = new views_join();
  102. }
  103. $join->definition = $def;
  104. $join->construct();
  105. $join->adjusted = TRUE;
  106. // use a short alias for this:
  107. $alias = $def['table'] . '_' . $this->table;
  108. $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
  109. }
  110. }
  111. /**
  112. * A special handler to take the place of missing or broken handlers.
  113. */
  114. class views_handler_relationship_broken extends views_handler_relationship {
  115. function ui_name($short = FALSE) {
  116. return t('Broken/missing handler');
  117. }
  118. function ensure_my_table() { /* No table to ensure! */ }
  119. function query() { /* No query to run */ }
  120. function options_form(&$form, &$form_state) {
  121. $form['markup'] = array(
  122. '#prefix' => '<div class="form-item description">',
  123. '#value' => t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.'),
  124. );
  125. }
  126. /**
  127. * Determine if the handler is considered 'broken'
  128. */
  129. function broken() { return TRUE; }
  130. }
  131. /**
  132. * @}
  133. */