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_in_operator',
54 'options callback' => 'relation_get_types_options',
57 'handler' => 'views_handler_sort',
62 $data['relation']['vid'] = array(
64 'help' => t('The relation revision ID.'),
66 'handler' => 'views_handler_field_numeric',
67 'click sortable' => TRUE
,
70 'handler' => 'views_handler_argument_numeric',
74 'handler' => 'views_handler_filter_numeric',
77 'handler' => 'views_handler_sort',
80 $data['relation']['uid'] = array(
82 'help' => t('The relation uid.'),
84 'handler' => 'views_handler_field_numeric',
85 'click sortable' => TRUE
,
88 'handler' => 'views_handler_argument_numeric',
92 'handler' => 'views_handler_filter_numeric',
95 'handler' => 'views_handler_sort',
97 'relationship' => array(
98 'label' => 'relation author',
99 'title' => t('Relation Author'),
101 'base field' => 'uid',
105 $data['relation']['created'] = array(
106 'title' => t('Create Date'),
107 'help' => t('The date when the relation was created.'),
109 'handler' => 'views_handler_field_date',
110 'click sortable' => TRUE
,
113 'handler' => 'views_handler_argument_date',
117 'handler' => 'views_handler_filter_date',
120 'handler' => 'views_handler_sort',
124 $data['relation']['changed'] = array(
125 'title' => t('Change Date'),
126 'help' => t('The date when the relation was last changed.'),
128 'handler' => 'views_handler_field_date',
129 'click sortable' => TRUE
,
132 'handler' => 'views_handler_argument_date',
136 'handler' => 'views_handler_filter_date',
139 'handler' => 'views_handler_sort',
143 $data['relation']['arity'] = array(
144 'title' => t('Number of rows'),
145 'help' => t('The number of rows in this relation.'),
147 'handler' => 'views_handler_field_numeric',
148 'click sortable' => TRUE
,
151 'handler' => 'views_handler_argument_numeric',
155 'handler' => 'views_handler_filter_numeric',
158 'handler' => 'views_handler_sort',
166 * Implements hook_views_data_alter().
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'];
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
,
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
,
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
,
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
,