One Relationship Handler to Rule Them All. (Props to solotandem for the wild idea...
[project/relation.git] / views / relation.views.inc
1 <?php
2
3 /**
4 * @file
5 * Views support.
6 */
7
8 /**
9 * Implements hook_views_data().
10 */
11 function relation_views_data() {
12 // Define the base group of this table.
13 $data['relation']['table']['group'] = t('Relation');
14
15 // Advertise this table as a possible base table
16 $data['relation']['table']['base'] = array(
17 'field' => 'rid',
18 'title' => t('Relation'),
19 'weight' => -10,
20 );
21 $data['relation']['table']['entity type'] = 'relation';
22 $data['relation']['rid'] = array(
23 'title' => t('Rid'),
24 'help' => t('The relation ID.'),
25 'field' => array(
26 'handler' => 'views_handler_field_numeric',
27 'click sortable' => TRUE,
28 ),
29 'argument' => array(
30 'handler' => 'views_handler_argument_numeric',
31 'numeric' => TRUE,
32 ),
33 'filter' => array(
34 'handler' => 'views_handler_filter_numeric',
35 ),
36 'sort' => array(
37 'handler' => 'views_handler_sort',
38 ),
39 );
40
41 $data['relation']['relation_type'] = array(
42 'title' => t('Relation type'),
43 'help' => t('The relation type.'),
44 'field' => array(
45 'handler' => 'views_handler_field',
46 'click sortable' => TRUE,
47 ),
48 'argument' => array(
49 'handler' => 'views_handler_argument_string',
50 'numeric' => TRUE,
51 ),
52 'filter' => array(
53 'handler' => 'views_handler_filter_in_operator',
54 'options callback' => 'relation_get_types_options',
55 ),
56 'sort' => array(
57 'handler' => 'views_handler_sort',
58 ),
59 );
60
61
62 $data['relation']['vid'] = array(
63 'title' => t('Vid'),
64 'help' => t('The relation revision ID.'),
65 'field' => array(
66 'handler' => 'views_handler_field_numeric',
67 'click sortable' => TRUE,
68 ),
69 'argument' => array(
70 'handler' => 'views_handler_argument_numeric',
71 'numeric' => TRUE,
72 ),
73 'filter' => array(
74 'handler' => 'views_handler_filter_numeric',
75 ),
76 'sort' => array(
77 'handler' => 'views_handler_sort',
78 ),
79 );
80 $data['relation']['uid'] = array(
81 'title' => t('Uid'),
82 'help' => t('The relation uid.'),
83 'field' => array(
84 'handler' => 'views_handler_field_numeric',
85 'click sortable' => TRUE,
86 ),
87 'argument' => array(
88 'handler' => 'views_handler_argument_numeric',
89 'numeric' => TRUE,
90 ),
91 'filter' => array(
92 'handler' => 'views_handler_filter_numeric',
93 ),
94 'sort' => array(
95 'handler' => 'views_handler_sort',
96 ),
97 'relationship' => array(
98 'label' => 'relation author',
99 'title' => t('Relation Author'),
100 'base' => 'users',
101 'base field' => 'uid',
102 ),
103 );
104
105 $data['relation']['created'] = array(
106 'title' => t('Create Date'),
107 'help' => t('The date when the relation was created.'),
108 'field' => array(
109 'handler' => 'views_handler_field_date',
110 'click sortable' => TRUE,
111 ),
112 'argument' => array(
113 'handler' => 'views_handler_argument_date',
114 'numeric' => TRUE,
115 ),
116 'filter' => array(
117 'handler' => 'views_handler_filter_date',
118 ),
119 'sort' => array(
120 'handler' => 'views_handler_sort',
121 ),
122 );
123
124 $data['relation']['changed'] = array(
125 'title' => t('Change Date'),
126 'help' => t('The date when the relation was last changed.'),
127 'field' => array(
128 'handler' => 'views_handler_field_date',
129 'click sortable' => TRUE,
130 ),
131 'argument' => array(
132 'handler' => 'views_handler_argument_date',
133 'numeric' => TRUE,
134 ),
135 'filter' => array(
136 'handler' => 'views_handler_filter_date',
137 ),
138 'sort' => array(
139 'handler' => 'views_handler_sort',
140 ),
141 );
142
143 $data['relation']['arity'] = array(
144 'title' => t('Number of rows'),
145 'help' => t('The number of rows in this relation.'),
146 'field' => array(
147 'handler' => 'views_handler_field_numeric',
148 'click sortable' => TRUE,
149 ),
150 'argument' => array(
151 'handler' => 'views_handler_argument_numeric',
152 'numeric' => TRUE,
153 ),
154 'filter' => array(
155 'handler' => 'views_handler_filter_numeric',
156 ),
157 'sort' => array(
158 'handler' => 'views_handler_sort',
159 ),
160 );
161 return $data;
162 }
163
164
165 /**
166 * Implements hook_views_data_alter().
167 */
168 function relation_views_data_alter(&$data) {
169 // Find out which entity type has which base table.
170 $entity_infos = entity_get_info();
171 $entity_tables = array();
172 foreach ($entity_infos as $entity_type => $entity_info) {
173 if (isset($entity_info['base table'])) {
174 $entity_tables[$entity_type] = $entity_info['base table'];
175 }
176 }
177 $field = field_info_field('endpoints');
178 $relation_data_table_name = _field_sql_storage_tablename($field);
179 $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
180 $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
181 // Build the relations between the different tables.
182 $types = relation_get_types();
183 foreach ($types as $type => $relation_type) {
184 $target_index = $relation_type->directional ? 'target_bundles' : 'source_bundles';
185 foreach ($relation_type->source_bundles as $source_bundle) {
186 $source_bundle = explode(':', $source_bundle, 2);
187 $entity_type_left = $source_bundle[0];
188 $relationship_field = $entity_infos[$entity_type_left]['entity keys']['id'];
189 $base_table_left = $entity_tables[$entity_type_left];
190 $t_arguments = array(
191 '@left' => $entity_type_left,
192 '@relation_type_label' => $relation_type->label,
193 );
194 $data[$base_table_left]['relation_base_left_' . $type] = array(
195 'title' => t('Relation: @relation_type_label (@left -> relation)', $t_arguments),
196 'help' => t('Provides a relationship from @left to the relation table via the relation @relation_type_label', $t_arguments),
197 'relationship' => array(
198 // relation_handler_relationship::options_form() relies on this check_plain().
199 'label' => check_plain($relation_type->label),
200 'base' => 'relation',
201 'base field' => 'rid',
202 'relationship field' => $relationship_field,
203 'handler' => 'relation_handler_relationship',
204 'relation_type' => $type,
205 'entity_type_left' => $entity_type_left,
206 'directional' => $relation_type->directional,
207 ),
208 );
209 foreach ($relation_type->$target_index as $target_bundle) {
210 $target_bundle = explode(':', $target_bundle, 2);
211 $entity_type_right = $target_bundle[0];
212 $base_table_right = $entity_tables[$entity_type_right];
213 $t_arguments['@right'] = $entity_type_right;
214 $data[$base_table_left]['relation_' . $type . '_' . $entity_type_right] = array(
215 'title' => t('Relation: @relation_type_label (@left <-> @right)', $t_arguments),
216 'help' => t('Provides a relationship from @left to @right via the relation @relation_type_label', $t_arguments),
217 'relationship' => array(
218 // relation_handler_relationship::options_form() relies on this check_plain().
219 'label' => check_plain($relation_type->label),
220 'base' => $base_table_right,
221 'base field' => $entity_infos[$entity_type_right]['entity keys']['id'],
222 'relationship field' => $relationship_field,
223 'handler' => 'relation_handler_relationship',
224 'relation_type' => $type,
225 'entity_type_left' => $entity_type_left,
226 'entity_type_right' => $entity_type_right,
227 'directional' => $relation_type->directional,
228 ),
229 );
230 $data['relation']['relation_base_' . $type . '_' . $entity_type_right] = array(
231 'title' => t('Relation: @relation_type_label (relation -> @right)', $t_arguments),
232 'help' => t('Provides a relationship from the relation table to @right via the relation @relation_type_label', $t_arguments),
233 'relationship' => array(
234 // relation_handler_relationship::options_form() relies on this check_plain().
235 'label' => check_plain($relation_type->label),
236 'base' => $base_table_right,
237 'base field' => $entity_infos[$entity_type_right]['entity keys']['id'],
238 'relationship field' => 'rid',
239 'handler' => 'relation_handler_relationship',
240 'relation_type' => $type,
241 'entity_type_right' => $entity_type_right,
242 'directional' => $relation_type->directional,
243 ),
244 );
245 }
246 }
247 }
248 }