system.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the system module.
  5. */
  6. use Drupal\Component\Utility\Html;
  7. use Drupal\Core\Render\Element;
  8. use Drupal\Core\Template\Attribute;
  9. /**
  10. * Prepares variables for administrative content block templates.
  11. *
  12. * Default template: admin-block-content.html.twig.
  13. *
  14. * @param $variables
  15. * An associative array containing:
  16. * - content: An array containing information about the block. Each element
  17. * of the array represents an administrative menu item, and must at least
  18. * contain the keys 'title', 'link_path', and 'localized_options', which are
  19. * passed to l(). A 'description' key may also be provided.
  20. */
  21. function template_preprocess_admin_block_content(&$variables) {
  22. if (!empty($variables['content'])) {
  23. $variables['compact'] = system_admin_compact_mode();
  24. foreach ($variables['content'] as $key => $item) {
  25. $variables['content'][$key]['link'] = \Drupal::l($item['title'], $item['url']);
  26. if (!$variables['compact'] && isset($item['description'])) {
  27. $variables['content'][$key]['description'] = ['#markup' => $item['description']];
  28. }
  29. else {
  30. $variables['content'][$key]['description'] = FALSE;
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * Prepares variables for administrative index page templates.
  37. *
  38. * Default template: admin-page.html.twig.
  39. *
  40. * @param $variables
  41. * An associative array containing:
  42. * - blocks: An array of blocks to display. Each array should include a
  43. * 'title', a 'description', a formatted 'content' and a 'position' which
  44. * will control which container it will be in. This is usually 'left' or
  45. * 'right'.
  46. */
  47. function template_preprocess_admin_page(&$variables) {
  48. $variables['system_compact_link'] = [
  49. '#type' => 'system_compact_link',
  50. ];
  51. $variables['containers'] = [];
  52. $stripe = 0;
  53. foreach ($variables['blocks'] as $block) {
  54. if (!empty($block['content']['#content'])) {
  55. if (empty($block['position'])) {
  56. // Perform automatic striping.
  57. $block['position'] = ++$stripe % 2 ? 'left' : 'right';
  58. }
  59. $variables['containers'][$block['position']]['blocks'][] = [
  60. '#theme' => 'admin_block',
  61. '#block' => $block,
  62. ];
  63. }
  64. }
  65. }
  66. /**
  67. * Prepares variables for admin index templates.
  68. *
  69. * Default template: system-admin-index.html.twig.
  70. *
  71. * @param $variables
  72. * An associative array containing:
  73. * - menu_items: An array of modules to be displayed.
  74. */
  75. function template_preprocess_system_admin_index(&$variables) {
  76. $variables['system_compact_link'] = [
  77. '#type' => 'system_compact_link',
  78. ];
  79. $variables['containers'] = [];
  80. $stripe = 0;
  81. // Iterate over all modules.
  82. foreach ($variables['menu_items'] as $module => $block) {
  83. list($description, $items) = $block;
  84. $position = ++$stripe % 2 ? 'left' : 'right';
  85. // Output links.
  86. if (count($items)) {
  87. $variables['containers'][$position][] = [
  88. '#theme' => 'admin_block',
  89. '#block' => [
  90. 'position' => $position,
  91. 'title' => $module,
  92. 'content' => [
  93. '#theme' => 'admin_block_content',
  94. '#content' => $items,
  95. ],
  96. 'description' => t($description),
  97. ],
  98. ];
  99. }
  100. }
  101. }
  102. /**
  103. * Prepares variables for the module details templates.
  104. *
  105. * Default template: system-modules-details.html.twig.
  106. *
  107. * @param $variables
  108. * An associative array containing:
  109. * - form: A render element representing the form. The main form element
  110. * represents a package, and child elements of the form are individual
  111. * projects. Each project (or module) is an associative array containing the
  112. * following elements:
  113. * - name: The name of the module.
  114. * - enable: A checkbox for enabling the module.
  115. * - description: A description of the module.
  116. * - version: The version of the module.
  117. * - links: Administration links provided by the module.
  118. * - #requires: A list of modules that the project requires.
  119. * - #required_by: A list of modules that require the project.
  120. * - #attributes: A list of attributes for the module wrapper.
  121. *
  122. * @see \Drupal\system\Form\ModulesListForm
  123. */
  124. function template_preprocess_system_modules_details(&$variables) {
  125. $form = $variables['form'];
  126. $variables['modules'] = [];
  127. // Iterate through all the modules, which are children of this element.
  128. foreach (Element::children($form) as $key) {
  129. // Stick the key into $module for easier access.
  130. $module = $form[$key];
  131. unset($module['enable']['#title']);
  132. $module['#requires'] = array_filter($module['#requires']);
  133. $module['#required_by'] = array_filter($module['#required_by']);
  134. // Add the checkbox to allow installing new modules and to show the
  135. // installation status of the module.
  136. $module['checkbox'] = $module['enable'];
  137. // Add the module label and expand/collapse functionality.
  138. $id = Html::getUniqueId('module-' . $key);
  139. $module['id'] = $id;
  140. $module['enable_id'] = $module['enable']['#id'];
  141. // @todo Remove early rendering and use safe_join in the Twig template once
  142. // https://www.drupal.org/node/2579091 is fixed.
  143. $renderer = \Drupal::service('renderer');
  144. $machine_name_render = [
  145. '#prefix' => '<span dir="ltr" class="table-filter-text-source">',
  146. '#plain_text' => $key,
  147. '#suffix' => '</span>',
  148. ];
  149. $module['machine_name'] = $renderer->render($machine_name_render);
  150. if (!empty($module['#requires'])) {
  151. $requires = [
  152. '#theme' => 'item_list',
  153. '#items' => $module['#requires'],
  154. '#context' => ['list_style' => 'comma-list'],
  155. ];
  156. $module['requires'] = $renderer->render($requires);
  157. }
  158. if (!empty($module['#required_by'])) {
  159. $required_by = [
  160. '#theme' => 'item_list',
  161. '#items' => $module['#required_by'],
  162. '#context' => ['list_style' => 'comma-list'],
  163. ];
  164. $module['required_by'] = $renderer->render($required_by);
  165. }
  166. if (!empty($module['version'])) {
  167. $module['version'] = $renderer->render($module['version']);
  168. }
  169. $module['attributes'] = new Attribute($module['#attributes']);
  170. $variables['modules'][] = $module;
  171. }
  172. }
  173. /**
  174. * Prepares variables for module uninstall templates.
  175. *
  176. * Default template: system-modules-uninstall.html.twig.
  177. *
  178. * @param $variables
  179. * An associative array containing:
  180. * - form: A render element representing the form. Child elements of the form
  181. * are individual modules. Each module is an associative array containing
  182. * the following elements:
  183. * - #module_name: The name of the module as a string.
  184. * - name: The name of the module in a renderable array.
  185. * - description: A description of the module.
  186. * - #required_by: (optional) A list of modules that require the module.
  187. * - #validation_reasons: (optional) Additional reasons why the module
  188. * cannot be uninstalled.
  189. * - #attributes: A list of attributes for the module wrapper.
  190. *
  191. * @ingroup themeable
  192. */
  193. function template_preprocess_system_modules_uninstall(&$variables) {
  194. $form = $variables['form'];
  195. $variables['modules'] = [];
  196. // Iterate through all the modules, which are children of this element.
  197. foreach (Element::children($form['modules']) as $key) {
  198. $module = $form['modules'][$key];
  199. $module['module_name'] = $module['#module_name'];
  200. $module['checkbox'] = $form['uninstall'][$key];
  201. $module['checkbox_id'] = $form['uninstall'][$key]['#id'];
  202. if (!empty($module['#validation_reasons'])) {
  203. $module['validation_reasons'] = $module['#validation_reasons'];
  204. $module['reasons_count'] = count($module['validation_reasons']);
  205. }
  206. else {
  207. $module['reasons_count'] = 0;
  208. }
  209. if (!empty($module['#required_by'])) {
  210. $module['required_by'] = $module['#required_by'];
  211. $module['reasons_count'] = $module['reasons_count'] + 1;
  212. }
  213. $module['attributes'] = new Attribute($module['#attributes']);
  214. $variables['modules'][] = $module;
  215. }
  216. }
  217. /**
  218. * Prepares variables for appearance page templates.
  219. *
  220. * Default template: system-themes-page.html.twig.
  221. *
  222. * @param $variables
  223. * An associative array containing:
  224. * - theme_groups: An associative array containing groups of themes.
  225. * - theme_group_titles: An associative array containing titles of themes.
  226. */
  227. function template_preprocess_system_themes_page(&$variables) {
  228. $groups = [];
  229. $theme_groups = $variables['theme_groups'];
  230. $variables['attributes']['id'] = 'system-themes-page';
  231. foreach ($variables['theme_group_titles'] as $state => $title) {
  232. if (!count($theme_groups[$state])) {
  233. // Skip this group of themes if no theme is there.
  234. continue;
  235. }
  236. // Start new theme group.
  237. $theme_group = [];
  238. $theme_group['state'] = $state;
  239. $theme_group['title'] = $title;
  240. $theme_group['themes'] = [];
  241. $theme_group['attributes'] = new Attribute();
  242. foreach ($theme_groups[$state] as $theme) {
  243. $current_theme = [];
  244. // Screenshot depicting the theme.
  245. if ($theme->screenshot) {
  246. $current_theme['screenshot'] = [
  247. '#theme' => 'image',
  248. '#uri' => $theme->screenshot['uri'],
  249. '#alt' => $theme->screenshot['alt'],
  250. '#title' => $theme->screenshot['title'],
  251. '#attributes' => $theme->screenshot['attributes'],
  252. ];
  253. }
  254. else {
  255. $current_theme['screenshot'] = [
  256. '#theme' => 'image',
  257. '#uri' => drupal_get_path('module', 'system') . '/images/no_screenshot.png',
  258. '#alt' => t('No screenshot'),
  259. '#title' => t('No screenshot'),
  260. '#attributes' => new Attribute(['class' => ['no-screenshot']]),
  261. ];
  262. }
  263. // Localize the theme description.
  264. $current_theme['description'] = t($theme->info['description']);
  265. $current_theme['attributes'] = new Attribute();
  266. $current_theme['name'] = $theme->info['name'];
  267. $current_theme['version'] = isset($theme->info['version']) ? $theme->info['version'] : '';
  268. $current_theme['notes'] = $theme->notes;
  269. $current_theme['is_default'] = $theme->is_default;
  270. $current_theme['is_admin'] = $theme->is_admin;
  271. // Make sure to provide feedback on compatibility.
  272. $current_theme['incompatible'] = '';
  273. if (!empty($theme->incompatible_core)) {
  274. $current_theme['incompatible'] = t("This theme is not compatible with Drupal @core_version. Check that the .info.yml file contains the correct 'core' value.", ['@core_version' => \Drupal::CORE_COMPATIBILITY]);
  275. }
  276. elseif (!empty($theme->incompatible_region)) {
  277. $current_theme['incompatible'] = t("This theme is missing a 'content' region.");
  278. }
  279. elseif (!empty($theme->incompatible_php)) {
  280. if (substr_count($theme->info['php'], '.') < 2) {
  281. $theme->info['php'] .= '.*';
  282. }
  283. $current_theme['incompatible'] = t('This theme requires PHP version @php_required and is incompatible with PHP version @php_version.', ['@php_required' => $theme->info['php'], '@php_version' => phpversion()]);
  284. }
  285. elseif (!empty($theme->incompatible_base)) {
  286. $current_theme['incompatible'] = t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => $theme->info['base theme']]);
  287. }
  288. elseif (!empty($theme->incompatible_engine)) {
  289. $current_theme['incompatible'] = t('This theme requires the theme engine @theme_engine to operate correctly.', ['@theme_engine' => $theme->info['engine']]);
  290. }
  291. // Build operation links.
  292. $current_theme['operations'] = [
  293. '#theme' => 'links',
  294. '#links' => $theme->operations,
  295. '#attributes' => [
  296. 'class' => ['operations', 'clearfix'],
  297. ],
  298. ];
  299. $theme_group['themes'][] = $current_theme;
  300. }
  301. $groups[] = $theme_group;
  302. }
  303. $variables['theme_groups'] = $groups;
  304. }