views.views.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. /**
  3. * @file
  4. * Provide views data that isn't tied to any other module.
  5. */
  6. use Drupal\Component\Utility\NestedArray;
  7. use Drupal\Core\Entity\EntityStorageInterface;
  8. use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
  9. use Drupal\Core\Render\Markup;
  10. use Drupal\field\Entity\FieldConfig;
  11. use Drupal\field\FieldConfigInterface;
  12. use Drupal\field\FieldStorageConfigInterface;
  13. use Drupal\system\ActionConfigEntityInterface;
  14. /**
  15. * Implements hook_views_data().
  16. */
  17. function views_views_data() {
  18. $data['views']['table']['group'] = t('Global');
  19. $data['views']['table']['join'] = [
  20. // #global is a special flag which allows a table to appear all the time.
  21. '#global' => [],
  22. ];
  23. $data['views']['random'] = [
  24. 'title' => t('Random'),
  25. 'help' => t('Randomize the display order.'),
  26. 'sort' => [
  27. 'id' => 'random',
  28. ],
  29. ];
  30. $data['views']['null'] = [
  31. 'title' => t('Null'),
  32. 'help' => t('Allow a contextual filter value to be ignored. The query will not be altered by this contextual filter value. Can be used when contextual filter values come from the URL, and a part of the URL needs to be ignored.'),
  33. 'argument' => [
  34. 'id' => 'null',
  35. ],
  36. ];
  37. $data['views']['nothing'] = [
  38. 'title' => t('Custom text'),
  39. 'help' => t('Provide custom text or link.'),
  40. 'field' => [
  41. 'id' => 'custom',
  42. ],
  43. ];
  44. $data['views']['counter'] = [
  45. 'title' => t('View result counter'),
  46. 'help' => t('Displays the actual position of the view result'),
  47. 'field' => [
  48. 'id' => 'counter',
  49. ],
  50. ];
  51. $data['views']['area'] = [
  52. 'title' => t('Text area'),
  53. 'help' => t('Provide markup text for the area.'),
  54. 'area' => [
  55. 'id' => 'text',
  56. ],
  57. ];
  58. $data['views']['area_text_custom'] = [
  59. 'title' => t('Unfiltered text'),
  60. 'help' => t('Add unrestricted, custom text or markup. This is similar to the custom text field.'),
  61. 'area' => [
  62. 'id' => 'text_custom',
  63. ],
  64. ];
  65. $data['views']['title'] = [
  66. 'title' => t('Title override'),
  67. 'help' => t('Override the default view title for this view. This is useful to display an alternative title when a view is empty.'),
  68. 'area' => [
  69. 'id' => 'title',
  70. 'sub_type' => 'empty',
  71. ],
  72. ];
  73. $data['views']['view'] = [
  74. 'title' => t('View area'),
  75. 'help' => t('Insert a view inside an area.'),
  76. 'area' => [
  77. 'id' => 'view',
  78. ],
  79. ];
  80. $data['views']['result'] = [
  81. 'title' => t('Result summary'),
  82. 'help' => t('Shows result summary, for example the items per page.'),
  83. 'area' => [
  84. 'id' => 'result',
  85. ],
  86. ];
  87. $data['views']['messages'] = [
  88. 'title' => t('Messages'),
  89. 'help' => t('Displays messages in an area.'),
  90. 'area' => [
  91. 'id' => 'messages',
  92. ],
  93. ];
  94. $data['views']['http_status_code'] = [
  95. 'title' => t('Response status code'),
  96. 'help' => t('Alter the HTTP response status code used by this view, mostly helpful for empty results.'),
  97. 'area' => [
  98. 'id' => 'http_status_code',
  99. ],
  100. ];
  101. $data['views']['combine'] = [
  102. 'title' => t('Combine fields filter'),
  103. 'help' => t('Combine multiple fields together and search by them.'),
  104. 'filter' => [
  105. 'id' => 'combine',
  106. ],
  107. ];
  108. $data['views']['dropbutton'] = [
  109. 'title' => t('Dropbutton'),
  110. 'help' => t('Display fields in a dropbutton.'),
  111. 'field' => [
  112. 'id' => 'dropbutton',
  113. ],
  114. ];
  115. // Registers an entity area handler per entity type.
  116. foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
  117. // Excludes entity types, which cannot be rendered.
  118. if ($entity_type->hasViewBuilderClass()) {
  119. $label = $entity_type->getLabel();
  120. $data['views']['entity_' . $entity_type_id] = [
  121. 'title' => t('Rendered entity - @label', ['@label' => $label]),
  122. 'help' => t('Displays a rendered @label entity in an area.', ['@label' => $label]),
  123. 'area' => [
  124. 'entity_type' => $entity_type_id,
  125. 'id' => 'entity',
  126. ],
  127. ];
  128. }
  129. }
  130. // Registers an action bulk form per entity.
  131. foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $entity_info) {
  132. $actions = array_filter(\Drupal::entityManager()->getStorage('action')->loadMultiple(), function (ActionConfigEntityInterface $action) use ($entity_type) {
  133. return $action->getType() == $entity_type;
  134. });
  135. if (empty($actions)) {
  136. continue;
  137. }
  138. $data[$entity_info->getBaseTable()][$entity_type . '_bulk_form'] = [
  139. 'title' => t('Bulk update'),
  140. 'help' => t('Allows users to apply an action to one or more items.'),
  141. 'field' => [
  142. 'id' => 'bulk_form',
  143. ],
  144. ];
  145. }
  146. // Registers views data for the entity itself.
  147. foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
  148. if ($entity_type->hasHandlerClass('views_data')) {
  149. /** @var \Drupal\views\EntityViewsDataInterface $views_data */
  150. $views_data = \Drupal::entityManager()->getHandler($entity_type_id, 'views_data');
  151. $data = NestedArray::mergeDeep($data, $views_data->getViewsData());
  152. }
  153. }
  154. // Field modules can implement hook_field_views_data() to override the default
  155. // behavior for adding fields.
  156. $module_handler = \Drupal::moduleHandler();
  157. $entity_manager = \Drupal::entityManager();
  158. if ($entity_manager->hasDefinition('field_storage_config')) {
  159. /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  160. foreach ($entity_manager->getStorage('field_storage_config')->loadMultiple() as $field_storage) {
  161. if (_views_field_get_entity_type_storage($field_storage)) {
  162. $result = (array) $module_handler->invoke($field_storage->getTypeProvider(), 'field_views_data', [$field_storage]);
  163. if (empty($result)) {
  164. $result = views_field_default_views_data($field_storage);
  165. }
  166. $module_handler->alter('field_views_data', $result, $field_storage);
  167. if (is_array($result)) {
  168. $data = NestedArray::mergeDeep($result, $data);
  169. }
  170. }
  171. }
  172. }
  173. return $data;
  174. }
  175. /**
  176. * Implements hook_views_data_alter().
  177. *
  178. * Field modules can implement hook_field_views_data_views_data_alter() to
  179. * alter the views data on a per field basis. This is weirdly named so as
  180. * not to conflict with the \Drupal::moduleHandler()->alter('field_views_data')
  181. * in views_views_data().
  182. */
  183. function views_views_data_alter(&$data) {
  184. $entity_manager = \Drupal::entityManager();
  185. if (!$entity_manager->hasDefinition('field_storage_config')) {
  186. return;
  187. }
  188. /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
  189. foreach ($entity_manager->getStorage('field_storage_config')->loadMultiple() as $field_storage) {
  190. if (_views_field_get_entity_type_storage($field_storage)) {
  191. $function = $field_storage->getTypeProvider() . '_field_views_data_views_data_alter';
  192. if (function_exists($function)) {
  193. $function($data, $field_storage);
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Determines whether the entity type the field appears in is SQL based.
  200. *
  201. * @param \Drupal\field\FieldStorageConfigInterface $field_storage
  202. * The field storage definition.
  203. *
  204. * @return \Drupal\Core\Entity\Sql\SqlContentEntityStorage
  205. * Returns the entity type storage if supported.
  206. */
  207. function _views_field_get_entity_type_storage(FieldStorageConfigInterface $field_storage) {
  208. $result = FALSE;
  209. $entity_manager = \Drupal::entityManager();
  210. if ($entity_manager->hasDefinition($field_storage->getTargetEntityTypeId())) {
  211. $storage = $entity_manager->getStorage($field_storage->getTargetEntityTypeId());
  212. $result = $storage instanceof SqlContentEntityStorage ? $storage : FALSE;
  213. }
  214. return $result;
  215. }
  216. /**
  217. * Returns the label of a certain field.
  218. *
  219. * Therefore it looks up in all bundles to find the most used field.
  220. */
  221. function views_entity_field_label($entity_type, $field_name) {
  222. $label_counter = [];
  223. $all_labels = [];
  224. // Count the amount of fields per label per field storage.
  225. foreach (array_keys(\Drupal::entityManager()->getBundleInfo($entity_type)) as $bundle) {
  226. $bundle_fields = array_filter(\Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle), function ($field_definition) {
  227. return $field_definition instanceof FieldConfigInterface;
  228. });
  229. if (isset($bundle_fields[$field_name])) {
  230. $field = $bundle_fields[$field_name];
  231. $label = $field->getLabel();
  232. $label_counter[$label] = isset($label_counter[$label]) ? ++$label_counter[$label] : 1;
  233. $all_labels[$label] = TRUE;
  234. }
  235. }
  236. if (empty($label_counter)) {
  237. return [$field_name, $all_labels];
  238. }
  239. // Sort the field labels by it most used label and return the most used one.
  240. // If the counts are equal, sort by the label to ensure the result is
  241. // deterministic.
  242. uksort($label_counter, function ($a, $b) use ($label_counter) {
  243. if ($label_counter[$a] === $label_counter[$b]) {
  244. return strcmp($a, $b);
  245. }
  246. return $label_counter[$a] > $label_counter[$b] ? -1 : 1;
  247. });
  248. $label_counter = array_keys($label_counter);
  249. return [$label_counter[0], $all_labels];
  250. }
  251. /**
  252. * Default views data implementation for a field.
  253. *
  254. * @param \Drupal\field\FieldStorageConfigInterface $field_storage
  255. * The field definition.
  256. *
  257. * @return array
  258. * The default views data for the field.
  259. */
  260. function views_field_default_views_data(FieldStorageConfigInterface $field_storage) {
  261. $data = [];
  262. // Check the field type is available.
  263. if (!\Drupal::service('plugin.manager.field.field_type')->hasDefinition($field_storage->getType())) {
  264. return $data;
  265. }
  266. // Check the field storage has fields.
  267. if (!$field_storage->getBundles()) {
  268. return $data;
  269. }
  270. // Ignore custom storage too.
  271. if ($field_storage->hasCustomStorage()) {
  272. return $data;
  273. }
  274. // Check whether the entity type storage is supported.
  275. $storage = _views_field_get_entity_type_storage($field_storage);
  276. if (!$storage) {
  277. return $data;
  278. }
  279. $field_name = $field_storage->getName();
  280. $field_columns = $field_storage->getColumns();
  281. // Grab information about the entity type tables.
  282. // We need to join to both the base table and the data table, if available.
  283. $entity_manager = \Drupal::entityManager();
  284. $entity_type_id = $field_storage->getTargetEntityTypeId();
  285. $entity_type = $entity_manager->getDefinition($entity_type_id);
  286. if (!$base_table = $entity_type->getBaseTable()) {
  287. // We cannot do anything if for some reason there is no base table.
  288. return $data;
  289. }
  290. $entity_tables = [$base_table => $entity_type_id];
  291. // Some entities may not have a data table.
  292. $data_table = $entity_type->getDataTable();
  293. if ($data_table) {
  294. $entity_tables[$data_table] = $entity_type_id;
  295. }
  296. $entity_revision_table = $entity_type->getRevisionTable();
  297. $supports_revisions = $entity_type->hasKey('revision') && $entity_revision_table;
  298. if ($supports_revisions) {
  299. $entity_tables[$entity_revision_table] = $entity_type_id;
  300. $entity_revision_data_table = $entity_type->getRevisionDataTable();
  301. if ($entity_revision_data_table) {
  302. $entity_tables[$entity_revision_data_table] = $entity_type_id;
  303. }
  304. }
  305. // Description of the field tables.
  306. // @todo Generalize this code to make it work with any table layout. See
  307. // https://www.drupal.org/node/2079019.
  308. $table_mapping = $storage->getTableMapping();
  309. $field_tables = [
  310. EntityStorageInterface::FIELD_LOAD_CURRENT => [
  311. 'table' => $table_mapping->getDedicatedDataTableName($field_storage),
  312. 'alias' => "{$entity_type_id}__{$field_name}",
  313. ],
  314. ];
  315. if ($supports_revisions) {
  316. $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION] = [
  317. 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage),
  318. 'alias' => "{$entity_type_id}_revision__{$field_name}",
  319. ];
  320. }
  321. // Determine if the fields are translatable.
  322. $bundles_names = $field_storage->getBundles();
  323. $translation_join_type = FALSE;
  324. $fields = [];
  325. $translatable_configs = [];
  326. $untranslatable_configs = [];
  327. $untranslatable_config_bundles = [];
  328. foreach ($bundles_names as $bundle) {
  329. $fields[$bundle] = FieldConfig::loadByName($entity_type->id(), $bundle, $field_name);
  330. }
  331. foreach ($fields as $bundle => $config_entity) {
  332. if (!empty($config_entity)) {
  333. if ($config_entity->isTranslatable()) {
  334. $translatable_configs[$bundle] = $config_entity;
  335. }
  336. else {
  337. $untranslatable_configs[$bundle] = $config_entity;
  338. }
  339. }
  340. else {
  341. // https://www.drupal.org/node/2451657#comment-11462881
  342. \Drupal::logger('views')->error(
  343. 'A non-existent config entity name returned by FieldStorageConfigInterface::getBundles(): entity type: %entity_type, bundle: %bundle, field name: %field',
  344. [
  345. '%entity_type' => $entity_type->id(),
  346. '%bundle' => $bundle,
  347. '%field' => $field_name,
  348. ]
  349. );
  350. }
  351. }
  352. // If the field is translatable on all the bundles, there will be a join on
  353. // the langcode.
  354. if (!empty($translatable_configs) && empty($untranslatable_configs)) {
  355. $translation_join_type = 'language';
  356. }
  357. // If the field is translatable only on certain bundles, there will be a join
  358. // on langcode OR bundle name.
  359. elseif (!empty($translatable_configs) && !empty($untranslatable_configs)) {
  360. foreach ($untranslatable_configs as $config) {
  361. $untranslatable_config_bundles[] = $config->getTargetBundle();
  362. }
  363. $translation_join_type = 'language_bundle';
  364. }
  365. // Build the relationships between the field table and the entity tables.
  366. $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT]['alias'];
  367. if ($data_table) {
  368. // Tell Views how to join to the base table, via the data table.
  369. $data[$table_alias]['table']['join'][$data_table] = [
  370. 'table' => $table_mapping->getDedicatedDataTableName($field_storage),
  371. 'left_field' => $entity_type->getKey('id'),
  372. 'field' => 'entity_id',
  373. 'extra' => [
  374. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  375. ],
  376. ];
  377. }
  378. else {
  379. // If there is no data table, just join directly.
  380. $data[$table_alias]['table']['join'][$base_table] = [
  381. 'table' => $table_mapping->getDedicatedDataTableName($field_storage),
  382. 'left_field' => $entity_type->getKey('id'),
  383. 'field' => 'entity_id',
  384. 'extra' => [
  385. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  386. ],
  387. ];
  388. }
  389. if ($translation_join_type === 'language_bundle') {
  390. $data[$table_alias]['table']['join'][$data_table]['join_id'] = 'field_or_language_join';
  391. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  392. 'left_field' => 'langcode',
  393. 'field' => 'langcode',
  394. ];
  395. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  396. 'field' => 'bundle',
  397. 'value' => $untranslatable_config_bundles,
  398. ];
  399. }
  400. elseif ($translation_join_type === 'language') {
  401. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  402. 'left_field' => 'langcode',
  403. 'field' => 'langcode',
  404. ];
  405. }
  406. if ($supports_revisions) {
  407. $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
  408. if ($entity_revision_data_table) {
  409. // Tell Views how to join to the revision table, via the data table.
  410. $data[$table_alias]['table']['join'][$entity_revision_data_table] = [
  411. 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage),
  412. 'left_field' => $entity_type->getKey('revision'),
  413. 'field' => 'revision_id',
  414. 'extra' => [
  415. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  416. ],
  417. ];
  418. }
  419. else {
  420. // If there is no data table, just join directly.
  421. $data[$table_alias]['table']['join'][$entity_revision_table] = [
  422. 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage),
  423. 'left_field' => $entity_type->getKey('revision'),
  424. 'field' => 'revision_id',
  425. 'extra' => [
  426. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  427. ],
  428. ];
  429. }
  430. if ($translation_join_type === 'language_bundle') {
  431. $data[$table_alias]['table']['join'][$entity_revision_data_table]['join_id'] = 'field_or_language_join';
  432. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  433. 'left_field' => 'langcode',
  434. 'field' => 'langcode',
  435. ];
  436. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  437. 'value' => $untranslatable_config_bundles,
  438. 'field' => 'bundle',
  439. ];
  440. }
  441. elseif ($translation_join_type === 'language') {
  442. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  443. 'left_field' => 'langcode',
  444. 'field' => 'langcode',
  445. ];
  446. }
  447. }
  448. $group_name = $entity_type->getLabel();
  449. // Get the list of bundles the field appears in.
  450. $bundles_names = $field_storage->getBundles();
  451. // Build the list of additional fields to add to queries.
  452. $add_fields = ['delta', 'langcode', 'bundle'];
  453. foreach (array_keys($field_columns) as $column) {
  454. $add_fields[] = $table_mapping->getFieldColumnName($field_storage, $column);
  455. }
  456. // Determine the label to use for the field. We don't have a label available
  457. // at the field level, so we just go through all fields and take the one
  458. // which is used the most frequently.
  459. list($label, $all_labels) = views_entity_field_label($entity_type_id, $field_name);
  460. // Expose data for the field as a whole.
  461. foreach ($field_tables as $type => $table_info) {
  462. $table = $table_info['table'];
  463. $table_alias = $table_info['alias'];
  464. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  465. $group = $group_name;
  466. $field_alias = $field_name;
  467. }
  468. else {
  469. $group = t('@group (historical data)', ['@group' => $group_name]);
  470. $field_alias = $field_name . '-revision_id';
  471. }
  472. $data[$table_alias][$field_alias] = [
  473. 'group' => $group,
  474. 'title' => $label,
  475. 'title short' => $label,
  476. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  477. ];
  478. // Go through and create a list of aliases for all possible combinations of
  479. // entity type + name.
  480. $aliases = [];
  481. $also_known = [];
  482. foreach ($all_labels as $label_name => $true) {
  483. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  484. if ($label != $label_name) {
  485. $aliases[] = [
  486. 'base' => $base_table,
  487. 'group' => $group_name,
  488. 'title' => $label_name,
  489. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]),
  490. ];
  491. $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $label_name]);
  492. }
  493. }
  494. elseif ($supports_revisions && $label != $label_name) {
  495. $aliases[] = [
  496. 'base' => $table,
  497. 'group' => t('@group (historical data)', ['@group' => $group_name]),
  498. 'title' => $label_name,
  499. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]),
  500. ];
  501. $also_known[] = t('@group (historical data): @field', ['@group' => $group_name, '@field' => $label_name]);
  502. }
  503. }
  504. if ($aliases) {
  505. $data[$table_alias][$field_alias]['aliases'] = $aliases;
  506. // The $also_known variable contains markup that is HTML escaped and that
  507. // loses safeness when imploded. The help text is used in #description
  508. // and therefore XSS admin filtered by default. Escaped HTML is not
  509. // altered by XSS filtering, therefore it is safe to just concatenate the
  510. // strings. Afterwards we mark the entire string as safe, so it won't be
  511. // escaped, no matter where it is used.
  512. // Considering the dual use of this help data (both as metadata and as
  513. // help text), other patterns such as use of #markup would not be correct
  514. // here.
  515. $data[$table_alias][$field_alias]['help'] = Markup::create($data[$table_alias][$field_alias]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  516. }
  517. $keys = array_keys($field_columns);
  518. $real_field = reset($keys);
  519. $data[$table_alias][$field_alias]['field'] = [
  520. 'table' => $table,
  521. 'id' => 'field',
  522. 'field_name' => $field_name,
  523. 'entity_type' => $entity_type_id,
  524. // Provide a real field for group by.
  525. 'real field' => $field_alias . '_' . $real_field,
  526. 'additional fields' => $add_fields,
  527. // Default the element type to div, let the UI change it if necessary.
  528. 'element type' => 'div',
  529. 'is revision' => $type == EntityStorageInterface::FIELD_LOAD_REVISION,
  530. ];
  531. }
  532. // Expose data for each field property individually.
  533. foreach ($field_columns as $column => $attributes) {
  534. $allow_sort = TRUE;
  535. // Identify likely filters and arguments for each column based on field type.
  536. switch ($attributes['type']) {
  537. case 'int':
  538. case 'mediumint':
  539. case 'tinyint':
  540. case 'bigint':
  541. case 'serial':
  542. case 'numeric':
  543. case 'float':
  544. $filter = 'numeric';
  545. $argument = 'numeric';
  546. $sort = 'standard';
  547. if ($field_storage->getType() == 'boolean') {
  548. $filter = 'boolean';
  549. }
  550. break;
  551. case 'text':
  552. case 'blob':
  553. // It does not make sense to sort by blob or text.
  554. $allow_sort = FALSE;
  555. default:
  556. $filter = 'string';
  557. $argument = 'string';
  558. $sort = 'standard';
  559. break;
  560. }
  561. if (count($field_columns) == 1 || $column == 'value') {
  562. $title = t('@label (@name)', ['@label' => $label, '@name' => $field_name]);
  563. $title_short = $label;
  564. }
  565. else {
  566. $title = t('@label (@name:@column)', ['@label' => $label, '@name' => $field_name, '@column' => $column]);
  567. $title_short = t('@label:@column', ['@label' => $label, '@column' => $column]);
  568. }
  569. // Expose data for the property.
  570. foreach ($field_tables as $type => $table_info) {
  571. $table = $table_info['table'];
  572. $table_alias = $table_info['alias'];
  573. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  574. $group = $group_name;
  575. }
  576. else {
  577. $group = t('@group (historical data)', ['@group' => $group_name]);
  578. }
  579. $column_real_name = $table_mapping->getFieldColumnName($field_storage, $column);
  580. // Load all the fields from the table by default.
  581. $additional_fields = $table_mapping->getAllColumns($table);
  582. $data[$table_alias][$column_real_name] = [
  583. 'group' => $group,
  584. 'title' => $title,
  585. 'title short' => $title_short,
  586. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  587. ];
  588. // Go through and create a list of aliases for all possible combinations of
  589. // entity type + name.
  590. $aliases = [];
  591. $also_known = [];
  592. foreach ($all_labels as $label_name => $true) {
  593. if ($label != $label_name) {
  594. if (count($field_columns) == 1 || $column == 'value') {
  595. $alias_title = t('@label (@name)', ['@label' => $label_name, '@name' => $field_name]);
  596. }
  597. else {
  598. $alias_title = t('@label (@name:@column)', ['@label' => $label_name, '@name' => $field_name, '@column' => $column]);
  599. }
  600. $aliases[] = [
  601. 'group' => $group_name,
  602. 'title' => $alias_title,
  603. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $title]),
  604. ];
  605. $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $title]);
  606. }
  607. }
  608. if ($aliases) {
  609. $data[$table_alias][$column_real_name]['aliases'] = $aliases;
  610. // The $also_known variable contains markup that is HTML escaped and
  611. // that loses safeness when imploded. The help text is used in
  612. // #description and therefore XSS admin filtered by default. Escaped
  613. // HTML is not altered by XSS filtering, therefore it is safe to just
  614. // concatenate the strings. Afterwards we mark the entire string as
  615. // safe, so it won't be escaped, no matter where it is used.
  616. // Considering the dual use of this help data (both as metadata and as
  617. // help text), other patterns such as use of #markup would not be
  618. // correct here.
  619. $data[$table_alias][$column_real_name]['help'] = Markup::create($data[$table_alias][$column_real_name]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  620. }
  621. $data[$table_alias][$column_real_name]['argument'] = [
  622. 'field' => $column_real_name,
  623. 'table' => $table,
  624. 'id' => $argument,
  625. 'additional fields' => $additional_fields,
  626. 'field_name' => $field_name,
  627. 'entity_type' => $entity_type_id,
  628. 'empty field name' => t('- No value -'),
  629. ];
  630. $data[$table_alias][$column_real_name]['filter'] = [
  631. 'field' => $column_real_name,
  632. 'table' => $table,
  633. 'id' => $filter,
  634. 'additional fields' => $additional_fields,
  635. 'field_name' => $field_name,
  636. 'entity_type' => $entity_type_id,
  637. 'allow empty' => TRUE,
  638. ];
  639. if (!empty($allow_sort)) {
  640. $data[$table_alias][$column_real_name]['sort'] = [
  641. 'field' => $column_real_name,
  642. 'table' => $table,
  643. 'id' => $sort,
  644. 'additional fields' => $additional_fields,
  645. 'field_name' => $field_name,
  646. 'entity_type' => $entity_type_id,
  647. ];
  648. }
  649. // Set click sortable if there is a field definition.
  650. if (isset($data[$table_alias][$field_name]['field'])) {
  651. $data[$table_alias][$field_name]['field']['click sortable'] = $allow_sort;
  652. }
  653. // Expose additional delta column for multiple value fields.
  654. if ($field_storage->isMultiple()) {
  655. $title_delta = t('@label (@name:delta)', ['@label' => $label, '@name' => $field_name]);
  656. $title_short_delta = t('@label:delta', ['@label' => $label]);
  657. $data[$table_alias]['delta'] = [
  658. 'group' => $group,
  659. 'title' => $title_delta,
  660. 'title short' => $title_short_delta,
  661. 'help' => t('Delta - Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  662. ];
  663. $data[$table_alias]['delta']['field'] = [
  664. 'id' => 'numeric',
  665. ];
  666. $data[$table_alias]['delta']['argument'] = [
  667. 'field' => 'delta',
  668. 'table' => $table,
  669. 'id' => 'numeric',
  670. 'additional fields' => $additional_fields,
  671. 'empty field name' => t('- No value -'),
  672. 'field_name' => $field_name,
  673. 'entity_type' => $entity_type_id,
  674. ];
  675. $data[$table_alias]['delta']['filter'] = [
  676. 'field' => 'delta',
  677. 'table' => $table,
  678. 'id' => 'numeric',
  679. 'additional fields' => $additional_fields,
  680. 'field_name' => $field_name,
  681. 'entity_type' => $entity_type_id,
  682. 'allow empty' => TRUE,
  683. ];
  684. $data[$table_alias]['delta']['sort'] = [
  685. 'field' => 'delta',
  686. 'table' => $table,
  687. 'id' => 'standard',
  688. 'additional fields' => $additional_fields,
  689. 'field_name' => $field_name,
  690. 'entity_type' => $entity_type_id,
  691. ];
  692. }
  693. }
  694. }
  695. return $data;
  696. }
  697. /**
  698. * Implements hook_field_views_data().
  699. *
  700. * The function implements the hook in behalf of 'core' because it adds a
  701. * relationship and a reverse relationship to entity_reference field type, which
  702. * is provided by core.
  703. */
  704. function core_field_views_data(FieldStorageConfigInterface $field_storage) {
  705. $data = views_field_default_views_data($field_storage);
  706. // The code below only deals with the Entity reference field type.
  707. if ($field_storage->getType() != 'entity_reference') {
  708. return $data;
  709. }
  710. $entity_manager = \Drupal::entityManager();
  711. $entity_type_id = $field_storage->getTargetEntityTypeId();
  712. /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  713. $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
  714. foreach ($data as $table_name => $table_data) {
  715. // Add a relationship to the target entity type.
  716. $target_entity_type_id = $field_storage->getSetting('target_type');
  717. $target_entity_type = $entity_manager->getDefinition($target_entity_type_id);
  718. $entity_type_id = $field_storage->getTargetEntityTypeId();
  719. $entity_type = $entity_manager->getDefinition($entity_type_id);
  720. $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
  721. $field_name = $field_storage->getName();
  722. // Provide a relationship for the entity type with the entity reference
  723. // field.
  724. $args = [
  725. '@label' => $target_entity_type->getLabel(),
  726. '@field_name' => $field_name,
  727. ];
  728. $data[$table_name][$field_name]['relationship'] = [
  729. 'title' => t('@label referenced from @field_name', $args),
  730. 'label' => t('@field_name: @label', $args),
  731. 'group' => $entity_type->getLabel(),
  732. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $field_storage->getBundles())]),
  733. 'id' => 'standard',
  734. 'base' => $target_base_table,
  735. 'entity type' => $target_entity_type_id,
  736. 'base field' => $target_entity_type->getKey('id'),
  737. 'relationship field' => $field_name . '_target_id',
  738. ];
  739. // Provide a reverse relationship for the entity type that is referenced by
  740. // the field.
  741. $args['@entity'] = $entity_type->getLabel();
  742. $args['@label'] = $target_entity_type->getLowercaseLabel();
  743. $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
  744. $data[$target_base_table][$pseudo_field_name]['relationship'] = [
  745. 'title' => t('@entity using @field_name', $args),
  746. 'label' => t('@field_name', ['@field_name' => $field_name]),
  747. 'group' => $target_entity_type->getLabel(),
  748. 'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
  749. 'id' => 'entity_reverse',
  750. 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
  751. 'entity_type' => $entity_type_id,
  752. 'base field' => $entity_type->getKey('id'),
  753. 'field_name' => $field_name,
  754. 'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
  755. 'field field' => $field_name . '_target_id',
  756. 'join_extra' => [
  757. [
  758. 'field' => 'deleted',
  759. 'value' => 0,
  760. 'numeric' => TRUE,
  761. ],
  762. ],
  763. ];
  764. }
  765. return $data;
  766. }