views.views.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. t('A non-existent config entity name returned by FieldStorageConfigInterface::getBundles(): field name: %field, bundle: %bundle',
  344. ['%field' => $field_name, '%bundle' => $bundle]
  345. ));
  346. }
  347. }
  348. // If the field is translatable on all the bundles, there will be a join on
  349. // the langcode.
  350. if (!empty($translatable_configs) && empty($untranslatable_configs)) {
  351. $translation_join_type = 'language';
  352. }
  353. // If the field is translatable only on certain bundles, there will be a join
  354. // on langcode OR bundle name.
  355. elseif (!empty($translatable_configs) && !empty($untranslatable_configs)) {
  356. foreach ($untranslatable_configs as $config) {
  357. $untranslatable_config_bundles[] = $config->getTargetBundle();
  358. }
  359. $translation_join_type = 'language_bundle';
  360. }
  361. // Build the relationships between the field table and the entity tables.
  362. $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT]['alias'];
  363. if ($data_table) {
  364. // Tell Views how to join to the base table, via the data table.
  365. $data[$table_alias]['table']['join'][$data_table] = [
  366. 'table' => $table_mapping->getDedicatedDataTableName($field_storage),
  367. 'left_field' => $entity_type->getKey('id'),
  368. 'field' => 'entity_id',
  369. 'extra' => [
  370. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  371. ],
  372. ];
  373. }
  374. else {
  375. // If there is no data table, just join directly.
  376. $data[$table_alias]['table']['join'][$base_table] = [
  377. 'table' => $table_mapping->getDedicatedDataTableName($field_storage),
  378. 'left_field' => $entity_type->getKey('id'),
  379. 'field' => 'entity_id',
  380. 'extra' => [
  381. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  382. ],
  383. ];
  384. }
  385. if ($translation_join_type === 'language_bundle') {
  386. $data[$table_alias]['table']['join'][$data_table]['join_id'] = 'field_or_language_join';
  387. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  388. 'left_field' => 'langcode',
  389. 'field' => 'langcode',
  390. ];
  391. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  392. 'field' => 'bundle',
  393. 'value' => $untranslatable_config_bundles,
  394. ];
  395. }
  396. elseif ($translation_join_type === 'language') {
  397. $data[$table_alias]['table']['join'][$data_table]['extra'][] = [
  398. 'left_field' => 'langcode',
  399. 'field' => 'langcode',
  400. ];
  401. }
  402. if ($supports_revisions) {
  403. $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
  404. if ($entity_revision_data_table) {
  405. // Tell Views how to join to the revision table, via the data table.
  406. $data[$table_alias]['table']['join'][$entity_revision_data_table] = [
  407. 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage),
  408. 'left_field' => $entity_type->getKey('revision'),
  409. 'field' => 'revision_id',
  410. 'extra' => [
  411. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  412. ],
  413. ];
  414. }
  415. else {
  416. // If there is no data table, just join directly.
  417. $data[$table_alias]['table']['join'][$entity_revision_table] = [
  418. 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage),
  419. 'left_field' => $entity_type->getKey('revision'),
  420. 'field' => 'revision_id',
  421. 'extra' => [
  422. ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
  423. ],
  424. ];
  425. }
  426. if ($translation_join_type === 'language_bundle') {
  427. $data[$table_alias]['table']['join'][$entity_revision_data_table]['join_id'] = 'field_or_language_join';
  428. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  429. 'left_field' => 'langcode',
  430. 'field' => 'langcode',
  431. ];
  432. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  433. 'value' => $untranslatable_config_bundles,
  434. 'field' => 'bundle',
  435. ];
  436. }
  437. elseif ($translation_join_type === 'language') {
  438. $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [
  439. 'left_field' => 'langcode',
  440. 'field' => 'langcode',
  441. ];
  442. }
  443. }
  444. $group_name = $entity_type->getLabel();
  445. // Get the list of bundles the field appears in.
  446. $bundles_names = $field_storage->getBundles();
  447. // Build the list of additional fields to add to queries.
  448. $add_fields = ['delta', 'langcode', 'bundle'];
  449. foreach (array_keys($field_columns) as $column) {
  450. $add_fields[] = $table_mapping->getFieldColumnName($field_storage, $column);
  451. }
  452. // Determine the label to use for the field. We don't have a label available
  453. // at the field level, so we just go through all fields and take the one
  454. // which is used the most frequently.
  455. list($label, $all_labels) = views_entity_field_label($entity_type_id, $field_name);
  456. // Expose data for the field as a whole.
  457. foreach ($field_tables as $type => $table_info) {
  458. $table = $table_info['table'];
  459. $table_alias = $table_info['alias'];
  460. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  461. $group = $group_name;
  462. $field_alias = $field_name;
  463. }
  464. else {
  465. $group = t('@group (historical data)', ['@group' => $group_name]);
  466. $field_alias = $field_name . '-revision_id';
  467. }
  468. $data[$table_alias][$field_alias] = [
  469. 'group' => $group,
  470. 'title' => $label,
  471. 'title short' => $label,
  472. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  473. ];
  474. // Go through and create a list of aliases for all possible combinations of
  475. // entity type + name.
  476. $aliases = [];
  477. $also_known = [];
  478. foreach ($all_labels as $label_name => $true) {
  479. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  480. if ($label != $label_name) {
  481. $aliases[] = [
  482. 'base' => $base_table,
  483. 'group' => $group_name,
  484. 'title' => $label_name,
  485. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]),
  486. ];
  487. $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $label_name]);
  488. }
  489. }
  490. elseif ($supports_revisions && $label != $label_name) {
  491. $aliases[] = [
  492. 'base' => $table,
  493. 'group' => t('@group (historical data)', ['@group' => $group_name]),
  494. 'title' => $label_name,
  495. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]),
  496. ];
  497. $also_known[] = t('@group (historical data): @field', ['@group' => $group_name, '@field' => $label_name]);
  498. }
  499. }
  500. if ($aliases) {
  501. $data[$table_alias][$field_alias]['aliases'] = $aliases;
  502. // The $also_known variable contains markup that is HTML escaped and that
  503. // loses safeness when imploded. The help text is used in #description
  504. // and therefore XSS admin filtered by default. Escaped HTML is not
  505. // altered by XSS filtering, therefore it is safe to just concatenate the
  506. // strings. Afterwards we mark the entire string as safe, so it won't be
  507. // escaped, no matter where it is used.
  508. // Considering the dual use of this help data (both as metadata and as
  509. // help text), other patterns such as use of #markup would not be correct
  510. // here.
  511. $data[$table_alias][$field_alias]['help'] = Markup::create($data[$table_alias][$field_alias]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  512. }
  513. $keys = array_keys($field_columns);
  514. $real_field = reset($keys);
  515. $data[$table_alias][$field_alias]['field'] = [
  516. 'table' => $table,
  517. 'id' => 'field',
  518. 'field_name' => $field_name,
  519. 'entity_type' => $entity_type_id,
  520. // Provide a real field for group by.
  521. 'real field' => $field_alias . '_' . $real_field,
  522. 'additional fields' => $add_fields,
  523. // Default the element type to div, let the UI change it if necessary.
  524. 'element type' => 'div',
  525. 'is revision' => $type == EntityStorageInterface::FIELD_LOAD_REVISION,
  526. ];
  527. }
  528. // Expose data for each field property individually.
  529. foreach ($field_columns as $column => $attributes) {
  530. $allow_sort = TRUE;
  531. // Identify likely filters and arguments for each column based on field type.
  532. switch ($attributes['type']) {
  533. case 'int':
  534. case 'mediumint':
  535. case 'tinyint':
  536. case 'bigint':
  537. case 'serial':
  538. case 'numeric':
  539. case 'float':
  540. $filter = 'numeric';
  541. $argument = 'numeric';
  542. $sort = 'standard';
  543. if ($field_storage->getType() == 'boolean') {
  544. $filter = 'boolean';
  545. }
  546. break;
  547. case 'text':
  548. case 'blob':
  549. // It does not make sense to sort by blob or text.
  550. $allow_sort = FALSE;
  551. default:
  552. $filter = 'string';
  553. $argument = 'string';
  554. $sort = 'standard';
  555. break;
  556. }
  557. if (count($field_columns) == 1 || $column == 'value') {
  558. $title = t('@label (@name)', ['@label' => $label, '@name' => $field_name]);
  559. $title_short = $label;
  560. }
  561. else {
  562. $title = t('@label (@name:@column)', ['@label' => $label, '@name' => $field_name, '@column' => $column]);
  563. $title_short = t('@label:@column', ['@label' => $label, '@column' => $column]);
  564. }
  565. // Expose data for the property.
  566. foreach ($field_tables as $type => $table_info) {
  567. $table = $table_info['table'];
  568. $table_alias = $table_info['alias'];
  569. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  570. $group = $group_name;
  571. }
  572. else {
  573. $group = t('@group (historical data)', ['@group' => $group_name]);
  574. }
  575. $column_real_name = $table_mapping->getFieldColumnName($field_storage, $column);
  576. // Load all the fields from the table by default.
  577. $additional_fields = $table_mapping->getAllColumns($table);
  578. $data[$table_alias][$column_real_name] = [
  579. 'group' => $group,
  580. 'title' => $title,
  581. 'title short' => $title_short,
  582. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  583. ];
  584. // Go through and create a list of aliases for all possible combinations of
  585. // entity type + name.
  586. $aliases = [];
  587. $also_known = [];
  588. foreach ($all_labels as $label_name => $true) {
  589. if ($label != $label_name) {
  590. if (count($field_columns) == 1 || $column == 'value') {
  591. $alias_title = t('@label (@name)', ['@label' => $label_name, '@name' => $field_name]);
  592. }
  593. else {
  594. $alias_title = t('@label (@name:@column)', ['@label' => $label_name, '@name' => $field_name, '@column' => $column]);
  595. }
  596. $aliases[] = [
  597. 'group' => $group_name,
  598. 'title' => $alias_title,
  599. 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $title]),
  600. ];
  601. $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $title]);
  602. }
  603. }
  604. if ($aliases) {
  605. $data[$table_alias][$column_real_name]['aliases'] = $aliases;
  606. // The $also_known variable contains markup that is HTML escaped and
  607. // that loses safeness when imploded. The help text is used in
  608. // #description and therefore XSS admin filtered by default. Escaped
  609. // HTML is not altered by XSS filtering, therefore it is safe to just
  610. // concatenate the strings. Afterwards we mark the entire string as
  611. // safe, so it won't be escaped, no matter where it is used.
  612. // Considering the dual use of this help data (both as metadata and as
  613. // help text), other patterns such as use of #markup would not be
  614. // correct here.
  615. $data[$table_alias][$column_real_name]['help'] = Markup::create($data[$table_alias][$column_real_name]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  616. }
  617. $data[$table_alias][$column_real_name]['argument'] = [
  618. 'field' => $column_real_name,
  619. 'table' => $table,
  620. 'id' => $argument,
  621. 'additional fields' => $additional_fields,
  622. 'field_name' => $field_name,
  623. 'entity_type' => $entity_type_id,
  624. 'empty field name' => t('- No value -'),
  625. ];
  626. $data[$table_alias][$column_real_name]['filter'] = [
  627. 'field' => $column_real_name,
  628. 'table' => $table,
  629. 'id' => $filter,
  630. 'additional fields' => $additional_fields,
  631. 'field_name' => $field_name,
  632. 'entity_type' => $entity_type_id,
  633. 'allow empty' => TRUE,
  634. ];
  635. if (!empty($allow_sort)) {
  636. $data[$table_alias][$column_real_name]['sort'] = [
  637. 'field' => $column_real_name,
  638. 'table' => $table,
  639. 'id' => $sort,
  640. 'additional fields' => $additional_fields,
  641. 'field_name' => $field_name,
  642. 'entity_type' => $entity_type_id,
  643. ];
  644. }
  645. // Set click sortable if there is a field definition.
  646. if (isset($data[$table_alias][$field_name]['field'])) {
  647. $data[$table_alias][$field_name]['field']['click sortable'] = $allow_sort;
  648. }
  649. // Expose additional delta column for multiple value fields.
  650. if ($field_storage->isMultiple()) {
  651. $title_delta = t('@label (@name:delta)', ['@label' => $label, '@name' => $field_name]);
  652. $title_short_delta = t('@label:delta', ['@label' => $label]);
  653. $data[$table_alias]['delta'] = [
  654. 'group' => $group,
  655. 'title' => $title_delta,
  656. 'title short' => $title_short_delta,
  657. 'help' => t('Delta - Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]),
  658. ];
  659. $data[$table_alias]['delta']['field'] = [
  660. 'id' => 'numeric',
  661. ];
  662. $data[$table_alias]['delta']['argument'] = [
  663. 'field' => 'delta',
  664. 'table' => $table,
  665. 'id' => 'numeric',
  666. 'additional fields' => $additional_fields,
  667. 'empty field name' => t('- No value -'),
  668. 'field_name' => $field_name,
  669. 'entity_type' => $entity_type_id,
  670. ];
  671. $data[$table_alias]['delta']['filter'] = [
  672. 'field' => 'delta',
  673. 'table' => $table,
  674. 'id' => 'numeric',
  675. 'additional fields' => $additional_fields,
  676. 'field_name' => $field_name,
  677. 'entity_type' => $entity_type_id,
  678. 'allow empty' => TRUE,
  679. ];
  680. $data[$table_alias]['delta']['sort'] = [
  681. 'field' => 'delta',
  682. 'table' => $table,
  683. 'id' => 'standard',
  684. 'additional fields' => $additional_fields,
  685. 'field_name' => $field_name,
  686. 'entity_type' => $entity_type_id,
  687. ];
  688. }
  689. }
  690. }
  691. return $data;
  692. }
  693. /**
  694. * Implements hook_field_views_data().
  695. *
  696. * The function implements the hook in behalf of 'core' because it adds a
  697. * relationship and a reverse relationship to entity_reference field type, which
  698. * is provided by core.
  699. */
  700. function core_field_views_data(FieldStorageConfigInterface $field_storage) {
  701. $data = views_field_default_views_data($field_storage);
  702. // The code below only deals with the Entity reference field type.
  703. if ($field_storage->getType() != 'entity_reference') {
  704. return $data;
  705. }
  706. $entity_manager = \Drupal::entityManager();
  707. $entity_type_id = $field_storage->getTargetEntityTypeId();
  708. /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  709. $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
  710. foreach ($data as $table_name => $table_data) {
  711. // Add a relationship to the target entity type.
  712. $target_entity_type_id = $field_storage->getSetting('target_type');
  713. $target_entity_type = $entity_manager->getDefinition($target_entity_type_id);
  714. $entity_type_id = $field_storage->getTargetEntityTypeId();
  715. $entity_type = $entity_manager->getDefinition($entity_type_id);
  716. $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
  717. $field_name = $field_storage->getName();
  718. // Provide a relationship for the entity type with the entity reference
  719. // field.
  720. $args = [
  721. '@label' => $target_entity_type->getLabel(),
  722. '@field_name' => $field_name,
  723. ];
  724. $data[$table_name][$field_name]['relationship'] = [
  725. 'title' => t('@label referenced from @field_name', $args),
  726. 'label' => t('@field_name: @label', $args),
  727. 'group' => $entity_type->getLabel(),
  728. 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $field_storage->getBundles())]),
  729. 'id' => 'standard',
  730. 'base' => $target_base_table,
  731. 'entity type' => $target_entity_type_id,
  732. 'base field' => $target_entity_type->getKey('id'),
  733. 'relationship field' => $field_name . '_target_id',
  734. ];
  735. // Provide a reverse relationship for the entity type that is referenced by
  736. // the field.
  737. $args['@entity'] = $entity_type->getLabel();
  738. $args['@label'] = $target_entity_type->getLowercaseLabel();
  739. $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
  740. $data[$target_base_table][$pseudo_field_name]['relationship'] = [
  741. 'title' => t('@entity using @field_name', $args),
  742. 'label' => t('@field_name', ['@field_name' => $field_name]),
  743. 'group' => $target_entity_type->getLabel(),
  744. 'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
  745. 'id' => 'entity_reverse',
  746. 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
  747. 'entity_type' => $entity_type_id,
  748. 'base field' => $entity_type->getKey('id'),
  749. 'field_name' => $field_name,
  750. 'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
  751. 'field field' => $field_name . '_target_id',
  752. 'join_extra' => [
  753. [
  754. 'field' => 'deleted',
  755. 'value' => 0,
  756. 'numeric' => TRUE,
  757. ],
  758. ],
  759. ];
  760. }
  761. return $data;
  762. }