Replace relationship definitions to cover all the bases and so Relation plays well...
[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_string',
54 ),
55 'sort' => array(
56 'handler' => 'views_handler_sort',
57 ),
58 );
59
60 $data['relation']['vid'] = array(
61 'title' => t('Vid'),
62 'help' => t('The relation revision ID.'),
63 'field' => array(
64 'handler' => 'views_handler_field_numeric',
65 'click sortable' => TRUE,
66 ),
67 'argument' => array(
68 'handler' => 'views_handler_argument_numeric',
69 'numeric' => TRUE,
70 ),
71 'filter' => array(
72 'handler' => 'views_handler_filter_numeric',
73 ),
74 'sort' => array(
75 'handler' => 'views_handler_sort',
76 ),
77 );
78 $data['relation']['uid'] = array(
79 'title' => t('Uid'),
80 'help' => t('The relation uid.'),
81 'field' => array(
82 'handler' => 'views_handler_field_numeric',
83 'click sortable' => TRUE,
84 ),
85 'argument' => array(
86 'handler' => 'views_handler_argument_numeric',
87 'numeric' => TRUE,
88 ),
89 'filter' => array(
90 'handler' => 'views_handler_filter_numeric',
91 ),
92 'sort' => array(
93 'handler' => 'views_handler_sort',
94 ),
95 'relationship' => array(
96 'label' => 'relation author',
97 'title' => t('Relation Author'),
98 'base' => 'users',
99 'base field' => 'uid',
100 ),
101 );
102
103 $data['relation']['created'] = array(
104 'title' => t('Create Date'),
105 'help' => t('The date when the relation was created.'),
106 'field' => array(
107 'handler' => 'views_handler_field_date',
108 'click sortable' => TRUE,
109 ),
110 'argument' => array(
111 'handler' => 'views_handler_argument_date',
112 'numeric' => TRUE,
113 ),
114 'filter' => array(
115 'handler' => 'views_handler_filter_date',
116 ),
117 'sort' => array(
118 'handler' => 'views_handler_sort',
119 ),
120 );
121
122 $data['relation']['changed'] = array(
123 'title' => t('Change Date'),
124 'help' => t('The date when the relation was last changed.'),
125 'field' => array(
126 'handler' => 'views_handler_field_date',
127 'click sortable' => TRUE,
128 ),
129 'argument' => array(
130 'handler' => 'views_handler_argument_date',
131 'numeric' => TRUE,
132 ),
133 'filter' => array(
134 'handler' => 'views_handler_filter_date',
135 ),
136 'sort' => array(
137 'handler' => 'views_handler_sort',
138 ),
139 );
140
141 $data['relation']['arity'] = array(
142 'title' => t('Number of rows'),
143 'help' => t('The number of rows in this relation.'),
144 'field' => array(
145 'handler' => 'views_handler_field_numeric',
146 'click sortable' => TRUE,
147 ),
148 'argument' => array(
149 'handler' => 'views_handler_argument_numeric',
150 'numeric' => TRUE,
151 ),
152 'filter' => array(
153 'handler' => 'views_handler_filter_numeric',
154 ),
155 'sort' => array(
156 'handler' => 'views_handler_sort',
157 ),
158 );
159
160 // Define the base group of this table.
161 $data['field_data_endpoints']['table']['group'] = t('Endpoints');
162
163 // Find out which entity type has which base table.
164 $entity_infos = entity_get_info();
165 $entity_tables = array();
166 foreach ($entity_infos as $entity_type => $entity_info) {
167 if (isset($entity_info['base table'])) {
168 $entity_tables[$entity_type] = $entity_info['base table'];
169 }
170 }
171 $field = field_info_field('endpoints');
172 $relation_data_table_name = _field_sql_storage_tablename($field);
173 $entity_id_field_name = _field_sql_storage_columnname('endpoints', 'entity_id');
174 $entity_type_field_name = _field_sql_storage_columnname('endpoints', 'entity_type');
175 // Build the relations between the different tables.
176 $types = relation_get_types();
177 foreach ($types as $type => $relation_type) {
178 $target_index = $relation_type->directional ? 'target_bundles' : 'source_bundles';
179 foreach ($relation_type->source_bundles as $source_bundle) {
180 $source_bundle = explode(':', $source_bundle, 2);
181 $entity_type_left = $source_bundle[0];
182 $relationship_field = $entity_infos[$entity_type_left]['entity keys']['id'];
183 $base_table_left = $entity_tables[$entity_type_left];
184 $t_arguments = array(
185 '@left' => $entity_type_left,
186 '@relation_type_label' => $relation_type->label,
187 );
188 foreach ($relation_type->$target_index as $target_bundle) {
189 $target_bundle = explode(':', $target_bundle, 2);
190 $entity_type_right = $target_bundle[0];
191 $base_table_right = $entity_tables[$entity_type_right];
192 $t_arguments['@right'] = $entity_type_right;
193 // Define [join and] relationship from relation to endpoints.
194 $data['relation']['relation_base_' . $type . '_right_endpoints'] = array(
195 'title' => t('Endpoints: @relation_type_label (relation -> endpoints)', $t_arguments),
196 'help' => t('Provides a relationship from the relation table to endpoints relative to the "@relation_type_label" relation', $t_arguments),
197 'relationship' => array(
198 // relation_handler_relationship::options_form() relies on this check_plain().
199 'label' => check_plain('Endpoints::' . $relation_type->label),
200 'base' => 'field_data_endpoints',
201 'base field' => 'entity_id',
202 'handler' => 'relation_handler_relationship', // To choose the r_index option.
203 'relation_type' => $type, // USE THIS as bundle
204 'entity_type_right' => $entity_type_right, // Use this as endpoint_entity_type.
205 'directional' => $relation_type->directional,
206 'max_arity' => $relation_type->max_arity,
207 'join_handler' => 'relation_handler_relation_endpoint_join', // To add the r_index condition to join.
208 ),
209 );
210
211 // Define join and relationship from endpoints to entity.
212 $data['field_data_endpoints']['table']['join'] = array(
213 // Directly links to the $entity_type_right table.
214 $entity_type_right => array(
215 'left_field' => $entity_infos[$entity_type_right]['entity keys']['id'],
216 'field' => 'endpoints_entity_id',
217 ),
218 );
219 $data['field_data_endpoints']['endpoints_base_' . $type . '_right_' . $entity_type_right] = array(
220 'title' => t('@relation_type_label (relation[@relation_type_label] -> @right)', $t_arguments),
221 'help' => t('Provides a relationship from the relation to @right using "@relation_type_label" relation', $t_arguments),
222 'relationship' => array(
223 'label' => check_plain('Endpoints::' . $relation_type->label),
224 'base' => $base_table_right,
225 'base field' => $entity_infos[$entity_type_right]['entity keys']['id'],
226 'relationship field' => 'endpoints_entity_id',
227 ),
228 );
229
230 // Define [join and] relationship from endpoints to endpoints.
231 $data['field_data_endpoints']['endpoints_base_' . $type . '_right_endpoints_' . $entity_type_right] = array(
232 'title' => t('@relation_type_label (@left -> relation[@relation_type_label])', $t_arguments),
233 'help' => t('Provides a relationship from @left to the relation using to the "@relation_type_label" relation', $t_arguments),
234 'relationship' => array(
235 // relation_handler_relationship::options_form() relies on this check_plain().
236 'label' => check_plain('Endpoints::' . $relation_type->label),
237 'base' => 'field_data_endpoints',
238 'base field' => 'entity_id',
239 'handler' => 'relation_handler_relationship',
240 'relation_type' => $type, // USE THIS as bundle
241 'entity_type_right' => $entity_type_right, // Use this as endpoint_entity_type.
242 'directional' => $relation_type->directional,
243 'join_handler' => 'relation_handler_endpoint_endpoint_join',
244 ),
245 );
246 }
247 }
248 }
249 return $data;
250 }