annotate core/modules/views/src/EntityViewsData.php @ 0:c75dbcec494b

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