PageRenderTime 58ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/system/system.admin.inc

http://github.com/drupal/drupal
Pascal | 387 lines | 187 code | 16 blank | 184 comment | 21 complexity | ad2c01b838e90dc242b26a69512f2236 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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'] = array(
  49. '#type' => 'system_compact_link',
  50. );
  51. $variables['containers'] = array();
  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'][] = array(
  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'] = array(
  77. '#type' => 'system_compact_link',
  78. );
  79. $variables['containers'] = array();
  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][] = array(
  88. '#theme' => 'admin_block',
  89. '#block' => array(
  90. 'position' => $position,
  91. 'title' => $module,
  92. 'content' => array(
  93. '#theme' => 'admin_block_content',
  94. '#content' => $items,
  95. ),
  96. 'description' => t($description),
  97. ),
  98. );
  99. }
  100. }
  101. }
  102. /**
  103. * Prepares variables for status report template.
  104. *
  105. * Default template: status-report.html.twig.
  106. *
  107. * This theme function is dependent on install.inc being loaded, because
  108. * that's where the constants are defined.
  109. *
  110. * @param $variables
  111. * An associative array containing:
  112. * - requirements: An array of requirements/status items. Each requirement
  113. * is an associative array containing the following elements:
  114. * - title: The name of the requirement.
  115. * - value: (optional) The current value (version, time, level, etc).
  116. * - description: (optional) The description of the requirement.
  117. * - severity: (optional) The requirement's result/severity level, one of:
  118. * - REQUIREMENT_INFO: Status information.
  119. * - REQUIREMENT_OK: The requirement is satisfied.
  120. * - REQUIREMENT_WARNING: The requirement failed with a warning.
  121. * - REQUIREMENT_ERROR: The requirement failed with an error.
  122. */
  123. function template_preprocess_status_report(&$variables) {
  124. $severities = array(
  125. REQUIREMENT_INFO => array(
  126. 'title' => t('Info'),
  127. 'status' => 'info',
  128. ),
  129. REQUIREMENT_OK => array(
  130. 'title' => t('OK'),
  131. 'status' => 'ok',
  132. ),
  133. REQUIREMENT_WARNING => array(
  134. 'title' => t('Warning'),
  135. 'status' => 'warning',
  136. ),
  137. REQUIREMENT_ERROR => array(
  138. 'title' => t('Error'),
  139. 'status' => 'error',
  140. ),
  141. );
  142. foreach ($variables['requirements'] as $i => $requirement) {
  143. // Always use the explicit requirement severity, if defined. Otherwise,
  144. // default to REQUIREMENT_OK in the installer to visually confirm that
  145. // installation requirements are met. And default to REQUIREMENT_INFO to
  146. // denote neutral information without special visualization.
  147. if (isset($requirement['severity'])) {
  148. $severity = $severities[(int) $requirement['severity']];
  149. }
  150. elseif (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install') {
  151. $severity = $severities[REQUIREMENT_OK];
  152. }
  153. else {
  154. $severity = $severities[REQUIREMENT_INFO];
  155. }
  156. $variables['requirements'][$i]['severity_title'] = $severity['title'];
  157. $variables['requirements'][$i]['severity_status'] = $severity['status'];
  158. }
  159. }
  160. /**
  161. * Prepares variables for the module details templates.
  162. *
  163. * Default template: system-modules-details.html.twig.
  164. *
  165. * @param $variables
  166. * An associative array containing:
  167. * - form: A render element representing the form. The main form element
  168. * represents a package, and child elements of the form are individual
  169. * projects. Each project (or module) is an associative array containing the
  170. * following elements:
  171. * - name: The name of the module.
  172. * - enable: A checkbox for enabling the module.
  173. * - description: A description of the module.
  174. * - version: The version of the module.
  175. * - links: Administration links provided by the module.
  176. * - #requires: A list of modules that the project requires.
  177. * - #required_by: A list of modules that require the project.
  178. * - #attributes: A list of attributes for the module wrapper.
  179. *
  180. * @see \Drupal\system\Form\ModulesListForm
  181. */
  182. function template_preprocess_system_modules_details(&$variables) {
  183. $form = $variables['form'];
  184. $variables['modules'] = [];
  185. // Iterate through all the modules, which are children of this element.
  186. foreach (Element::children($form) as $key) {
  187. // Stick the key into $module for easier access.
  188. $module = $form[$key];
  189. unset($module['enable']['#title']);
  190. $module['#requires'] = array_filter($module['#requires']);
  191. $module['#required_by'] = array_filter($module['#required_by']);
  192. // Add the checkbox to allow installing new modules and to show the
  193. // installation status of the module.
  194. $module['checkbox'] = $module['enable'];
  195. // Add the module label and expand/collapse functionality.
  196. $id = Html::getUniqueId('module-' . $key);
  197. $module['id'] = $id;
  198. $module['enable_id'] = $module['enable']['#id'];
  199. // @todo Remove early rendering and use safe_join in the Twig template once
  200. // https://www.drupal.org/node/2579091 is fixed.
  201. $renderer = \Drupal::service('renderer');
  202. $machine_name_render = [
  203. '#prefix' => '<span dir="ltr" class="table-filter-text-source">',
  204. '#plain_text' => $key,
  205. '#suffix' => '</span>',
  206. ];
  207. $module['machine_name'] = $renderer->render($machine_name_render);
  208. if (!empty($module['#requires'])) {
  209. $requires = [
  210. '#theme' => 'item_list',
  211. '#items' => $module['#requires'],
  212. '#context' => ['list_style' => 'comma-list'],
  213. ];
  214. $module['requires'] = $renderer->render($requires);
  215. }
  216. if (!empty($module['#required_by'])) {
  217. $required_by = [
  218. '#theme' => 'item_list',
  219. '#items' => $module['#required_by'],
  220. '#context' => ['list_style' => 'comma-list'],
  221. ];
  222. $module['required_by'] = $renderer->render($required_by);
  223. }
  224. if (!empty($module['version'])) {
  225. $module['version'] = $renderer->render($module['version']);
  226. }
  227. $module['attributes'] = new Attribute($module['#attributes']);
  228. $variables['modules'][] = $module;
  229. }
  230. }
  231. /**
  232. * Prepares variables for module uninstall templates.
  233. *
  234. * Default template: system-modules-uninstall.html.twig.
  235. *
  236. * @param $variables
  237. * An associative array containing:
  238. * - form: A render element representing the form. Child elements of the form
  239. * are individual modules. Each module is an associative array containing
  240. * the following elements:
  241. * - #module_name: The name of the module as a string.
  242. * - name: The name of the module in a renderable array.
  243. * - description: A description of the module.
  244. * - #required_by: (optional) A list of modules that require the module.
  245. * - #validation_reasons: (optional) Additional reasons why the module
  246. * cannot be uninstalled.
  247. * - #attributes: A list of attributes for the module wrapper.
  248. *
  249. * @ingroup themeable
  250. */
  251. function template_preprocess_system_modules_uninstall(&$variables) {
  252. $form = $variables['form'];
  253. $variables['modules'] = [];
  254. // Iterate through all the modules, which are children of this element.
  255. foreach (Element::children($form['modules']) as $key) {
  256. $module = $form['modules'][$key];
  257. $module['module_name'] = $module['#module_name'];
  258. $module['checkbox'] = $form['uninstall'][$key];
  259. $module['checkbox_id'] = $form['uninstall'][$key]['#id'];
  260. if (!empty($module['#validation_reasons'])) {
  261. $module['validation_reasons'] = $module['#validation_reasons'];
  262. $module['reasons_count'] = count($module['validation_reasons']);
  263. }
  264. else {
  265. $module['reasons_count'] = 0;
  266. }
  267. if (!empty($module['#required_by'])) {
  268. $module['required_by'] = $module['#required_by'];
  269. $module['reasons_count'] = $module['reasons_count'] + 1;
  270. }
  271. $module['attributes'] = new Attribute($module['#attributes']);
  272. $variables['modules'][] = $module;
  273. }
  274. }
  275. /**
  276. * Prepares variables for appearance page templates.
  277. *
  278. * Default template: system-themes-page.html.twig.
  279. *
  280. * @param $variables
  281. * An associative array containing:
  282. * - theme_groups: An associative array containing groups of themes.
  283. * - theme_group_titles: An associative array containing titles of themes.
  284. */
  285. function template_preprocess_system_themes_page(&$variables) {
  286. $groups = array();
  287. $theme_groups = $variables['theme_groups'];
  288. $variables['attributes']['id'] = 'system-themes-page';
  289. foreach ($variables['theme_group_titles'] as $state => $title) {
  290. if (!count($theme_groups[$state])) {
  291. // Skip this group of themes if no theme is there.
  292. continue;
  293. }
  294. // Start new theme group.
  295. $theme_group = array();
  296. $theme_group['state'] = $state;
  297. $theme_group['title'] = $title;
  298. $theme_group['themes'] = array();
  299. $theme_group['attributes'] = new Attribute();
  300. foreach ($theme_groups[$state] as $theme) {
  301. $current_theme = array();
  302. // Screenshot depicting the theme.
  303. if ($theme->screenshot) {
  304. $current_theme['screenshot'] = array(
  305. '#theme' => 'image',
  306. '#uri' => $theme->screenshot['uri'],
  307. '#alt' => $theme->screenshot['alt'],
  308. '#title' => $theme->screenshot['title'],
  309. '#attributes' => $theme->screenshot['attributes'],
  310. );
  311. }
  312. else {
  313. $current_theme['screenshot'] = array(
  314. '#theme' => 'image',
  315. '#uri' => drupal_get_path('module', 'system') . '/images/no_screenshot.png',
  316. '#alt' => t('No screenshot'),
  317. '#title' => t('No screenshot'),
  318. '#attributes' => new Attribute(array('class' => array('no-screenshot'))),
  319. );
  320. }
  321. // Localize the theme description.
  322. $current_theme['description'] = t($theme->info['description']);
  323. $current_theme['attributes'] = new Attribute();
  324. $current_theme['name'] = $theme->info['name'];
  325. $current_theme['version'] = isset($theme->info['version']) ? $theme->info['version'] : '';
  326. $current_theme['notes'] = $theme->notes;
  327. $current_theme['is_default'] = $theme->is_default;
  328. $current_theme['is_admin'] = $theme->is_admin;
  329. // Make sure to provide feedback on compatibility.
  330. $current_theme['incompatible'] = '';
  331. if (!empty($theme->incompatible_core)) {
  332. $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]);
  333. }
  334. elseif (!empty($theme->incompatible_region)) {
  335. $current_theme['incompatible'] = t("This theme is missing a 'content' region.");
  336. }
  337. elseif (!empty($theme->incompatible_php)) {
  338. if (substr_count($theme->info['php'], '.') < 2) {
  339. $theme->info['php'] .= '.*';
  340. }
  341. $current_theme['incompatible'] = t('This theme requires PHP version @php_required and is incompatible with PHP version @php_version.', array('@php_required' => $theme->info['php'], '@php_version' => phpversion()));
  342. }
  343. elseif (!empty($theme->incompatible_base)) {
  344. $current_theme['incompatible'] = t('This theme requires the base theme @base_theme to operate correctly.', array('@base_theme' => $theme->info['base theme']));
  345. }
  346. elseif (!empty($theme->incompatible_engine)) {
  347. $current_theme['incompatible'] = t('This theme requires the theme engine @theme_engine to operate correctly.', array('@theme_engine' => $theme->info['engine']));
  348. }
  349. // Build operation links.
  350. $current_theme['operations'] = array(
  351. '#theme' => 'links',
  352. '#links' => $theme->operations,
  353. '#attributes' => array(
  354. 'class' => array('operations', 'clearfix'),
  355. ),
  356. );
  357. $theme_group['themes'][] = $current_theme;
  358. }
  359. $groups[] = $theme_group;
  360. }
  361. $variables['theme_groups'] = $groups;
  362. }