-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_entity_reference.views.inc
262 lines (246 loc) · 11.8 KB
/
dynamic_entity_reference.views.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* @file
* Provides views data for the dynamic_entity_reference module.
*/
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem;
use Drupal\field\FieldStorageConfigInterface;
/**
* Implements hook_field_views_data().
*/
function dynamic_entity_reference_field_views_data(FieldStorageConfigInterface $field_storage) {
$data = views_field_default_views_data($field_storage);
$entity_manager = \Drupal::entityTypeManager();
$labels = \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE);
$options = array_keys($labels[(string) t('Content', [], ['context' => 'Entity type group'])]);
$settings = $field_storage->getSettings();
// Identify all the target entity type ids that can be referenced.
if ($settings['exclude_entity_types']) {
$target_entity_type_ids = array_diff($options, $settings['entity_type_ids'] ?: []);
}
else {
$target_entity_type_ids = array_intersect($options, $settings['entity_type_ids'] ?: []);
}
foreach ($data as $table_name => $table_data) {
// Add a relationship to all the target entity types.
foreach ($target_entity_type_ids as $target_entity_type_id) {
if ($entity_manager->hasHandler($target_entity_type_id, 'views_data')) {
$target_entity_type = $entity_manager->getDefinition($target_entity_type_id);
$entity_type_id = $field_storage->getTargetEntityTypeId();
$entity_type = $entity_manager->getDefinition($entity_type_id);
$target_base_table = $entity_manager->getHandler($target_entity_type_id, 'views_data')
->getViewsTableForEntityType($target_entity_type);
$field_name = $field_storage->getName();
// Provide a relationship for the entity type with the dynamic entity
// reference field.
$args = [
'@label' => $target_entity_type->getLabel(),
'@field_name' => $field_name,
];
$data[$table_name][$target_entity_type_id . '__' . $field_name]['relationship'] = [
'title' => t('@label referenced from @field_name', $args),
'label' => t('@field_name: @label', $args),
'group' => $entity_type->getLabel(),
'help' => t('Appears in: @bundles.', [
'@bundles' => implode(', ', $field_storage->getBundles()),
]),
'id' => 'standard',
'base' => $target_base_table,
'entity type' => $target_entity_type_id,
'base field' => $target_entity_type->getKey('id'),
'relationship field' => $field_name . '_target_id',
// Entity reference field only has one target type whereas dynamic
// entity reference field can have multiple target types that is why
// an extra join condition on target types is needed.
'extra' => [
[
'left_field' => $field_name . '_target_type',
'value' => $target_entity_type_id,
],
],
];
// Provide a reverse relationship for the entity type that is referenced
// by the field.
$pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_manager->getStorage($entity_type_id)
->getTableMapping();
$args['@entity'] = $entity_type->getLabel();
$args['@label'] = $target_entity_type->getSingularLabel();
$data[$target_base_table][$pseudo_field_name]['relationship'] = [
'title' => t('@entity using @field_name', $args),
'label' => t('@field_name', ['@field_name' => $field_name]),
'group' => $target_entity_type->getLabel(),
'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
'id' => 'entity_reverse',
'base' => $entity_manager->getHandler($entity_type_id, 'views_data')
->getViewsTableForEntityType($entity_type),
'entity_type' => $entity_type_id,
'base field' => $entity_type->getKey('id'),
'field_name' => $field_name,
'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
'field field' => $field_name . '_target_id',
// Entity reference field only has one target type whereas dynamic
// entity reference field can have multiple target types that is why
// an extra join condition on target types is needed.
'join_extra' => [
[
'field' => $field_name . '_target_type',
'value' => $target_entity_type_id,
],
[
'field' => 'deleted',
'value' => 0,
'numeric' => TRUE,
],
],
];
}
}
}
return $data;
}
/**
* Implements hook_views_data().
*
* Adds relationships for dynamic_entity_reference base fields.
*
* @todo remove this when https://www.drupal.org/node/2337515 is in.
*/
function dynamic_entity_reference_views_data() {
$entity_manager = \Drupal::entityTypeManager();
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types = [];
$sql_entity_types = [];
$fields_all = [];
// Ensure origin and target entity types are SQL.
foreach ($entity_manager->getDefinitions() as $entity_type) {
// \Drupal\views\EntityViewsData class only allows entities with
// \Drupal\Core\Entity\Sql\SqlEntityStorageInterface.
if ($entity_type->hasHandlerClass('views_data') && $entity_manager->getStorage($entity_type->id()) instanceof SqlEntityStorageInterface) {
$sql_entity_types[$entity_type->id()] = $entity_type->id();
// Only fieldable entities have base fields.
if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
$entity_types[$entity_type->id()] = $entity_type;
$entity_field_manager = \Drupal::service('entity_field.manager');
foreach ($entity_field_manager->getBaseFieldDefinitions($entity_type->id()) as $base_field) {
if ($base_field->getType() == 'dynamic_entity_reference' && !$base_field->isComputed()) {
$fields_all[$entity_type->id()][] = $base_field;
}
}
}
}
}
$data = [];
foreach ($fields_all as $entity_type_id => $fields) {
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
$entity_type = $entity_types[$entity_type_id];
$base_table = $entity_manager->getHandler($entity_type_id, 'views_data')->getViewsTableForEntityType($entity_type);
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
foreach ($fields as $field) {
$field_name = $field->getName();
$columns = $table_mapping->getColumnNames($field_name);
$column_id = $columns['target_id'];
$column_type = $columns['target_type'];
// Unlimited (-1) or > 1 store field data in a dedicated table.
$table = $field->getCardinality() == 1 ? $base_table : $entity_type->getBaseTable() . '__' . $field_name;
// Filter out non SQL entity types because \Drupal\views\EntityViewsData
// class only allows entities with
// \Drupal\Core\Entity\Sql\SqlEntityStorageInterface.
$targets = array_intersect(DynamicEntityReferenceItem::getTargetTypes($field->getSettings()), array_keys($sql_entity_types));
foreach ($targets as $target_entity_type_id) {
if ($entity_manager->hasHandler($target_entity_type_id, 'views_data')) {
$target_entity_type = $entity_types[$target_entity_type_id];
$target_table = $entity_manager->getHandler($target_entity_type_id, 'views_data')
->getViewsTableForEntityType($target_entity_type);
$t_args = [
'@origin_label' => $entity_type->getLabel(),
'@target_label' => $target_entity_type->getLabel(),
'@field_name' => $field->getLabel() ?: $field_name,
'@type' => 'base field',
];
// Relationship (Origin -> Target).
$psuedo_field = $target_entity_type_id . '__' . $field_name;
$data[$table][$psuedo_field]['relationship'] = [
'title' => t('@field_name to @target_label entities', $t_args),
'label' => t('@field_name: @target_label', $t_args),
'group' => $entity_type->getLabel(),
'help' => t('References to @target_label entities referenced by @field_name @type on @origin_label entities.', $t_args),
'id' => 'standard',
'base' => $target_table,
'entity type' => $target_entity_type_id,
'base field' => $target_entity_type->getKey('id'),
'relationship field' => $column_id,
'extra' => [
[
// Entity reference field only has one target type whereas
// dynamic entity reference field can have multiple target types
// that is why extra join condition on target types is needed.
'left_field' => $column_type,
'value' => $target_entity_type_id,
],
],
];
// Reverse Relationship (Target -> Origin).
$psuedo_field = 'reverse__' . $entity_type_id . '__' . $field_name;
$data[$target_table][$psuedo_field]['relationship'] = [
'title' => t('Reverse reference to @field_name @type on @origin_label', $t_args),
'label' => t('Reverse reference to @field_name @type on @origin_label', $t_args),
'group' => $target_entity_type->getLabel(),
'help' => t('Reverse reference from @target_label entities referenced by @field_name @type on @origin_label entities.', $t_args),
];
// When base field cardinality is 1 then the 'base' and 'field table'
// are same because field column(s) exist in entity base table
// therefore we can't use entity_reverse relationship plugin.
if ($field->getCardinality() == 1) {
$data[$target_table][$psuedo_field]['relationship'] += [
'id' => 'standard',
'base' => $table,
'entity type' => $entity_type_id,
'base field' => $column_id,
'field' => $target_entity_type->getKey('id'),
'extra' => [
[
// Entity reference field only has one target type whereas
// dynamic entity reference field can have multiple target
// types that is why an extra join condition on target types
// is needed.
'field' => $column_type,
'value' => $target_entity_type_id,
],
],
];
}
// When base field cardinality is greater then 1 the 'base' and
// 'field table' are not same because field column(s) exist in
// separate table therefore we have to use entity_reverse relationship
// plugin.
else {
$data[$target_table][$psuedo_field]['relationship'] += [
'id' => 'entity_reverse',
'base' => $base_table,
'entity_type' => $entity_type_id,
'base field' => $entity_type->getKey('id'),
'field_name' => $field_name,
'field table' => $table,
'field field' => $column_id,
// Entity reference field only has one target type whereas dynamic
// entity reference field can have multiple target types that is
// why an extra join condition on target types is needed.
'join_extra' => [
[
'field' => $column_type,
'value' => $target_entity_type_id,
],
],
];
}
}
}
}
}
return $data;
}