views.views.inc 27 KB

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