EntityViewsData.php 26 KB

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