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