Mercurial > hg > isophonics-drupal-site
comparison core/modules/views/src/EntityViewsData.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views; | |
4 | |
5 use Drupal\Core\Entity\ContentEntityType; | |
6 use Drupal\Core\Entity\EntityHandlerInterface; | |
7 use Drupal\Core\Entity\EntityManagerInterface; | |
8 use Drupal\Core\Entity\EntityTypeInterface; | |
9 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; | |
10 use Drupal\Core\Entity\Sql\TableMappingInterface; | |
11 use Drupal\Core\Extension\ModuleHandlerInterface; | |
12 use Drupal\Core\Field\FieldDefinitionInterface; | |
13 use Drupal\Core\StringTranslation\StringTranslationTrait; | |
14 use Drupal\Core\StringTranslation\TranslationInterface; | |
15 use Symfony\Component\DependencyInjection\Container; | |
16 use Symfony\Component\DependencyInjection\ContainerInterface; | |
17 | |
18 /** | |
19 * Provides generic views integration for entities. | |
20 */ | |
21 class EntityViewsData implements EntityHandlerInterface, EntityViewsDataInterface { | |
22 | |
23 use StringTranslationTrait; | |
24 | |
25 /** | |
26 * Entity type for this views data handler instance. | |
27 * | |
28 * @var \Drupal\Core\Entity\EntityTypeInterface | |
29 */ | |
30 protected $entityType; | |
31 | |
32 /** | |
33 * The storage used for this entity type. | |
34 * | |
35 * @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface | |
36 */ | |
37 protected $storage; | |
38 | |
39 /** | |
40 * The module handler. | |
41 * | |
42 * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
43 */ | |
44 protected $moduleHandler; | |
45 | |
46 /** | |
47 * The translation manager. | |
48 * | |
49 * @var \Drupal\Core\StringTranslation\TranslationInterface | |
50 */ | |
51 protected $translationManager; | |
52 | |
53 /** | |
54 * The field storage definitions for all base fields of the entity type. | |
55 * | |
56 * @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] | |
57 */ | |
58 protected $fieldStorageDefinitions; | |
59 | |
60 /** | |
61 * The entity manager. | |
62 * | |
63 * @var \Drupal\Core\Entity\EntityManagerInterface | |
64 */ | |
65 protected $entityManager; | |
66 | |
67 /** | |
68 * Constructs an EntityViewsData object. | |
69 * | |
70 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type | |
71 * The entity type to provide views integration for. | |
72 * @param \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $storage_controller | |
73 * The storage handler used for this entity type. | |
74 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | |
75 * The entity manager. | |
76 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
77 * The module handler. | |
78 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager | |
79 * The translation manager. | |
80 */ | |
81 public function __construct(EntityTypeInterface $entity_type, SqlEntityStorageInterface $storage_controller, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) { | |
82 $this->entityType = $entity_type; | |
83 $this->entityManager = $entity_manager; | |
84 $this->storage = $storage_controller; | |
85 $this->moduleHandler = $module_handler; | |
86 $this->setStringTranslation($translation_manager); | |
87 } | |
88 | |
89 /** | |
90 * {@inheritdoc} | |
91 */ | |
92 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { | |
93 return new static( | |
94 $entity_type, | |
95 $container->get('entity.manager')->getStorage($entity_type->id()), | |
96 $container->get('entity.manager'), | |
97 $container->get('module_handler'), | |
98 $container->get('string_translation'), | |
99 $container->get('typed_data_manager') | |
100 ); | |
101 } | |
102 | |
103 /** | |
104 * Gets the field storage definitions. | |
105 * | |
106 * @return \Drupal\Core\Field\FieldStorageDefinitionInterface[] | |
107 */ | |
108 protected function getFieldStorageDefinitions() { | |
109 if (!isset($this->fieldStorageDefinitions)) { | |
110 $this->fieldStorageDefinitions = $this->entityManager->getFieldStorageDefinitions($this->entityType->id()); | |
111 } | |
112 return $this->fieldStorageDefinitions; | |
113 } | |
114 | |
115 /** | |
116 * {@inheritdoc} | |
117 */ | |
118 public function getViewsData() { | |
119 $data = []; | |
120 | |
121 $base_table = $this->entityType->getBaseTable() ?: $this->entityType->id(); | |
122 $views_revision_base_table = NULL; | |
123 $revisionable = $this->entityType->isRevisionable(); | |
124 $base_field = $this->entityType->getKey('id'); | |
125 | |
126 $revision_table = ''; | |
127 if ($revisionable) { | |
128 $revision_table = $this->entityType->getRevisionTable() ?: $this->entityType->id() . '_revision'; | |
129 } | |
130 | |
131 $translatable = $this->entityType->isTranslatable(); | |
132 $data_table = ''; | |
133 if ($translatable) { | |
134 $data_table = $this->entityType->getDataTable() ?: $this->entityType->id() . '_field_data'; | |
135 } | |
136 | |
137 // Some entity types do not have a revision data table defined, but still | |
138 // have a revision table name set in | |
139 // \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout() so we | |
140 // apply the same kind of logic. | |
141 $revision_data_table = ''; | |
142 if ($revisionable && $translatable) { | |
143 $revision_data_table = $this->entityType->getRevisionDataTable() ?: $this->entityType->id() . '_field_revision'; | |
144 } | |
145 $revision_field = $this->entityType->getKey('revision'); | |
146 | |
147 // Setup base information of the views data. | |
148 $data[$base_table]['table']['group'] = $this->entityType->getLabel(); | |
149 $data[$base_table]['table']['provider'] = $this->entityType->getProvider(); | |
150 | |
151 $views_base_table = $base_table; | |
152 if ($data_table) { | |
153 $views_base_table = $data_table; | |
154 } | |
155 $data[$views_base_table]['table']['base'] = [ | |
156 'field' => $base_field, | |
157 'title' => $this->entityType->getLabel(), | |
158 'cache_contexts' => $this->entityType->getListCacheContexts(), | |
159 ]; | |
160 $data[$base_table]['table']['entity revision'] = FALSE; | |
161 | |
162 if ($label_key = $this->entityType->getKey('label')) { | |
163 if ($data_table) { | |
164 $data[$views_base_table]['table']['base']['defaults'] = [ | |
165 'field' => $label_key, | |
166 'table' => $data_table, | |
167 ]; | |
168 } | |
169 else { | |
170 $data[$views_base_table]['table']['base']['defaults'] = [ | |
171 'field' => $label_key, | |
172 ]; | |
173 } | |
174 } | |
175 | |
176 // Entity types must implement a list_builder in order to use Views' | |
177 // entity operations field. | |
178 if ($this->entityType->hasListBuilderClass()) { | |
179 $data[$base_table]['operations'] = [ | |
180 'field' => [ | |
181 'title' => $this->t('Operations links'), | |
182 'help' => $this->t('Provides links to perform entity operations.'), | |
183 'id' => 'entity_operations', | |
184 ], | |
185 ]; | |
186 } | |
187 | |
188 if ($this->entityType->hasViewBuilderClass()) { | |
189 $data[$base_table]['rendered_entity'] = [ | |
190 'field' => [ | |
191 'title' => $this->t('Rendered entity'), | |
192 'help' => $this->t('Renders an entity in a view mode.'), | |
193 'id' => 'rendered_entity', | |
194 ], | |
195 ]; | |
196 } | |
197 | |
198 // Setup relations to the revisions/property data. | |
199 if ($data_table) { | |
200 $data[$base_table]['table']['join'][$data_table] = [ | |
201 'left_field' => $base_field, | |
202 'field' => $base_field, | |
203 'type' => 'INNER' | |
204 ]; | |
205 $data[$data_table]['table']['group'] = $this->entityType->getLabel(); | |
206 $data[$data_table]['table']['provider'] = $this->entityType->getProvider(); | |
207 $data[$data_table]['table']['entity revision'] = FALSE; | |
208 } | |
209 if ($revision_table) { | |
210 $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]); | |
211 $data[$revision_table]['table']['provider'] = $this->entityType->getProvider(); | |
212 | |
213 $views_revision_base_table = $revision_table; | |
214 if ($revision_data_table) { | |
215 $views_revision_base_table = $revision_data_table; | |
216 } | |
217 $data[$views_revision_base_table]['table']['entity revision'] = TRUE; | |
218 $data[$views_revision_base_table]['table']['base'] = [ | |
219 'field' => $revision_field, | |
220 'title' => $this->t('@entity_type revisions', ['@entity_type' => $this->entityType->getLabel()]), | |
221 ]; | |
222 // Join the revision table to the base table. | |
223 $data[$views_revision_base_table]['table']['join'][$views_base_table] = [ | |
224 'left_field' => $revision_field, | |
225 'field' => $revision_field, | |
226 'type' => 'INNER', | |
227 ]; | |
228 | |
229 if ($revision_data_table) { | |
230 $data[$revision_data_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]); | |
231 $data[$revision_data_table]['table']['entity revision'] = TRUE; | |
232 | |
233 $data[$revision_table]['table']['join'][$revision_data_table] = [ | |
234 'left_field' => $revision_field, | |
235 'field' => $revision_field, | |
236 'type' => 'INNER', | |
237 ]; | |
238 } | |
239 | |
240 // Add a filter for showing only the latest revisions of an entity. | |
241 $data[$revision_table]['latest_revision'] = [ | |
242 'title' => $this->t('Is Latest Revision'), | |
243 'help' => $this->t('Restrict the view to only revisions that are the latest revision of their entity.'), | |
244 'filter' => ['id' => 'latest_revision'], | |
245 ]; | |
246 } | |
247 | |
248 $this->addEntityLinks($data[$base_table]); | |
249 | |
250 // Load all typed data definitions of all fields. This should cover each of | |
251 // the entity base, revision, data tables. | |
252 $field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id()); | |
253 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ | |
254 if ($table_mapping = $this->storage->getTableMapping($field_definitions)) { | |
255 // Fetch all fields that can appear in both the base table and the data | |
256 // table. | |
257 $entity_keys = $this->entityType->getKeys(); | |
258 $duplicate_fields = array_intersect_key($entity_keys, array_flip(['id', 'revision', 'bundle'])); | |
259 // Iterate over each table we have so far and collect field data for each. | |
260 // Based on whether the field is in the field_definitions provided by the | |
261 // entity manager. | |
262 // @todo We should better just rely on information coming from the entity | |
263 // storage. | |
264 // @todo https://www.drupal.org/node/2337511 | |
265 foreach ($table_mapping->getTableNames() as $table) { | |
266 foreach ($table_mapping->getFieldNames($table) as $field_name) { | |
267 // To avoid confusing duplication in the user interface, for fields | |
268 // that are on both base and data tables, only add them on the data | |
269 // table (same for revision vs. revision data). | |
270 if ($data_table && ($table === $base_table || $table === $revision_table) && in_array($field_name, $duplicate_fields)) { | |
271 continue; | |
272 } | |
273 $this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]); | |
274 } | |
275 } | |
276 | |
277 foreach ($field_definitions as $field_definition) { | |
278 if ($table_mapping->requiresDedicatedTableStorage($field_definition->getFieldStorageDefinition())) { | |
279 $table = $table_mapping->getDedicatedDataTableName($field_definition->getFieldStorageDefinition()); | |
280 | |
281 $data[$table]['table']['group'] = $this->entityType->getLabel(); | |
282 $data[$table]['table']['provider'] = $this->entityType->getProvider(); | |
283 $data[$table]['table']['join'][$views_base_table] = [ | |
284 'left_field' => $base_field, | |
285 'field' => 'entity_id', | |
286 'extra' => [ | |
287 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], | |
288 ], | |
289 ]; | |
290 | |
291 if ($revisionable) { | |
292 $revision_table = $table_mapping->getDedicatedRevisionTableName($field_definition->getFieldStorageDefinition()); | |
293 | |
294 $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]); | |
295 $data[$revision_table]['table']['provider'] = $this->entityType->getProvider(); | |
296 $data[$revision_table]['table']['join'][$views_revision_base_table] = [ | |
297 'left_field' => $revision_field, | |
298 'field' => 'entity_id', | |
299 'extra' => [ | |
300 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], | |
301 ], | |
302 ]; | |
303 } | |
304 } | |
305 } | |
306 } | |
307 | |
308 // Add the entity type key to each table generated. | |
309 $entity_type_id = $this->entityType->id(); | |
310 array_walk($data, function (&$table_data) use ($entity_type_id) { | |
311 $table_data['table']['entity type'] = $entity_type_id; | |
312 }); | |
313 | |
314 return $data; | |
315 } | |
316 | |
317 /** | |
318 * Sets the entity links in case corresponding link templates exist. | |
319 * | |
320 * @param array $data | |
321 * The views data of the base table. | |
322 */ | |
323 protected function addEntityLinks(array &$data) { | |
324 $entity_type_id = $this->entityType->id(); | |
325 $t_arguments = ['@entity_type_label' => $this->entityType->getLabel()]; | |
326 if ($this->entityType->hasLinkTemplate('canonical')) { | |
327 $data['view_' . $entity_type_id] = [ | |
328 'field' => [ | |
329 'title' => $this->t('Link to @entity_type_label', $t_arguments), | |
330 'help' => $this->t('Provide a view link to the @entity_type_label.', $t_arguments), | |
331 'id' => 'entity_link', | |
332 ], | |
333 ]; | |
334 } | |
335 if ($this->entityType->hasLinkTemplate('edit-form')) { | |
336 $data['edit_' . $entity_type_id] = [ | |
337 'field' => [ | |
338 'title' => $this->t('Link to edit @entity_type_label', $t_arguments), | |
339 'help' => $this->t('Provide an edit link to the @entity_type_label.', $t_arguments), | |
340 'id' => 'entity_link_edit', | |
341 ], | |
342 ]; | |
343 } | |
344 if ($this->entityType->hasLinkTemplate('delete-form')) { | |
345 $data['delete_' . $entity_type_id] = [ | |
346 'field' => [ | |
347 'title' => $this->t('Link to delete @entity_type_label', $t_arguments), | |
348 'help' => $this->t('Provide a delete link to the @entity_type_label.', $t_arguments), | |
349 'id' => 'entity_link_delete', | |
350 ], | |
351 ]; | |
352 } | |
353 } | |
354 | |
355 /** | |
356 * Puts the views data for a single field onto the views data. | |
357 * | |
358 * @param string $table | |
359 * The table of the field to handle. | |
360 * @param string $field_name | |
361 * The name of the field to handle. | |
362 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
363 * The field definition defined in Entity::baseFieldDefinitions() | |
364 * @param \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping | |
365 * The table mapping information | |
366 * @param array $table_data | |
367 * A reference to a specific entity table (for example data_table) inside | |
368 * the views data. | |
369 */ | |
370 protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterface $field_definition, TableMappingInterface $table_mapping, &$table_data) { | |
371 // Create a dummy instance to retrieve property definitions. | |
372 $field_column_mapping = $table_mapping->getColumnNames($field_name); | |
373 $field_schema = $this->getFieldStorageDefinitions()[$field_name]->getSchema(); | |
374 | |
375 $field_definition_type = $field_definition->getType(); | |
376 // Add all properties to views table data. We need an entry for each | |
377 // column of each field, with the first one given special treatment. | |
378 // @todo Introduce concept of the "main" column for a field, rather than | |
379 // assuming the first one is the main column. See also what the | |
380 // mapSingleFieldViewsData() method does with $first. | |
381 $multiple = (count($field_column_mapping) > 1); | |
382 $first = TRUE; | |
383 foreach ($field_column_mapping as $field_column_name => $schema_field_name) { | |
384 $views_field_name = ($multiple) ? $field_name . '__' . $field_column_name : $field_name; | |
385 $table_data[$views_field_name] = $this->mapSingleFieldViewsData($table, $field_name, $field_definition_type, $field_column_name, $field_schema['columns'][$field_column_name]['type'], $first, $field_definition); | |
386 | |
387 $table_data[$views_field_name]['entity field'] = $field_name; | |
388 $first = FALSE; | |
389 } | |
390 } | |
391 | |
392 /** | |
393 * Provides the views data for a given data type and schema field. | |
394 * | |
395 * @param string $table | |
396 * The table of the field to handle. | |
397 * @param string $field_name | |
398 * The machine name of the field being processed. | |
399 * @param string $field_type | |
400 * The type of field being handled. | |
401 * @param string $column_name | |
402 * For fields containing multiple columns, the column name being processed. | |
403 * @param string $column_type | |
404 * Within the field, the column type being handled. | |
405 * @param bool $first | |
406 * TRUE if this is the first column within the field. | |
407 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
408 * The field definition. | |
409 * | |
410 * @return array | |
411 * The modified views data field definition. | |
412 */ | |
413 protected function mapSingleFieldViewsData($table, $field_name, $field_type, $column_name, $column_type, $first, FieldDefinitionInterface $field_definition) { | |
414 $views_field = []; | |
415 | |
416 // Provide a nicer, less verbose label for the first column within a field. | |
417 // @todo Introduce concept of the "main" column for a field, rather than | |
418 // assuming the first one is the main column. | |
419 if ($first) { | |
420 $views_field['title'] = $field_definition->getLabel(); | |
421 } | |
422 else { | |
423 $views_field['title'] = $field_definition->getLabel() . " ($column_name)"; | |
424 } | |
425 | |
426 if ($description = $field_definition->getDescription()) { | |
427 $views_field['help'] = $description; | |
428 } | |
429 | |
430 // Set up the field, sort, argument, and filters, based on | |
431 // the column and/or field data type. | |
432 // @todo Allow field types to customize this. | |
433 // @see https://www.drupal.org/node/2337515 | |
434 switch ($field_type) { | |
435 // Special case a few field types. | |
436 case 'timestamp': | |
437 case 'created': | |
438 case 'changed': | |
439 $views_field['field']['id'] = 'field'; | |
440 $views_field['argument']['id'] = 'date'; | |
441 $views_field['filter']['id'] = 'date'; | |
442 $views_field['sort']['id'] = 'date'; | |
443 break; | |
444 | |
445 case 'language': | |
446 $views_field['field']['id'] = 'field'; | |
447 $views_field['argument']['id'] = 'language'; | |
448 $views_field['filter']['id'] = 'language'; | |
449 $views_field['sort']['id'] = 'standard'; | |
450 break; | |
451 | |
452 case 'boolean': | |
453 $views_field['field']['id'] = 'field'; | |
454 $views_field['argument']['id'] = 'numeric'; | |
455 $views_field['filter']['id'] = 'boolean'; | |
456 $views_field['sort']['id'] = 'standard'; | |
457 break; | |
458 | |
459 case 'uri': | |
460 // Let's render URIs as URIs by default, not links. | |
461 $views_field['field']['id'] = 'field'; | |
462 $views_field['field']['default_formatter'] = 'string'; | |
463 | |
464 $views_field['argument']['id'] = 'string'; | |
465 $views_field['filter']['id'] = 'string'; | |
466 $views_field['sort']['id'] = 'standard'; | |
467 break; | |
468 | |
469 case 'text': | |
470 case 'text_with_summary': | |
471 // Treat these three long text fields the same. | |
472 $field_type = 'text_long'; | |
473 // Intentional fall-through here to the default processing! | |
474 | |
475 default: | |
476 // For most fields, the field type is generic enough to just use | |
477 // the column type to determine the filters etc. | |
478 switch ($column_type) { | |
479 | |
480 case 'int': | |
481 case 'integer': | |
482 case 'smallint': | |
483 case 'tinyint': | |
484 case 'mediumint': | |
485 case 'float': | |
486 case 'double': | |
487 case 'decimal': | |
488 $views_field['field']['id'] = 'field'; | |
489 $views_field['argument']['id'] = 'numeric'; | |
490 $views_field['filter']['id'] = 'numeric'; | |
491 $views_field['sort']['id'] = 'standard'; | |
492 break; | |
493 | |
494 case 'char': | |
495 case 'string': | |
496 case 'varchar': | |
497 case 'varchar_ascii': | |
498 case 'tinytext': | |
499 case 'text': | |
500 case 'mediumtext': | |
501 case 'longtext': | |
502 $views_field['field']['id'] = 'field'; | |
503 $views_field['argument']['id'] = 'string'; | |
504 $views_field['filter']['id'] = 'string'; | |
505 $views_field['sort']['id'] = 'standard'; | |
506 break; | |
507 | |
508 default: | |
509 $views_field['field']['id'] = 'field'; | |
510 $views_field['argument']['id'] = 'standard'; | |
511 $views_field['filter']['id'] = 'standard'; | |
512 $views_field['sort']['id'] = 'standard'; | |
513 } | |
514 } | |
515 | |
516 // Do post-processing for a few field types. | |
517 | |
518 $process_method = 'processViewsDataFor' . Container::camelize($field_type); | |
519 if (method_exists($this, $process_method)) { | |
520 $this->{$process_method}($table, $field_definition, $views_field, $column_name); | |
521 } | |
522 | |
523 return $views_field; | |
524 } | |
525 | |
526 /** | |
527 * Processes the views data for a language field. | |
528 * | |
529 * @param string $table | |
530 * The table the language field is added to. | |
531 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
532 * The field definition. | |
533 * @param array $views_field | |
534 * The views field data. | |
535 * @param string $field_column_name | |
536 * The field column being processed. | |
537 */ | |
538 protected function processViewsDataForLanguage($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) { | |
539 // Apply special titles for the langcode field. | |
540 if ($field_definition->getName() == $this->entityType->getKey('langcode')) { | |
541 if ($table == $this->entityType->getDataTable() || $table == $this->entityType->getRevisionDataTable()) { | |
542 $views_field['title'] = $this->t('Translation language'); | |
543 } | |
544 if ($table == $this->entityType->getBaseTable() || $table == $this->entityType->getRevisionTable()) { | |
545 $views_field['title'] = $this->t('Original language'); | |
546 } | |
547 } | |
548 } | |
549 | |
550 /** | |
551 * Processes the views data for an entity reference field. | |
552 * | |
553 * @param string $table | |
554 * The table the language field is added to. | |
555 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
556 * The field definition. | |
557 * @param array $views_field | |
558 * The views field data. | |
559 * @param string $field_column_name | |
560 * The field column being processed. | |
561 */ | |
562 protected function processViewsDataForEntityReference($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) { | |
563 | |
564 // @todo Should the actual field handler respect that this just renders a | |
565 // number? | |
566 // @todo Create an optional entity field handler, that can render the | |
567 // entity. | |
568 // @see https://www.drupal.org/node/2322949 | |
569 | |
570 if ($entity_type_id = $field_definition->getItemDefinition()->getSetting('target_type')) { | |
571 $entity_type = $this->entityManager->getDefinition($entity_type_id); | |
572 if ($entity_type instanceof ContentEntityType) { | |
573 $views_field['relationship'] = [ | |
574 'base' => $this->getViewsTableForEntityType($entity_type), | |
575 'base field' => $entity_type->getKey('id'), | |
576 'label' => $entity_type->getLabel(), | |
577 'title' => $entity_type->getLabel(), | |
578 'id' => 'standard', | |
579 ]; | |
580 $views_field['field']['id'] = 'field'; | |
581 $views_field['argument']['id'] = 'numeric'; | |
582 $views_field['filter']['id'] = 'numeric'; | |
583 $views_field['sort']['id'] = 'standard'; | |
584 } | |
585 else { | |
586 $views_field['field']['id'] = 'field'; | |
587 $views_field['argument']['id'] = 'string'; | |
588 $views_field['filter']['id'] = 'string'; | |
589 $views_field['sort']['id'] = 'standard'; | |
590 } | |
591 } | |
592 | |
593 if ($field_definition->getName() == $this->entityType->getKey('bundle')) { | |
594 $views_field['filter']['id'] = 'bundle'; | |
595 } | |
596 } | |
597 | |
598 /** | |
599 * Processes the views data for a text field with formatting. | |
600 * | |
601 * @param string $table | |
602 * The table the field is added to. | |
603 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
604 * The field definition. | |
605 * @param array $views_field | |
606 * The views field data. | |
607 * @param string $field_column_name | |
608 * The field column being processed. | |
609 */ | |
610 protected function processViewsDataForTextLong($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) { | |
611 // Connect the text field to its formatter. | |
612 if ($field_column_name == 'value') { | |
613 $views_field['field']['format'] = $field_definition->getName() . '__format'; | |
614 $views_field['field']['id'] = 'field'; | |
615 } | |
616 } | |
617 | |
618 /** | |
619 * Processes the views data for a UUID field. | |
620 * | |
621 * @param string $table | |
622 * The table the field is added to. | |
623 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition | |
624 * The field definition. | |
625 * @param array $views_field | |
626 * The views field data. | |
627 * @param string $field_column_name | |
628 * The field column being processed. | |
629 */ | |
630 protected function processViewsDataForUuid($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) { | |
631 // It does not make sense for UUID fields to be click sortable. | |
632 $views_field['field']['click sortable'] = FALSE; | |
633 } | |
634 | |
635 /** | |
636 * {@inheritdoc} | |
637 */ | |
638 public function getViewsTableForEntityType(EntityTypeInterface $entity_type) { | |
639 return $entity_type->getDataTable() ?: $entity_type->getBaseTable(); | |
640 } | |
641 | |
642 } |