views.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Views.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function views_install() {
  10. module_set_weight('views', 10);
  11. }
  12. /**
  13. * @addtogroup updates-8.0.0-beta
  14. * @{
  15. */
  16. /**
  17. * Update views field plugins.
  18. */
  19. function views_update_8001(&$sandbox) {
  20. $config_factory = \Drupal::configFactory();
  21. $ids = [];
  22. $message = NULL;
  23. $ago_formats = [
  24. 'time ago',
  25. 'time hence',
  26. 'time span',
  27. 'raw time ago',
  28. 'raw time hence',
  29. 'raw time span',
  30. 'inverse time span',
  31. ];
  32. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  33. $view = $config_factory->getEditable($view_config_name);
  34. $displays = $view->get('display');
  35. foreach ($displays as $display_name => $display) {
  36. if (!empty($display['display_options']['fields'])) {
  37. foreach ($display['display_options']['fields'] as $field_name => $field) {
  38. if (isset($field['entity_type']) && $field['plugin_id'] === 'date') {
  39. $ids[] = $view->get('id');
  40. // Grab the settings we need to move to a different place in the
  41. // config schema.
  42. $date_format = !empty($field['date_format']) ? $field['date_format'] : 'medium';
  43. $custom_date_format = !empty($field['custom_date_format']) ? $field['custom_date_format'] : '';
  44. $timezone = !empty($field['timezone']) ? $field['timezone'] : '';
  45. // Save off the base part of the config path we are updating.
  46. $base = "display.$display_name.display_options.fields.$field_name";
  47. if (in_array($date_format, $ago_formats)) {
  48. // Update the field to use the Field API formatter.
  49. $view->set($base . '.plugin_id', 'field');
  50. $view->set($base . '.type', 'timestamp_ago');
  51. // Ensure the granularity is an integer, which is defined in the
  52. // field.formatter.settings.timestamp_ago schema.
  53. $granularity = is_numeric($custom_date_format) ? (int) $custom_date_format : 2;
  54. // Add the new settings.
  55. if ($date_format === 'time ago' || $date_format === 'time hence' || $date_format === 'time span') {
  56. $view->set($base . '.settings.future_format', '@interval hence');
  57. $view->set($base . '.settings.past_format', '@interval ago');
  58. $view->set($base . '.settings.granularity', $granularity);
  59. }
  60. elseif ($date_format === 'raw time ago' || $date_format === 'raw time hence') {
  61. $view->set($base . '.settings.future_format', '@interval');
  62. $view->set($base . '.settings.past_format', '@interval');
  63. $view->set($base . '.settings.granularity', $granularity);
  64. }
  65. elseif ($date_format === 'raw time span') {
  66. $view->set($base . '.settings.future_format', '@interval');
  67. $view->set($base . '.settings.past_format', '-@interval');
  68. $view->set($base . '.settings.granularity', $granularity);
  69. }
  70. elseif ($date_format === 'inverse time span') {
  71. $view->set($base . '.settings.future_format', '-@interval');
  72. $view->set($base . '.settings.past_format', '@interval');
  73. $view->set($base . '.settings.granularity', $granularity);
  74. }
  75. }
  76. else {
  77. // Update the field to use the Field API formatter.
  78. $view->set($base . '.plugin_id', 'field');
  79. $view->set($base . '.type', 'timestamp');
  80. // Add the new settings, and make sure everything is a string
  81. // to conform with the field.formatter.settings.timestamp schema.
  82. $view->set($base . '.settings.date_format', (string) $date_format);
  83. $view->set($base . '.settings.custom_date_format', (string) $custom_date_format);
  84. $view->set($base . '.settings.timezone', (string) $timezone);
  85. }
  86. // Remove the old settings.
  87. $view->clear($base . '.date_format');
  88. $view->clear($base . '.custom_date_format');
  89. $view->clear($base . '.timezone');
  90. }
  91. }
  92. }
  93. }
  94. $view->save(TRUE);
  95. }
  96. if (!empty($ids)) {
  97. $message = \Drupal::translation()->translate('Updated field plugins for views: @ids', ['@ids' => implode(', ', array_unique($ids))]);
  98. }
  99. return $message;
  100. }
  101. /**
  102. * Updates %1 and !1 tokens to argument tokens.
  103. */
  104. function views_update_8002() {
  105. $config_factory = \Drupal::configFactory();
  106. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  107. $view = $config_factory->getEditable($view_config_name);
  108. $displays = $view->get('display');
  109. $argument_map_per_display = _views_update_argument_map($displays);
  110. $changed = FALSE;
  111. // Update all the field settings, which support tokens.
  112. foreach ($displays as $display_name => &$display) {
  113. if (!empty($display['display_options']['fields'])) {
  114. $token_values = [
  115. 'path',
  116. 'alt',
  117. 'link_class',
  118. 'rel',
  119. 'target',
  120. 'query',
  121. 'fragment',
  122. 'prefix',
  123. 'suffix',
  124. 'more_link_text',
  125. 'more_link_path',
  126. 'link_attributes',
  127. 'text',
  128. ];
  129. foreach ($display['display_options']['fields'] as $field_name => &$field) {
  130. foreach ($token_values as $token_name) {
  131. if (!empty($field['alter'][$token_name])) {
  132. if (is_array($field['alter'][$token_name])) {
  133. foreach (array_keys($field['alter'][$token_name]) as $key) {
  134. $field['alter'][$token_name][$key] = _views_update_8002_token_update($field['alter'][$token_name][$key], $argument_map_per_display[$display_name]);
  135. $changed = TRUE;
  136. }
  137. }
  138. else {
  139. $field['alter'][$token_name] = _views_update_8002_token_update($field['alter'][$token_name], $argument_map_per_display[$display_name]);
  140. $changed = TRUE;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. // Update the area handlers with tokens.
  148. foreach ($displays as $display_name => &$display) {
  149. $area_types = ['header', 'footer', 'empty'];
  150. foreach ($area_types as $area_type) {
  151. if (!empty($display['display_options'][$area_type])) {
  152. foreach ($display['display_options'][$area_type] as &$area) {
  153. switch ($area['plugin_id']) {
  154. case 'title':
  155. $area['title'] = _views_update_8002_token_update($area['title'], $argument_map_per_display[$display_name]);
  156. $changed = TRUE;
  157. break;
  158. case 'result':
  159. $area['content'] = _views_update_8002_token_update($area['content'], $argument_map_per_display[$display_name]);
  160. $changed = TRUE;
  161. break;
  162. case 'text':
  163. $area['content']['value'] = _views_update_8002_token_update($area['content']['value'], $argument_map_per_display[$display_name]);
  164. $changed = TRUE;
  165. break;
  166. case 'text_custom':
  167. $area['content'] = _views_update_8002_token_update($area['content'], $argument_map_per_display[$display_name]);
  168. $changed = TRUE;
  169. break;
  170. case 'entity':
  171. $area['target'] = _views_update_8002_token_update($area['target'], $argument_map_per_display[$display_name]);
  172. $changed = TRUE;
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. // Update the argument title settings.
  180. foreach ($displays as $display_name => &$display) {
  181. if (!empty($display['display_options']['arguments'])) {
  182. foreach ($display['display_options']['arguments'] as &$argument) {
  183. if (isset($argument['exception']['title'])) {
  184. $argument['exception']['title'] = _views_update_8002_token_update($argument['exception']['title'], $argument_map_per_display[$display_name]);
  185. $changed = TRUE;
  186. }
  187. if (isset($argument['title'])) {
  188. $argument['title'] = _views_update_8002_token_update($argument['title'], $argument_map_per_display[$display_name]);
  189. $changed = TRUE;
  190. }
  191. }
  192. }
  193. }
  194. // Update the display title settings.
  195. // Update the more link text and more link URL.
  196. foreach ($displays as $display_name => &$display) {
  197. if (!empty($display['display_options']['title'])) {
  198. $display['display_options']['title'] = _views_update_8002_token_update($display['display_options']['title'], $argument_map_per_display[$display_name]);
  199. $changed = TRUE;
  200. }
  201. if (!empty($display['display_options']['use_more_text'])) {
  202. $display['display_options']['use_more_text'] = _views_update_8002_token_update($display['display_options']['use_more_text'], $argument_map_per_display[$display_name]);
  203. $changed = TRUE;
  204. }
  205. if (!empty($display['display_options']['link_url'])) {
  206. $display['display_options']['link_url'] = _views_update_8002_token_update($display['display_options']['link_url'], $argument_map_per_display[$display_name]);
  207. $changed = TRUE;
  208. }
  209. }
  210. // Update custom classes for row class + grid classes.
  211. // Update RSS description field.
  212. foreach ($displays as $display_name => &$display) {
  213. if (!empty($display['display_options']['style'])) {
  214. if (!empty($display['display_options']['style']['options']['row_class_custom'])) {
  215. $display['display_options']['style']['options']['row_class_custom'] = _views_update_8002_token_update($display['display_options']['style']['options']['row_class_custom'], $argument_map_per_display[$display_name]);
  216. $changed = TRUE;
  217. }
  218. if (!empty($display['display_options']['style']['options']['col_class_custom'])) {
  219. $display['display_options']['style']['options']['col_class_custom'] = _views_update_8002_token_update($display['display_options']['style']['options']['col_class_custom'], $argument_map_per_display[$display_name]);
  220. $changed = TRUE;
  221. }
  222. if (!empty($display['display_options']['style']['options']['description'])) {
  223. $display['display_options']['style']['options']['description'] = _views_update_8002_token_update($display['display_options']['style']['options']['description'], $argument_map_per_display[$display_name]);
  224. $changed = TRUE;
  225. }
  226. }
  227. }
  228. if ($changed) {
  229. $view->set('display', $displays);
  230. $view->save(TRUE);
  231. }
  232. }
  233. }
  234. /**
  235. * Updates a views configuration string from using %/! to twig tokens.
  236. *
  237. * @param string $text
  238. * Text in which to search for argument tokens and replace them with their
  239. * twig representation.
  240. * @param array $argument_map
  241. * A map of argument machine names keyed by their previous index.
  242. *
  243. * @return string
  244. * The updated token.
  245. */
  246. function _views_update_8002_token_update($text, array $argument_map) {
  247. $text = preg_replace_callback('/%(\d)/', function ($match) use ($argument_map) {
  248. return "{{ arguments.{$argument_map[$match[1]]} }}";
  249. }, $text);
  250. $text = preg_replace_callback('/!(\d)/', function ($match) use ($argument_map) {
  251. return "{{ raw_arguments.{$argument_map[$match[1]]} }}";
  252. }, $text);
  253. return $text;
  254. }
  255. /**
  256. * Builds an argument map for each Views display.
  257. *
  258. * @param array $displays
  259. * A list of Views displays.
  260. *
  261. * @return array
  262. * The argument map keyed by display id.
  263. */
  264. function _views_update_argument_map($displays) {
  265. $argument_map = [];
  266. foreach ($displays as $display_id => $display) {
  267. $argument_map[$display_id] = [];
  268. if (isset($display['display_options']['arguments'])) {
  269. foreach (array_keys($display['display_options']['arguments']) as $number => $name) {
  270. $argument_map[$display_id][$number + 1] = $name;
  271. }
  272. }
  273. elseif (isset($displays['default']['display_options']['arguments'])) {
  274. foreach (array_keys($displays['default']['display_options']['arguments']) as $number => $name) {
  275. $argument_map[$display_id][$number + 1] = $name;
  276. }
  277. }
  278. }
  279. return $argument_map;
  280. }
  281. /**
  282. * @} End of "addtogroup updates-8.0.0-beta".
  283. */
  284. /**
  285. * @addtogroup updates-8.0.0-rc
  286. * @{
  287. */
  288. /**
  289. * Clear caches to fix entity operations field.
  290. */
  291. function views_update_8003() {
  292. // Empty update to cause a cache flush so that views data is rebuilt. Entity
  293. // types that don't implement a list builder cannot have the entity operations
  294. // field.
  295. }
  296. /**
  297. * @} End of "addtogroup updates-8.0.0-rc".
  298. */
  299. /**
  300. * @addtogroup updates-8.0.x
  301. * @{
  302. */
  303. /**
  304. * Clear caches due to updated entity views data.
  305. */
  306. function views_update_8004() {
  307. // Empty update to cause a cache flush so that views data is rebuilt.
  308. }
  309. /**
  310. * @} End of "addtogroup updates-8.0.x".
  311. */
  312. /**
  313. * @addtogroup updates-8.1.0
  314. * @{
  315. */
  316. /**
  317. * Clear views data cache.
  318. */
  319. function views_update_8005() {
  320. // Empty update function to rebuild the views data.
  321. }
  322. /**
  323. * Clear caches due to updated entity views data.
  324. */
  325. function views_update_8100() {
  326. // Empty update to cause a cache flush so that views data is rebuilt.
  327. }
  328. /**
  329. * Set default values for enabled/expanded flag on page displays.
  330. */
  331. function views_update_8101() {
  332. $config_factory = \Drupal::configFactory();
  333. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  334. $view = $config_factory->getEditable($view_config_name);
  335. $save = FALSE;
  336. foreach ($view->get('display') as $display_id => $display) {
  337. if ($display['display_plugin'] == 'page') {
  338. $display['display_options']['menu']['enabled'] = TRUE;
  339. $display['display_options']['menu']['expanded'] = FALSE;
  340. $view->set("display.$display_id", $display);
  341. $save = TRUE;
  342. }
  343. }
  344. if ($save) {
  345. $view->save();
  346. }
  347. }
  348. }
  349. /**
  350. * @} End of "addtogroup updates-8.1.0".
  351. */