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. * @ingroup views_relationship_handlers
  34. */
  35. class views_handler_relationship extends views_handler {
  36. /**
  37. * Init handler to let relationships live on tables other than
  38. * the table they operate on.
  39. */
  40. function init(&$view, &$options) {
  41. parent::init($view, $options);
  42. if (isset($this->definition['relationship table'])) {
  43. $this->table = $this->definition['relationship table'];
  44. }
  45. if (isset($this->definition['relationship field'])) {
  46. // Set both real_field and field so custom handler
  47. // can rely on the old field value.
  48. $this->real_field = $this->field = $this->definition['relationship field'];
  49. }
  50. }
  51. /**
  52. * Get this field's label.
  53. */
  54. function label() {
  55. if (!isset($this->options['label'])) {
  56. return $this->ui_name();
  57. }
  58. return $this->options['label'];
  59. }
  60. function option_definition() {
  61. $options = parent::option_definition();
  62. // Relationships definitions should define a default label, but if they aren't get another default value.
  63. if (!empty($this->definition['label'])) {
  64. $label = $this->definition['label'];
  65. }
  66. else {
  67. $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
  68. }
  69. $options['label'] = array('default' => $label, 'translatable' => TRUE);
  70. $options['required'] = array('default' => FALSE, 'bool' => TRUE);
  71. return $options;
  72. }
  73. /**
  74. * Default options form that provides the label widget that all fields
  75. * should have.
  76. */
  77. function options_form(&$form, &$form_state) {
  78. parent::options_form($form, $form_state);
  79. $form['label'] = array(
  80. '#type' => 'textfield',
  81. '#title' => t('Identifier'),
  82. '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
  83. '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
  84. '#required' => TRUE,
  85. );
  86. $form['required'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('Require this relationship'),
  89. '#description' => t('Enable to hide items that do not contain this relationship'),
  90. '#default_value' => !empty($this->options['required']),
  91. );
  92. }
  93. /**
  94. * Called to implement a relationship in a query.
  95. */
  96. function query() {
  97. // Figure out what base table this relationship brings to the party.
  98. $table_data = views_fetch_data($this->definition['base']);
  99. $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
  100. $this->ensure_my_table();
  101. $def = $this->definition;
  102. $def['table'] = $this->definition['base'];
  103. $def['field'] = $base_field;
  104. $def['left_table'] = $this->table_alias;
  105. $def['left_field'] = $this->real_field;
  106. if (!empty($this->options['required'])) {
  107. $def['type'] = 'INNER';
  108. }
  109. if (!empty($this->definition['extra'])) {
  110. $def['extra'] = $this->definition['extra'];
  111. }
  112. if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
  113. $join = new $def['join_handler'];
  114. }
  115. else {
  116. $join = new views_join();
  117. }
  118. $join->definition = $def;
  119. $join->options = $this->options;
  120. $join->construct();
  121. $join->adjusted = TRUE;
  122. // use a short alias for this:
  123. $alias = $def['table'] . '_' . $this->table;
  124. $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
  125. // Add access tags if the base table provide it.
  126. if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
  127. $access_tag = $table_data['table']['base']['access query tag'];
  128. $this->query->add_tag($access_tag);
  129. }
  130. }
  131. /**
  132. * You can't groupby a relationship.
  133. */
  134. function use_group_by() {
  135. return FALSE;
  136. }
  137. }
  138. /**
  139. * A special handler to take the place of missing or broken handlers.
  140. *
  141. * @ingroup views_relationship_handlers
  142. */
  143. class views_handler_relationship_broken extends views_handler_relationship {
  144. function ui_name($short = FALSE) {
  145. return t('Broken/missing handler');
  146. }
  147. function ensure_my_table() { /* No table to ensure! */ }
  148. function query() { /* No query to run */ }
  149. function options_form(&$form, &$form_state) {
  150. $form['markup'] = array(
  151. '#markup' => '<div class="form-item description">' . 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.') . '</div>',
  152. );
  153. }
  154. /**
  155. * Determine if the handler is considered 'broken'
  156. */
  157. function broken() { return TRUE; }
  158. }
  159. /**
  160. * @}
  161. */