9 * Implements hook_views_data().
11 function relation_views_data() {
12 // Define the base group of this table.
13 $data['relation']['table']['group'] = t('Relation');
15 // Advertise this table as a possible base table
16 $data['relation']['table']['base'] = array(
18 'title' => t('Relation'),
21 $data['relation']['table']['entity type'] = 'relation';
22 $data['relation']['rid'] = array(
24 'help' => t('The relation ID.'),
26 'handler' => 'views_handler_field_numeric',
27 'click sortable' => TRUE
,
30 'handler' => 'views_handler_argument_numeric',
34 'handler' => 'views_handler_filter_numeric',
37 'handler' => 'views_handler_sort',
41 $data['relation']['relation_type'] = array(
42 'title' => t('Relation type'),
43 'help' => t('The relation type.'),
45 'handler' => 'views_handler_field',
46 'click sortable' => TRUE
,
49 'handler' => 'views_handler_argument_string',
53 'handler' => 'views_handler_filter_string',
56 'handler' => 'views_handler_sort',
60 $data['relation']['vid'] = array(
62 'help' => t('The relation revision ID.'),
64 'handler' => 'views_handler_field_numeric',
65 'click sortable' => TRUE
,
68 'handler' => 'views_handler_argument_numeric',
72 'handler' => 'views_handler_filter_numeric',
75 'handler' => 'views_handler_sort',
78 $data['relation']['uid'] = array(
80 'help' => t('The relation uid.'),
82 'handler' => 'views_handler_field_numeric',
83 'click sortable' => TRUE
,
86 'handler' => 'views_handler_argument_numeric',
90 'handler' => 'views_handler_filter_numeric',
93 'handler' => 'views_handler_sort',
95 'relationship' => array(
96 'label' => 'relation author',
97 'title' => t('Relation Author'),
99 'base field' => 'uid',
103 $data['relation']['created'] = array(
104 'title' => t('Create Date'),
105 'help' => t('The date when the relation was created.'),
107 'handler' => 'views_handler_field_date',
108 'click sortable' => TRUE
,
111 'handler' => 'views_handler_argument_date',
115 'handler' => 'views_handler_filter_date',
118 'handler' => 'views_handler_sort',
122 $data['relation']['changed'] = array(
123 'title' => t('Change Date'),
124 'help' => t('The date when the relation was last changed.'),
126 'handler' => 'views_handler_field_date',
127 'click sortable' => TRUE
,
130 'handler' => 'views_handler_argument_date',
134 'handler' => 'views_handler_filter_date',
137 'handler' => 'views_handler_sort',
141 $data['relation']['arity'] = array(
142 'title' => t('Number of rows'),
143 'help' => t('The number of rows in this relation.'),
145 'handler' => 'views_handler_field_numeric',
146 'click sortable' => TRUE
,
149 'handler' => 'views_handler_argument_numeric',
153 'handler' => 'views_handler_filter_numeric',
156 'handler' => 'views_handler_sort',
160 // Define the base group of this table.
161 $data['field_data_endpoints']['table']['group'] = t('Endpoints');
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'];
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
,
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.
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',
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',
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',