5 * Views' relationship handlers.
9 * @defgroup views_relationship_handlers Views relationship handlers
11 * Handlers to tell Views how to create alternate relationships.
15 * Simple relationship handler that allows a new version of the primary table
18 * The base relationship handler can only handle a single join. Some relationships
19 * are more complex and might require chains of joins; for those, you must
20 * utilize a custom relationship handler.
23 * - base: The new base table this relationship will be adding. This does not
24 * have to be a declared base table, but if there are no tables that
25 * utilize this base table, it won't be very effective.
26 * - base field: The field to use in the relationship; if left out this will be
27 * assumed to be the primary field.
28 * - relationship table: The actual table this relationship operates against.
29 * This is analogous to using a 'table' override.
30 * - relationship field: The actual field this relationship operates against.
31 * This is analogous to using a 'real field' override.
32 * - label: The default label to provide for this relationship, which is
33 * shown in parentheses next to any field/sort/filter/argument that uses
36 * @ingroup views_relationship_handlers
38 class views_handler_relationship extends views_handler {
40 * Init handler to let relationships live on tables other than
41 * the table they operate on.
43 function init(&$view, &$options) {
44 parent::init($view, $options);
45 if (isset($this->definition['relationship table'])) {
46 $this->table = $this->definition['relationship table'];
48 if (isset($this->definition['relationship field'])) {
49 // Set both real_field and field so custom handler
50 // can rely on the old field value.
51 $this->real_field = $this->field = $this->definition['relationship field'];
56 * Get this field's label.
59 if (!isset($this->options['label'])) {
60 return $this->ui_name();
62 return $this->options['label'];
65 function option_definition() {
66 $options = parent::option_definition();
69 // Relationships definitions should define a default label, but if they aren't get another default value.
70 if (!empty($this->definition['label'])) {
71 $label = $this->definition['label'];
74 $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
77 $options['label'] = array('default' => $label, 'translatable' => TRUE);
78 $options['required'] = array('default' => FALSE, 'bool' => TRUE);
84 * Default options form that provides the label widget that all fields
87 function options_form(&$form, &$form_state) {
88 parent::options_form($form, $form_state);
89 $form['label'] = array(
90 '#type' => 'textfield',
91 '#title' => t('Identifier'),
92 '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
93 '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
97 $form['required'] = array(
98 '#type' => 'checkbox',
99 '#title' => t('Require this relationship'),
100 '#description' => t('Enable to hide items that do not contain this relationship'),
101 '#default_value' => !empty($this->options['required']),
106 * Called to implement a relationship in a query.
109 // Figure out what base table this relationship brings to the party.
110 $table_data = views_fetch_data($this->definition['base']);
111 $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
113 $this->ensure_my_table();
115 $def = $this->definition;
116 $def['table'] = $this->definition['base'];
117 $def['field'] = $base_field;
118 $def['left_table'] = $this->table_alias;
119 $def['left_field'] = $this->real_field;
120 if (!empty($this->options['required'])) {
121 $def['type'] = 'INNER';
124 if (!empty($this->definition['extra'])) {
125 $def['extra'] = $this->definition['extra'];
128 if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
129 $join = new $def['join_handler'];
132 $join = new views_join();
135 $join->definition = $def;
136 $join->options = $this->options;
138 $join->adjusted = TRUE;
140 // use a short alias for this:
141 $alias = $def['table'] . '_' . $this->table;
143 $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
145 // Add access tags if the base table provide it.
146 if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
147 $access_tag = $table_data['table']['base']['access query tag'];
148 $this->query->add_tag($access_tag);
153 * You can't groupby a relationship.
155 function use_group_by() {
161 * A special handler to take the place of missing or broken handlers.
163 * @ingroup views_relationship_handlers
165 class views_handler_relationship_broken extends views_handler_relationship {
166 function ui_name($short = FALSE) {
167 return t('Broken/missing handler');
170 function ensure_my_table() { /* No table to ensure! */ }
171 function query() { /* No query to run */ }
172 function options_form(&$form, &$form_state) {
173 $form['markup'] = array(
174 '#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>',
179 * Determine if the handler is considered 'broken'
181 function broken() { return TRUE; }