annotate core/modules/views/src/EntityViewsData.php @ 19:fa3358dc1485 tip

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