4 * Views' relationship handlers.
8 * @defgroup views_relationship_handlers Views' relationship handlers
10 * Handlers to tell Views how to create alternate relationships.
14 * Simple relationship handler that allows a new version of the primary table
17 * The base relationship handler can only handle a single join. Some relationships
18 * are more complex and might require chains of joins; for those, you must
19 * utilize a custom relationship handler.
22 * - base: The new base table this relationship will be adding. This does not
23 * have to be a declared base table, but if there are no tables that
24 * utilize this base table, it won't be very effective.
25 * - base field: The field to use in the relationship; if left out this iwll be
26 * assumed to be the primary field.
27 * - relationship table: The actual table this relationship operates against.
28 * This is analogous to using a 'table' override.
29 * - relationship field: The actual field this relationsihp operates against.
30 * This is analogous to using a 'real field' override.
31 * - label: The default label to provide for this relationship, which is
32 * shown in parentheses next to any field/sort/filter/argument that uses
35 class views_handler_relationship
extends views_handler
{
37 * Init handler to let relationships live on tables other than
38 * the table they operate on.
40 function init(&$view, $options) {
41 parent
::init($view, $options);
42 if (isset($this->definition
['relationship table'])) {
43 $this->table
= $this->definition
['relationship table'];
45 if (isset($this->definition
['relationship field'])) {
46 $this->field
= $this->definition
['relationship field'];
51 * Get this field's label.
54 if (!isset($this->options
['label'])) {
55 return $this->ui_name();
57 return $this->options
['label'];
60 function option_definition() {
61 $options = parent
::option_definition();
63 $label = !empty($this->definition
['label']) ?
$this->definition
['label'] : $this->definition
['field'];
64 $options['label'] = array('default' => $label, 'translatable' => TRUE
);
65 $options['required'] = array('default' => FALSE
);
71 * Default options form that provides the label widget that all fields
74 function options_form(&$form, &$form_state) {
75 $form['label'] = array(
76 '#type' => 'textfield',
77 '#title' => t('Label'),
78 '#default_value' => isset($this->options
['label']) ?
$this->options
['label'] : '',
79 '#description' => t('The label for this relationship that will be displayed only administratively.'),
82 $form['required'] = array(
83 '#type' => 'checkbox',
84 '#title' => t('Require this relationship'),
85 '#description' => t('If required, items that do not contain this relationship will not appear.'),
86 '#default_value' => !empty($this->options
['required']),
91 * Called to implement a relationship in a query.
94 // Figure out what base table this relationship brings to the party.
95 $table_data = views_fetch_data($this->definition
['base']);
96 $base_field = empty($this->definition
['base field']) ?
$table_data['table']['base']['field'] : $this->definition
['base field'];
98 $this->ensure_my_table();
100 $def = $this->definition
;
101 $def['table'] = $this->definition
['base'];
102 $def['field'] = $base_field;
103 $def['left_table'] = $this->table_alias
;
104 $def['left_field'] = $this->field
;
105 if (!empty($this->options
['required'])) {
106 $def['type'] = 'INNER';
109 if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
110 $join = new
$def['join_handler'];
113 $join = new
views_join();
116 $join->definition
= $def;
118 $join->adjusted
= TRUE
;
120 // use a short alias for this:
121 $alias = $def['table'] .
'_' .
$this->table
;
123 $this->alias
= $this->query
->add_relationship($alias, $join, $this->definition
['base'], $this->relationship
);
128 * A special handler to take the place of missing or broken handlers.
130 class views_handler_relationship_broken
extends views_handler_relationship
{
131 function ui_name($short = FALSE
) {
132 return t('Broken/missing handler');
135 function ensure_my_table() { /* No table to ensure! */ }
136 function query() { /* No query to run */ }
137 function options_form(&$form, &$form_state) {
138 $form['markup'] = array(
139 '#prefix' => '<div class="form-item description">',
140 '#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.'),
145 * Determine if the handler is considered 'broken'
147 function broken() { return TRUE
; }