ViewsDataHelper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Drupal\views;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. /**
  5. * Defines a helper class for stuff related to views data.
  6. */
  7. class ViewsDataHelper {
  8. /**
  9. * The views data object, containing the cached information.
  10. *
  11. * @var \Drupal\views\ViewsData
  12. */
  13. protected $data;
  14. /**
  15. * A prepared list of all fields, keyed by base_table and handler type.
  16. *
  17. * @var array
  18. */
  19. protected $fields;
  20. /**
  21. * Constructs a ViewsData object.
  22. *
  23. * @param \Drupal\views\ViewsData $views_data
  24. * The views data object, containing the cached table information.
  25. */
  26. public function __construct(ViewsData $views_data) {
  27. $this->data = $views_data;
  28. }
  29. /**
  30. * Fetches a list of all fields available for a given base type.
  31. *
  32. * @param array|string $base
  33. * A list or a single base_table, for example node.
  34. * @param string $type
  35. * The handler type, for example field or filter.
  36. * @param bool $grouping
  37. * Should the result grouping by its 'group' label.
  38. * @param string $sub_type
  39. * An optional sub type. E.g. Allows making an area plugin available for
  40. * header only, instead of header, footer, and empty regions.
  41. *
  42. * @return array
  43. * A keyed array of in the form of 'base_table' => 'Description'.
  44. */
  45. public function fetchFields($base, $type, $grouping = FALSE, $sub_type = NULL) {
  46. if (!$this->fields) {
  47. $data = $this->data->get();
  48. // This constructs this ginormous multi dimensional array to
  49. // collect the important data about fields. In the end,
  50. // the structure looks a bit like this (using nid as an example)
  51. // $strings['nid']['filter']['title'] = 'string'.
  52. //
  53. // This is constructed this way because the above referenced strings
  54. // can appear in different places in the actual data structure so that
  55. // the data doesn't have to be repeated a lot. This essentially lets
  56. // each field have a cheap kind of inheritance.
  57. foreach ($data as $table => $table_data) {
  58. $bases = [];
  59. $strings = [];
  60. $skip_bases = [];
  61. foreach ($table_data as $field => $info) {
  62. // Collect table data from this table
  63. if ($field == 'table') {
  64. // calculate what tables this table can join to.
  65. if (!empty($info['join'])) {
  66. $bases = array_keys($info['join']);
  67. }
  68. // And it obviously joins to itself.
  69. $bases[] = $table;
  70. continue;
  71. }
  72. foreach (['field', 'sort', 'filter', 'argument', 'relationship', 'area'] as $key) {
  73. if (!empty($info[$key])) {
  74. if ($grouping && !empty($info[$key]['no group by'])) {
  75. continue;
  76. }
  77. if ($sub_type && isset($info[$key]['sub_type']) && (!in_array($sub_type, (array) $info[$key]['sub_type']))) {
  78. continue;
  79. }
  80. if (!empty($info[$key]['skip base'])) {
  81. foreach ((array) $info[$key]['skip base'] as $base_name) {
  82. $skip_bases[$field][$key][$base_name] = TRUE;
  83. }
  84. }
  85. elseif (!empty($info['skip base'])) {
  86. foreach ((array) $info['skip base'] as $base_name) {
  87. $skip_bases[$field][$key][$base_name] = TRUE;
  88. }
  89. }
  90. foreach (['title', 'group', 'help', 'base', 'aliases'] as $string) {
  91. // First, try the lowest possible level
  92. if (!empty($info[$key][$string])) {
  93. $strings[$field][$key][$string] = $info[$key][$string];
  94. }
  95. // Then try the field level
  96. elseif (!empty($info[$string])) {
  97. $strings[$field][$key][$string] = $info[$string];
  98. }
  99. // Finally, try the table level
  100. elseif (!empty($table_data['table'][$string])) {
  101. $strings[$field][$key][$string] = $table_data['table'][$string];
  102. }
  103. // We don't have any help provided for this field. If a better
  104. // description should be used for the Views UI you use
  105. // hook_views_data_alter() in module.views.inc or implement a
  106. // custom entity views_data handler.
  107. // @see hook_views_data_alter()
  108. // @see \Drupal\node\NodeViewsData
  109. elseif ($string == 'help') {
  110. $strings[$field][$key][$string] = '';
  111. }
  112. else {
  113. if ($string != 'base') {
  114. $strings[$field][$key][$string] = new FormattableMarkup("Error: missing @component", ['@component' => $string]);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. foreach ($bases as $base_name) {
  122. foreach ($strings as $field => $field_strings) {
  123. foreach ($field_strings as $type_name => $type_strings) {
  124. if (empty($skip_bases[$field][$type_name][$base_name])) {
  125. $this->fields[$base_name][$type_name]["$table.$field"] = $type_strings;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. // If we have an array of base tables available, go through them
  133. // all and add them together. Duplicate keys will be lost and that's
  134. // Just Fine.
  135. if (is_array($base)) {
  136. $strings = [];
  137. foreach ($base as $base_table) {
  138. if (isset($this->fields[$base_table][$type])) {
  139. $strings += $this->fields[$base_table][$type];
  140. }
  141. }
  142. uasort($strings, ['self', 'fetchedFieldSort']);
  143. return $strings;
  144. }
  145. if (isset($this->fields[$base][$type])) {
  146. uasort($this->fields[$base][$type], [$this, 'fetchedFieldSort']);
  147. return $this->fields[$base][$type];
  148. }
  149. return [];
  150. }
  151. /**
  152. * Sort function for fetched fields.
  153. *
  154. * @param array $a
  155. * First item for comparison. The compared items should be associative arrays
  156. * that include a 'group' and a 'title' key.
  157. * @param array $b
  158. * Second item for comparison.
  159. *
  160. * @return int
  161. * Returns -1 if $a comes before $b, 1 other way round and 0 if it cannot be
  162. * decided.
  163. */
  164. protected static function fetchedFieldSort($a, $b) {
  165. $a_group = mb_strtolower($a['group']);
  166. $b_group = mb_strtolower($b['group']);
  167. if ($a_group != $b_group) {
  168. return $a_group < $b_group ? -1 : 1;
  169. }
  170. $a_title = mb_strtolower($a['title']);
  171. $b_title = mb_strtolower($b['title']);
  172. if ($a_title != $b_title) {
  173. return $a_title < $b_title ? -1 : 1;
  174. }
  175. return 0;
  176. }
  177. }