views.views.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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'] = array(
  19. // #global is a special flag which allows a table to appear all the time.
  20. '#global' => array(),
  21. );
  22. $data['views']['random'] = array(
  23. 'title' => t('Random'),
  24. 'help' => t('Randomize the display order.'),
  25. 'sort' => array(
  26. 'id' => 'random',
  27. ),
  28. );
  29. $data['views']['null'] = array(
  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' => array(
  33. 'id' => 'null',
  34. ),
  35. );
  36. $data['views']['nothing'] = array(
  37. 'title' => t('Custom text'),
  38. 'help' => t('Provide custom text or link.'),
  39. 'field' => array(
  40. 'id' => 'custom',
  41. ),
  42. );
  43. $data['views']['counter'] = array(
  44. 'title' => t('View result counter'),
  45. 'help' => t('Displays the actual position of the view result'),
  46. 'field' => array(
  47. 'id' => 'counter',
  48. ),
  49. );
  50. $data['views']['area'] = array(
  51. 'title' => t('Text area'),
  52. 'help' => t('Provide markup text for the area.'),
  53. 'area' => array(
  54. 'id' => 'text',
  55. ),
  56. );
  57. $data['views']['area_text_custom'] = array(
  58. 'title' => t('Unfiltered text'),
  59. 'help' => t('Add unrestricted, custom text or markup. This is similar to the custom text field.'),
  60. 'area' => array(
  61. 'id' => 'text_custom',
  62. ),
  63. );
  64. $data['views']['title'] = array(
  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' => array(
  68. 'id' => 'title',
  69. 'sub_type' => 'empty',
  70. ),
  71. );
  72. $data['views']['view'] = array(
  73. 'title' => t('View area'),
  74. 'help' => t('Insert a view inside an area.'),
  75. 'area' => array(
  76. 'id' => 'view',
  77. ),
  78. );
  79. $data['views']['result'] = array(
  80. 'title' => t('Result summary'),
  81. 'help' => t('Shows result summary, for example the items per page.'),
  82. 'area' => array(
  83. 'id' => 'result',
  84. ),
  85. );
  86. $data['views']['messages'] = array(
  87. 'title' => t('Messages'),
  88. 'help' => t('Displays messages in an area.'),
  89. 'area' => array(
  90. 'id' => 'messages',
  91. ),
  92. );
  93. $data['views']['http_status_code'] = array(
  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' => array(
  97. 'id' => 'http_status_code',
  98. ),
  99. );
  100. $data['views']['combine'] = array(
  101. 'title' => t('Combine fields filter'),
  102. 'help' => t('Combine two fields together and search by them.'),
  103. 'filter' => array(
  104. 'id' => 'combine',
  105. ),
  106. );
  107. $data['views']['dropbutton'] = array(
  108. 'title' => t('Dropbutton'),
  109. 'help' => t('Display fields in a dropbutton.'),
  110. 'field' => array(
  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] = array(
  120. 'title' => t('Rendered entity - @label', array('@label' => $label)),
  121. 'help' => t('Displays a rendered @label entity in an area.', array('@label' => $label)),
  122. 'area' => array(
  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'] = array(
  138. 'title' => t('Bulk update'),
  139. 'help' => t('Allows users to apply an action to one or more items.'),
  140. 'field' => array(
  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', array($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 = array();
  222. $all_labels = array();
  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 array($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 array($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 = array();
  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 = array($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 = array(
  309. EntityStorageInterface::FIELD_LOAD_CURRENT => array(
  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] = array(
  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] = array(
  325. 'left_field' => $entity_type->getKey('id'),
  326. 'field' => 'entity_id',
  327. 'extra' => array(
  328. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  329. array('left_field' => 'langcode', 'field' => 'langcode'),
  330. ),
  331. );
  332. }
  333. else {
  334. // If there is no data table, just join directly.
  335. $data[$table_alias]['table']['join'][$base_table] = array(
  336. 'left_field' => $entity_type->getKey('id'),
  337. 'field' => 'entity_id',
  338. 'extra' => array(
  339. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  340. ),
  341. );
  342. }
  343. if ($supports_revisions) {
  344. $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias'];
  345. if ($entity_revision_data_table) {
  346. // Tell Views how to join to the revision table, via the data table.
  347. $data[$table_alias]['table']['join'][$entity_revision_data_table] = array(
  348. 'left_field' => $entity_type->getKey('revision'),
  349. 'field' => 'revision_id',
  350. 'extra' => array(
  351. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  352. array('left_field' => 'langcode', 'field' => 'langcode'),
  353. ),
  354. );
  355. }
  356. else {
  357. // If there is no data table, just join directly.
  358. $data[$table_alias]['table']['join'][$entity_revision_table] = array(
  359. 'left_field' => $entity_type->getKey('revision'),
  360. 'field' => 'revision_id',
  361. 'extra' => array(
  362. array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
  363. ),
  364. );
  365. }
  366. }
  367. $group_name = $entity_type->getLabel();
  368. // Get the list of bundles the field appears in.
  369. $bundles_names = $field_storage->getBundles();
  370. // Build the list of additional fields to add to queries.
  371. $add_fields = array('delta', 'langcode', 'bundle');
  372. foreach (array_keys($field_columns) as $column) {
  373. $add_fields[] = $table_mapping->getFieldColumnName($field_storage, $column);
  374. }
  375. // Determine the label to use for the field. We don't have a label available
  376. // at the field level, so we just go through all fields and take the one
  377. // which is used the most frequently.
  378. list($label, $all_labels) = views_entity_field_label($entity_type_id, $field_name);
  379. // Expose data for the field as a whole.
  380. foreach ($field_tables as $type => $table_info) {
  381. $table = $table_info['table'];
  382. $table_alias = $table_info['alias'];
  383. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  384. $group = $group_name;
  385. $field_alias = $field_name;
  386. }
  387. else {
  388. $group = t('@group (historical data)', array('@group' => $group_name));
  389. $field_alias = $field_name . '-revision_id';
  390. }
  391. $data[$table_alias][$field_alias] = array(
  392. 'group' => $group,
  393. 'title' => $label,
  394. 'title short' => $label,
  395. 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  396. );
  397. // Go through and create a list of aliases for all possible combinations of
  398. // entity type + name.
  399. $aliases = array();
  400. $also_known = array();
  401. foreach ($all_labels as $label_name => $true) {
  402. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  403. if ($label != $label_name) {
  404. $aliases[] = array(
  405. 'base' => $base_table,
  406. 'group' => $group_name,
  407. 'title' => $label_name,
  408. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $label)),
  409. );
  410. $also_known[] = t('@group: @field', array('@group' => $group_name, '@field' => $label_name));
  411. }
  412. }
  413. elseif ($supports_revisions && $label != $label_name) {
  414. $aliases[] = array(
  415. 'base' => $table,
  416. 'group' => t('@group (historical data)', array('@group' => $group_name)),
  417. 'title' => $label_name,
  418. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $label)),
  419. );
  420. $also_known[] = t('@group (historical data): @field', array('@group' => $group_name, '@field' => $label_name));
  421. }
  422. }
  423. if ($aliases) {
  424. $data[$table_alias][$field_alias]['aliases'] = $aliases;
  425. // The $also_known variable contains markup that is HTML escaped and that
  426. // loses safeness when imploded. The help text is used in #description
  427. // and therefore XSS admin filtered by default. Escaped HTML is not
  428. // altered by XSS filtering, therefore it is safe to just concatenate the
  429. // strings. Afterwards we mark the entire string as safe, so it won't be
  430. // escaped, no matter where it is used.
  431. // Considering the dual use of this help data (both as metadata and as
  432. // help text), other patterns such as use of #markup would not be correct
  433. // here.
  434. $data[$table_alias][$field_alias]['help'] = Markup::create($data[$table_alias][$field_alias]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  435. }
  436. $keys = array_keys($field_columns);
  437. $real_field = reset($keys);
  438. $data[$table_alias][$field_alias]['field'] = array(
  439. 'table' => $table,
  440. 'id' => 'field',
  441. 'field_name' => $field_name,
  442. 'entity_type' => $entity_type_id,
  443. // Provide a real field for group by.
  444. 'real field' => $field_alias . '_' . $real_field,
  445. 'additional fields' => $add_fields,
  446. // Default the element type to div, let the UI change it if necessary.
  447. 'element type' => 'div',
  448. 'is revision' => $type == EntityStorageInterface::FIELD_LOAD_REVISION,
  449. );
  450. }
  451. // Expose data for each field property individually.
  452. foreach ($field_columns as $column => $attributes) {
  453. $allow_sort = TRUE;
  454. // Identify likely filters and arguments for each column based on field type.
  455. switch ($attributes['type']) {
  456. case 'int':
  457. case 'mediumint':
  458. case 'tinyint':
  459. case 'bigint':
  460. case 'serial':
  461. case 'numeric':
  462. case 'float':
  463. $filter = 'numeric';
  464. $argument = 'numeric';
  465. $sort = 'standard';
  466. if ($field_storage->getType() == 'boolean') {
  467. $filter = 'boolean';
  468. }
  469. break;
  470. case 'text':
  471. case 'blob':
  472. // It does not make sense to sort by blob or text.
  473. $allow_sort = FALSE;
  474. default:
  475. $filter = 'string';
  476. $argument = 'string';
  477. $sort = 'standard';
  478. break;
  479. }
  480. if (count($field_columns) == 1 || $column == 'value') {
  481. $title = t('@label (@name)', array('@label' => $label, '@name' => $field_name));
  482. $title_short = $label;
  483. }
  484. else {
  485. $title = t('@label (@name:@column)', array('@label' => $label, '@name' => $field_name, '@column' => $column));
  486. $title_short = t('@label:@column', array('@label' => $label, '@column' => $column));
  487. }
  488. // Expose data for the property.
  489. foreach ($field_tables as $type => $table_info) {
  490. $table = $table_info['table'];
  491. $table_alias = $table_info['alias'];
  492. if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) {
  493. $group = $group_name;
  494. }
  495. else {
  496. $group = t('@group (historical data)', array('@group' => $group_name));
  497. }
  498. $column_real_name = $table_mapping->getFieldColumnName($field_storage, $column);
  499. // Load all the fields from the table by default.
  500. $additional_fields = $table_mapping->getAllColumns($table);
  501. $data[$table_alias][$column_real_name] = array(
  502. 'group' => $group,
  503. 'title' => $title,
  504. 'title short' => $title_short,
  505. 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  506. );
  507. // Go through and create a list of aliases for all possible combinations of
  508. // entity type + name.
  509. $aliases = array();
  510. $also_known = array();
  511. foreach ($all_labels as $label_name => $true) {
  512. if ($label != $label_name) {
  513. if (count($field_columns) == 1 || $column == 'value') {
  514. $alias_title = t('@label (@name)', array('@label' => $label_name, '@name' => $field_name));
  515. }
  516. else {
  517. $alias_title = t('@label (@name:@column)', array('@label' => $label_name, '@name' => $field_name, '@column' => $column));
  518. }
  519. $aliases[] = array(
  520. 'group' => $group_name,
  521. 'title' => $alias_title,
  522. 'help' => t('This is an alias of @group: @field.', array('@group' => $group_name, '@field' => $title)),
  523. );
  524. $also_known[] = t('@group: @field', array('@group' => $group_name, '@field' => $title));
  525. }
  526. }
  527. if ($aliases) {
  528. $data[$table_alias][$column_real_name]['aliases'] = $aliases;
  529. // The $also_known variable contains markup that is HTML escaped and
  530. // that loses safeness when imploded. The help text is used in
  531. // #description and therefore XSS admin filtered by default. Escaped
  532. // HTML is not altered by XSS filtering, therefore it is safe to just
  533. // concatenate the strings. Afterwards we mark the entire string as
  534. // safe, so it won't be escaped, no matter where it is used.
  535. // Considering the dual use of this help data (both as metadata and as
  536. // help text), other patterns such as use of #markup would not be
  537. // correct here.
  538. $data[$table_alias][$column_real_name]['help'] = Markup::create($data[$table_alias][$column_real_name]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known));
  539. }
  540. $data[$table_alias][$column_real_name]['argument'] = array(
  541. 'field' => $column_real_name,
  542. 'table' => $table,
  543. 'id' => $argument,
  544. 'additional fields' => $additional_fields,
  545. 'field_name' => $field_name,
  546. 'entity_type' => $entity_type_id,
  547. 'empty field name' => t('- No value -'),
  548. );
  549. $data[$table_alias][$column_real_name]['filter'] = array(
  550. 'field' => $column_real_name,
  551. 'table' => $table,
  552. 'id' => $filter,
  553. 'additional fields' => $additional_fields,
  554. 'field_name' => $field_name,
  555. 'entity_type' => $entity_type_id,
  556. 'allow empty' => TRUE,
  557. );
  558. if (!empty($allow_sort)) {
  559. $data[$table_alias][$column_real_name]['sort'] = array(
  560. 'field' => $column_real_name,
  561. 'table' => $table,
  562. 'id' => $sort,
  563. 'additional fields' => $additional_fields,
  564. 'field_name' => $field_name,
  565. 'entity_type' => $entity_type_id,
  566. );
  567. }
  568. // Set click sortable if there is a field definition.
  569. if (isset($data[$table_alias][$field_name]['field'])) {
  570. $data[$table_alias][$field_name]['field']['click sortable'] = $allow_sort;
  571. }
  572. // Expose additional delta column for multiple value fields.
  573. if ($field_storage->isMultiple()) {
  574. $title_delta = t('@label (@name:delta)', array('@label' => $label, '@name' => $field_name));
  575. $title_short_delta = t('@label:delta', array('@label' => $label));
  576. $data[$table_alias]['delta'] = array(
  577. 'group' => $group,
  578. 'title' => $title_delta,
  579. 'title short' => $title_short_delta,
  580. 'help' => t('Delta - Appears in: @bundles.', array('@bundles' => implode(', ', $bundles_names))),
  581. );
  582. $data[$table_alias]['delta']['field'] = array(
  583. 'id' => 'numeric',
  584. );
  585. $data[$table_alias]['delta']['argument'] = array(
  586. 'field' => 'delta',
  587. 'table' => $table,
  588. 'id' => 'numeric',
  589. 'additional fields' => $additional_fields,
  590. 'empty field name' => t('- No value -'),
  591. 'field_name' => $field_name,
  592. 'entity_type' => $entity_type_id,
  593. );
  594. $data[$table_alias]['delta']['filter'] = array(
  595. 'field' => 'delta',
  596. 'table' => $table,
  597. 'id' => 'numeric',
  598. 'additional fields' => $additional_fields,
  599. 'field_name' => $field_name,
  600. 'entity_type' => $entity_type_id,
  601. 'allow empty' => TRUE,
  602. );
  603. $data[$table_alias]['delta']['sort'] = array(
  604. 'field' => 'delta',
  605. 'table' => $table,
  606. 'id' => 'standard',
  607. 'additional fields' => $additional_fields,
  608. 'field_name' => $field_name,
  609. 'entity_type' => $entity_type_id,
  610. );
  611. }
  612. }
  613. }
  614. return $data;
  615. }
  616. /**
  617. * Implements hook_field_views_data().
  618. *
  619. * The function implements the hook in behalf of 'core' because it adds a
  620. * relationship and a reverse relationship to entity_reference field type, which
  621. * is provided by core.
  622. */
  623. function core_field_views_data(FieldStorageConfigInterface $field_storage) {
  624. $data = views_field_default_views_data($field_storage);
  625. // The code below only deals with the Entity reference field type.
  626. if ($field_storage->getType() != 'entity_reference') {
  627. return $data;
  628. }
  629. $entity_manager = \Drupal::entityManager();
  630. $entity_type_id = $field_storage->getTargetEntityTypeId();
  631. /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  632. $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
  633. foreach ($data as $table_name => $table_data) {
  634. // Add a relationship to the target entity type.
  635. $target_entity_type_id = $field_storage->getSetting('target_type');
  636. $target_entity_type = $entity_manager->getDefinition($target_entity_type_id);
  637. $entity_type_id = $field_storage->getTargetEntityTypeId();
  638. $entity_type = $entity_manager->getDefinition($entity_type_id);
  639. $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
  640. $field_name = $field_storage->getName();
  641. // Provide a relationship for the entity type with the entity reference
  642. // field.
  643. $args = array(
  644. '@label' => $target_entity_type->getLabel(),
  645. '@field_name' => $field_name,
  646. );
  647. $data[$table_name][$field_name]['relationship'] = array(
  648. 'title' => t('@label referenced from @field_name', $args),
  649. 'label' => t('@field_name: @label', $args),
  650. 'group' => $entity_type->getLabel(),
  651. 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $field_storage->getBundles()))),
  652. 'id' => 'standard',
  653. 'base' => $target_base_table,
  654. 'entity type' => $target_entity_type_id,
  655. 'base field' => $target_entity_type->getKey('id'),
  656. 'relationship field' => $field_name . '_target_id',
  657. );
  658. // Provide a reverse relationship for the entity type that is referenced by
  659. // the field.
  660. $args['@entity'] = $entity_type->getLabel();
  661. $args['@label'] = $target_entity_type->getLowercaseLabel();
  662. $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
  663. $data[$target_base_table][$pseudo_field_name]['relationship'] = array(
  664. 'title' => t('@entity using @field_name', $args),
  665. 'label' => t('@field_name', array('@field_name' => $field_name)),
  666. 'group' => $target_entity_type->getLabel(),
  667. 'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
  668. 'id' => 'entity_reverse',
  669. 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
  670. 'entity_type' => $entity_type_id,
  671. 'base field' => $entity_type->getKey('id'),
  672. 'field_name' => $field_name,
  673. 'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
  674. 'field field' => $field_name . '_target_id',
  675. 'join_extra' => array(
  676. array(
  677. 'field' => 'deleted',
  678. 'value' => 0,
  679. 'numeric' => TRUE,
  680. ),
  681. ),
  682. );
  683. }
  684. return $data;
  685. }