annotate core/modules/views/src/EntityViewsData.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
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@0 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@0 253 }
Chris@0 254
Chris@0 255 $this->addEntityLinks($data[$base_table]);
Chris@0 256
Chris@0 257 // Load all typed data definitions of all fields. This should cover each of
Chris@0 258 // the entity base, revision, data tables.
Chris@0 259 $field_definitions = $this->entityManager->getBaseFieldDefinitions($this->entityType->id());
Chris@0 260 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
Chris@0 261 if ($table_mapping = $this->storage->getTableMapping($field_definitions)) {
Chris@0 262 // Fetch all fields that can appear in both the base table and the data
Chris@0 263 // table.
Chris@0 264 $entity_keys = $this->entityType->getKeys();
Chris@0 265 $duplicate_fields = array_intersect_key($entity_keys, array_flip(['id', 'revision', 'bundle']));
Chris@0 266 // Iterate over each table we have so far and collect field data for each.
Chris@0 267 // Based on whether the field is in the field_definitions provided by the
Chris@0 268 // entity manager.
Chris@0 269 // @todo We should better just rely on information coming from the entity
Chris@0 270 // storage.
Chris@0 271 // @todo https://www.drupal.org/node/2337511
Chris@0 272 foreach ($table_mapping->getTableNames() as $table) {
Chris@0 273 foreach ($table_mapping->getFieldNames($table) as $field_name) {
Chris@0 274 // To avoid confusing duplication in the user interface, for fields
Chris@0 275 // that are on both base and data tables, only add them on the data
Chris@0 276 // table (same for revision vs. revision data).
Chris@0 277 if ($data_table && ($table === $base_table || $table === $revision_table) && in_array($field_name, $duplicate_fields)) {
Chris@0 278 continue;
Chris@0 279 }
Chris@0 280 $this->mapFieldDefinition($table, $field_name, $field_definitions[$field_name], $table_mapping, $data[$table]);
Chris@0 281 }
Chris@0 282 }
Chris@0 283
Chris@0 284 foreach ($field_definitions as $field_definition) {
Chris@0 285 if ($table_mapping->requiresDedicatedTableStorage($field_definition->getFieldStorageDefinition())) {
Chris@0 286 $table = $table_mapping->getDedicatedDataTableName($field_definition->getFieldStorageDefinition());
Chris@0 287
Chris@0 288 $data[$table]['table']['group'] = $this->entityType->getLabel();
Chris@0 289 $data[$table]['table']['provider'] = $this->entityType->getProvider();
Chris@0 290 $data[$table]['table']['join'][$views_base_table] = [
Chris@0 291 'left_field' => $base_field,
Chris@0 292 'field' => 'entity_id',
Chris@0 293 'extra' => [
Chris@0 294 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
Chris@0 295 ],
Chris@0 296 ];
Chris@0 297
Chris@0 298 if ($revisionable) {
Chris@0 299 $revision_table = $table_mapping->getDedicatedRevisionTableName($field_definition->getFieldStorageDefinition());
Chris@0 300
Chris@0 301 $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
Chris@0 302 $data[$revision_table]['table']['provider'] = $this->entityType->getProvider();
Chris@0 303 $data[$revision_table]['table']['join'][$views_revision_base_table] = [
Chris@0 304 'left_field' => $revision_field,
Chris@0 305 'field' => 'entity_id',
Chris@0 306 'extra' => [
Chris@0 307 ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
Chris@0 308 ],
Chris@0 309 ];
Chris@0 310 }
Chris@0 311 }
Chris@0 312 }
Chris@0 313 }
Chris@0 314
Chris@0 315 // Add the entity type key to each table generated.
Chris@0 316 $entity_type_id = $this->entityType->id();
Chris@0 317 array_walk($data, function (&$table_data) use ($entity_type_id) {
Chris@0 318 $table_data['table']['entity type'] = $entity_type_id;
Chris@0 319 });
Chris@0 320
Chris@0 321 return $data;
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * Sets the entity links in case corresponding link templates exist.
Chris@0 326 *
Chris@0 327 * @param array $data
Chris@0 328 * The views data of the base table.
Chris@0 329 */
Chris@0 330 protected function addEntityLinks(array &$data) {
Chris@0 331 $entity_type_id = $this->entityType->id();
Chris@0 332 $t_arguments = ['@entity_type_label' => $this->entityType->getLabel()];
Chris@0 333 if ($this->entityType->hasLinkTemplate('canonical')) {
Chris@0 334 $data['view_' . $entity_type_id] = [
Chris@0 335 'field' => [
Chris@0 336 'title' => $this->t('Link to @entity_type_label', $t_arguments),
Chris@0 337 'help' => $this->t('Provide a view link to the @entity_type_label.', $t_arguments),
Chris@0 338 'id' => 'entity_link',
Chris@0 339 ],
Chris@0 340 ];
Chris@0 341 }
Chris@0 342 if ($this->entityType->hasLinkTemplate('edit-form')) {
Chris@0 343 $data['edit_' . $entity_type_id] = [
Chris@0 344 'field' => [
Chris@0 345 'title' => $this->t('Link to edit @entity_type_label', $t_arguments),
Chris@0 346 'help' => $this->t('Provide an edit link to the @entity_type_label.', $t_arguments),
Chris@0 347 'id' => 'entity_link_edit',
Chris@0 348 ],
Chris@0 349 ];
Chris@0 350 }
Chris@0 351 if ($this->entityType->hasLinkTemplate('delete-form')) {
Chris@0 352 $data['delete_' . $entity_type_id] = [
Chris@0 353 'field' => [
Chris@0 354 'title' => $this->t('Link to delete @entity_type_label', $t_arguments),
Chris@0 355 'help' => $this->t('Provide a delete link to the @entity_type_label.', $t_arguments),
Chris@0 356 'id' => 'entity_link_delete',
Chris@0 357 ],
Chris@0 358 ];
Chris@0 359 }
Chris@0 360 }
Chris@0 361
Chris@0 362 /**
Chris@0 363 * Puts the views data for a single field onto the views data.
Chris@0 364 *
Chris@0 365 * @param string $table
Chris@0 366 * The table of the field to handle.
Chris@0 367 * @param string $field_name
Chris@0 368 * The name of the field to handle.
Chris@0 369 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 370 * The field definition defined in Entity::baseFieldDefinitions()
Chris@0 371 * @param \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping
Chris@0 372 * The table mapping information
Chris@0 373 * @param array $table_data
Chris@0 374 * A reference to a specific entity table (for example data_table) inside
Chris@0 375 * the views data.
Chris@0 376 */
Chris@0 377 protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterface $field_definition, TableMappingInterface $table_mapping, &$table_data) {
Chris@0 378 // Create a dummy instance to retrieve property definitions.
Chris@0 379 $field_column_mapping = $table_mapping->getColumnNames($field_name);
Chris@0 380 $field_schema = $this->getFieldStorageDefinitions()[$field_name]->getSchema();
Chris@0 381
Chris@0 382 $field_definition_type = $field_definition->getType();
Chris@0 383 // Add all properties to views table data. We need an entry for each
Chris@0 384 // column of each field, with the first one given special treatment.
Chris@0 385 // @todo Introduce concept of the "main" column for a field, rather than
Chris@0 386 // assuming the first one is the main column. See also what the
Chris@0 387 // mapSingleFieldViewsData() method does with $first.
Chris@0 388 $first = TRUE;
Chris@0 389 foreach ($field_column_mapping as $field_column_name => $schema_field_name) {
Chris@14 390 $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 391 $table_data[$schema_field_name]['entity field'] = $field_name;
Chris@0 392 $first = FALSE;
Chris@0 393 }
Chris@0 394 }
Chris@0 395
Chris@0 396 /**
Chris@0 397 * Provides the views data for a given data type and schema field.
Chris@0 398 *
Chris@0 399 * @param string $table
Chris@0 400 * The table of the field to handle.
Chris@0 401 * @param string $field_name
Chris@0 402 * The machine name of the field being processed.
Chris@0 403 * @param string $field_type
Chris@0 404 * The type of field being handled.
Chris@0 405 * @param string $column_name
Chris@0 406 * For fields containing multiple columns, the column name being processed.
Chris@0 407 * @param string $column_type
Chris@0 408 * Within the field, the column type being handled.
Chris@0 409 * @param bool $first
Chris@0 410 * TRUE if this is the first column within the field.
Chris@0 411 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 412 * The field definition.
Chris@0 413 *
Chris@0 414 * @return array
Chris@0 415 * The modified views data field definition.
Chris@0 416 */
Chris@0 417 protected function mapSingleFieldViewsData($table, $field_name, $field_type, $column_name, $column_type, $first, FieldDefinitionInterface $field_definition) {
Chris@0 418 $views_field = [];
Chris@0 419
Chris@0 420 // Provide a nicer, less verbose label for the first column within a field.
Chris@0 421 // @todo Introduce concept of the "main" column for a field, rather than
Chris@0 422 // assuming the first one is the main column.
Chris@0 423 if ($first) {
Chris@0 424 $views_field['title'] = $field_definition->getLabel();
Chris@0 425 }
Chris@0 426 else {
Chris@0 427 $views_field['title'] = $field_definition->getLabel() . " ($column_name)";
Chris@0 428 }
Chris@0 429
Chris@0 430 if ($description = $field_definition->getDescription()) {
Chris@0 431 $views_field['help'] = $description;
Chris@0 432 }
Chris@0 433
Chris@0 434 // Set up the field, sort, argument, and filters, based on
Chris@0 435 // the column and/or field data type.
Chris@0 436 // @todo Allow field types to customize this.
Chris@0 437 // @see https://www.drupal.org/node/2337515
Chris@0 438 switch ($field_type) {
Chris@0 439 // Special case a few field types.
Chris@0 440 case 'timestamp':
Chris@0 441 case 'created':
Chris@0 442 case 'changed':
Chris@0 443 $views_field['field']['id'] = 'field';
Chris@0 444 $views_field['argument']['id'] = 'date';
Chris@0 445 $views_field['filter']['id'] = 'date';
Chris@0 446 $views_field['sort']['id'] = 'date';
Chris@0 447 break;
Chris@0 448
Chris@0 449 case 'language':
Chris@0 450 $views_field['field']['id'] = 'field';
Chris@0 451 $views_field['argument']['id'] = 'language';
Chris@0 452 $views_field['filter']['id'] = 'language';
Chris@0 453 $views_field['sort']['id'] = 'standard';
Chris@0 454 break;
Chris@0 455
Chris@0 456 case 'boolean':
Chris@0 457 $views_field['field']['id'] = 'field';
Chris@0 458 $views_field['argument']['id'] = 'numeric';
Chris@0 459 $views_field['filter']['id'] = 'boolean';
Chris@0 460 $views_field['sort']['id'] = 'standard';
Chris@0 461 break;
Chris@0 462
Chris@0 463 case 'uri':
Chris@0 464 // Let's render URIs as URIs by default, not links.
Chris@0 465 $views_field['field']['id'] = 'field';
Chris@0 466 $views_field['field']['default_formatter'] = 'string';
Chris@0 467
Chris@0 468 $views_field['argument']['id'] = 'string';
Chris@0 469 $views_field['filter']['id'] = 'string';
Chris@0 470 $views_field['sort']['id'] = 'standard';
Chris@0 471 break;
Chris@0 472
Chris@0 473 case 'text':
Chris@0 474 case 'text_with_summary':
Chris@0 475 // Treat these three long text fields the same.
Chris@0 476 $field_type = 'text_long';
Chris@0 477 // Intentional fall-through here to the default processing!
Chris@0 478
Chris@0 479 default:
Chris@0 480 // For most fields, the field type is generic enough to just use
Chris@0 481 // the column type to determine the filters etc.
Chris@0 482 switch ($column_type) {
Chris@0 483
Chris@0 484 case 'int':
Chris@0 485 case 'integer':
Chris@0 486 case 'smallint':
Chris@0 487 case 'tinyint':
Chris@0 488 case 'mediumint':
Chris@0 489 case 'float':
Chris@0 490 case 'double':
Chris@0 491 case 'decimal':
Chris@0 492 $views_field['field']['id'] = 'field';
Chris@0 493 $views_field['argument']['id'] = 'numeric';
Chris@0 494 $views_field['filter']['id'] = 'numeric';
Chris@0 495 $views_field['sort']['id'] = 'standard';
Chris@0 496 break;
Chris@0 497
Chris@0 498 case 'char':
Chris@0 499 case 'string':
Chris@0 500 case 'varchar':
Chris@0 501 case 'varchar_ascii':
Chris@0 502 case 'tinytext':
Chris@0 503 case 'text':
Chris@0 504 case 'mediumtext':
Chris@0 505 case 'longtext':
Chris@0 506 $views_field['field']['id'] = 'field';
Chris@0 507 $views_field['argument']['id'] = 'string';
Chris@0 508 $views_field['filter']['id'] = 'string';
Chris@0 509 $views_field['sort']['id'] = 'standard';
Chris@0 510 break;
Chris@0 511
Chris@0 512 default:
Chris@0 513 $views_field['field']['id'] = 'field';
Chris@0 514 $views_field['argument']['id'] = 'standard';
Chris@0 515 $views_field['filter']['id'] = 'standard';
Chris@0 516 $views_field['sort']['id'] = 'standard';
Chris@0 517 }
Chris@0 518 }
Chris@0 519
Chris@0 520 // Do post-processing for a few field types.
Chris@0 521
Chris@0 522 $process_method = 'processViewsDataFor' . Container::camelize($field_type);
Chris@0 523 if (method_exists($this, $process_method)) {
Chris@0 524 $this->{$process_method}($table, $field_definition, $views_field, $column_name);
Chris@0 525 }
Chris@0 526
Chris@0 527 return $views_field;
Chris@0 528 }
Chris@0 529
Chris@0 530 /**
Chris@0 531 * Processes the views data for a language field.
Chris@0 532 *
Chris@0 533 * @param string $table
Chris@0 534 * The table the language field is added to.
Chris@0 535 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 536 * The field definition.
Chris@0 537 * @param array $views_field
Chris@0 538 * The views field data.
Chris@0 539 * @param string $field_column_name
Chris@0 540 * The field column being processed.
Chris@0 541 */
Chris@0 542 protected function processViewsDataForLanguage($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
Chris@0 543 // Apply special titles for the langcode field.
Chris@0 544 if ($field_definition->getName() == $this->entityType->getKey('langcode')) {
Chris@0 545 if ($table == $this->entityType->getDataTable() || $table == $this->entityType->getRevisionDataTable()) {
Chris@0 546 $views_field['title'] = $this->t('Translation language');
Chris@0 547 }
Chris@0 548 if ($table == $this->entityType->getBaseTable() || $table == $this->entityType->getRevisionTable()) {
Chris@0 549 $views_field['title'] = $this->t('Original language');
Chris@0 550 }
Chris@0 551 }
Chris@0 552 }
Chris@0 553
Chris@0 554 /**
Chris@0 555 * Processes the views data for an entity reference field.
Chris@0 556 *
Chris@0 557 * @param string $table
Chris@0 558 * The table the language field is added to.
Chris@0 559 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 560 * The field definition.
Chris@0 561 * @param array $views_field
Chris@0 562 * The views field data.
Chris@0 563 * @param string $field_column_name
Chris@0 564 * The field column being processed.
Chris@0 565 */
Chris@0 566 protected function processViewsDataForEntityReference($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
Chris@0 567
Chris@0 568 // @todo Should the actual field handler respect that this just renders a
Chris@0 569 // number?
Chris@0 570 // @todo Create an optional entity field handler, that can render the
Chris@0 571 // entity.
Chris@0 572 // @see https://www.drupal.org/node/2322949
Chris@0 573
Chris@0 574 if ($entity_type_id = $field_definition->getItemDefinition()->getSetting('target_type')) {
Chris@0 575 $entity_type = $this->entityManager->getDefinition($entity_type_id);
Chris@0 576 if ($entity_type instanceof ContentEntityType) {
Chris@0 577 $views_field['relationship'] = [
Chris@0 578 'base' => $this->getViewsTableForEntityType($entity_type),
Chris@0 579 'base field' => $entity_type->getKey('id'),
Chris@0 580 'label' => $entity_type->getLabel(),
Chris@0 581 'title' => $entity_type->getLabel(),
Chris@0 582 'id' => 'standard',
Chris@0 583 ];
Chris@0 584 $views_field['field']['id'] = 'field';
Chris@0 585 $views_field['argument']['id'] = 'numeric';
Chris@0 586 $views_field['filter']['id'] = 'numeric';
Chris@0 587 $views_field['sort']['id'] = 'standard';
Chris@0 588 }
Chris@0 589 else {
Chris@0 590 $views_field['field']['id'] = 'field';
Chris@0 591 $views_field['argument']['id'] = 'string';
Chris@0 592 $views_field['filter']['id'] = 'string';
Chris@0 593 $views_field['sort']['id'] = 'standard';
Chris@0 594 }
Chris@0 595 }
Chris@0 596
Chris@0 597 if ($field_definition->getName() == $this->entityType->getKey('bundle')) {
Chris@0 598 $views_field['filter']['id'] = 'bundle';
Chris@0 599 }
Chris@0 600 }
Chris@0 601
Chris@0 602 /**
Chris@0 603 * Processes the views data for a text field with formatting.
Chris@0 604 *
Chris@0 605 * @param string $table
Chris@0 606 * The table the field is added to.
Chris@0 607 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 608 * The field definition.
Chris@0 609 * @param array $views_field
Chris@0 610 * The views field data.
Chris@0 611 * @param string $field_column_name
Chris@0 612 * The field column being processed.
Chris@0 613 */
Chris@0 614 protected function processViewsDataForTextLong($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
Chris@0 615 // Connect the text field to its formatter.
Chris@0 616 if ($field_column_name == 'value') {
Chris@0 617 $views_field['field']['format'] = $field_definition->getName() . '__format';
Chris@0 618 $views_field['field']['id'] = 'field';
Chris@0 619 }
Chris@0 620 }
Chris@0 621
Chris@0 622 /**
Chris@0 623 * Processes the views data for a UUID field.
Chris@0 624 *
Chris@0 625 * @param string $table
Chris@0 626 * The table the field is added to.
Chris@0 627 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
Chris@0 628 * The field definition.
Chris@0 629 * @param array $views_field
Chris@0 630 * The views field data.
Chris@0 631 * @param string $field_column_name
Chris@0 632 * The field column being processed.
Chris@0 633 */
Chris@0 634 protected function processViewsDataForUuid($table, FieldDefinitionInterface $field_definition, array &$views_field, $field_column_name) {
Chris@0 635 // It does not make sense for UUID fields to be click sortable.
Chris@0 636 $views_field['field']['click sortable'] = FALSE;
Chris@0 637 }
Chris@0 638
Chris@0 639 /**
Chris@0 640 * {@inheritdoc}
Chris@0 641 */
Chris@0 642 public function getViewsTableForEntityType(EntityTypeInterface $entity_type) {
Chris@0 643 return $entity_type->getDataTable() ?: $entity_type->getBaseTable();
Chris@0 644 }
Chris@0 645
Chris@0 646 }