PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Controller/SystemController.php

http://github.com/drupal/drupal
PHP | 330 lines | 211 code | 29 blank | 90 comment | 37 complexity | 333810c52f982acb90dbfcf53f641063 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\system\Controller;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Controller\ControllerBase;
  5. use Drupal\Core\Extension\ThemeHandlerInterface;
  6. use Drupal\Core\Form\FormBuilderInterface;
  7. use Drupal\Core\Menu\MenuLinkTreeInterface;
  8. use Drupal\Core\Menu\MenuTreeParameters;
  9. use Drupal\Core\Theme\ThemeAccessCheck;
  10. use Drupal\Core\Url;
  11. use Drupal\system\SystemManager;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Returns responses for System routes.
  15. */
  16. class SystemController extends ControllerBase {
  17. /**
  18. * System Manager Service.
  19. *
  20. * @var \Drupal\system\SystemManager
  21. */
  22. protected $systemManager;
  23. /**
  24. * The theme access checker service.
  25. *
  26. * @var \Drupal\Core\Theme\ThemeAccessCheck
  27. */
  28. protected $themeAccess;
  29. /**
  30. * The form builder service.
  31. *
  32. * @var \Drupal\Core\Form\FormBuilderInterface
  33. */
  34. protected $formBuilder;
  35. /**
  36. * The theme handler service.
  37. *
  38. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  39. */
  40. protected $themeHandler;
  41. /**
  42. * The menu link tree service.
  43. *
  44. * @var \Drupal\Core\Menu\MenuLinkTreeInterface
  45. */
  46. protected $menuLinkTree;
  47. /**
  48. * Constructs a new SystemController.
  49. *
  50. * @param \Drupal\system\SystemManager $systemManager
  51. * System manager service.
  52. * @param \Drupal\Core\Theme\ThemeAccessCheck $theme_access
  53. * The theme access checker service.
  54. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
  55. * The form builder.
  56. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  57. * The theme handler.
  58. * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree
  59. * The menu link tree service.
  60. */
  61. public function __construct(SystemManager $systemManager, ThemeAccessCheck $theme_access, FormBuilderInterface $form_builder, ThemeHandlerInterface $theme_handler, MenuLinkTreeInterface $menu_link_tree) {
  62. $this->systemManager = $systemManager;
  63. $this->themeAccess = $theme_access;
  64. $this->formBuilder = $form_builder;
  65. $this->themeHandler = $theme_handler;
  66. $this->menuLinkTree = $menu_link_tree;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public static function create(ContainerInterface $container) {
  72. return new static(
  73. $container->get('system.manager'),
  74. $container->get('access_check.theme'),
  75. $container->get('form_builder'),
  76. $container->get('theme_handler'),
  77. $container->get('menu.link_tree')
  78. );
  79. }
  80. /**
  81. * Provide the administration overview page.
  82. *
  83. * @param string $link_id
  84. * The ID of the administrative path link for which to display child links.
  85. *
  86. * @return array
  87. * A renderable array of the administration overview page.
  88. */
  89. public function overview($link_id) {
  90. // Check for status report errors.
  91. if ($this->systemManager->checkRequirements() && $this->currentUser()->hasPermission('administer site configuration')) {
  92. $this->messenger()->addError($this->t('One or more problems were detected with your Drupal installation. Check the <a href=":status">status report</a> for more information.', [':status' => Url::fromRoute('system.status')->toString()]));
  93. }
  94. // Load all menu links below it.
  95. $parameters = new MenuTreeParameters();
  96. $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
  97. $tree = $this->menuLinkTree->load(NULL, $parameters);
  98. $manipulators = [
  99. ['callable' => 'menu.default_tree_manipulators:checkAccess'],
  100. ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
  101. ];
  102. $tree = $this->menuLinkTree->transform($tree, $manipulators);
  103. $tree_access_cacheability = new CacheableMetadata();
  104. $blocks = [];
  105. foreach ($tree as $key => $element) {
  106. $tree_access_cacheability = $tree_access_cacheability->merge(CacheableMetadata::createFromObject($element->access));
  107. // Only render accessible links.
  108. if (!$element->access->isAllowed()) {
  109. continue;
  110. }
  111. $link = $element->link;
  112. $block['title'] = $link->getTitle();
  113. $block['description'] = $link->getDescription();
  114. $block['content'] = [
  115. '#theme' => 'admin_block_content',
  116. '#content' => $this->systemManager->getAdminBlock($link),
  117. ];
  118. if (!empty($block['content']['#content'])) {
  119. $blocks[$key] = $block;
  120. }
  121. }
  122. if ($blocks) {
  123. ksort($blocks);
  124. $build = [
  125. '#theme' => 'admin_page',
  126. '#blocks' => $blocks,
  127. ];
  128. $tree_access_cacheability->applyTo($build);
  129. return $build;
  130. }
  131. else {
  132. $build = [
  133. '#markup' => $this->t('You do not have any administrative items.'),
  134. ];
  135. $tree_access_cacheability->applyTo($build);
  136. return $build;
  137. }
  138. }
  139. /**
  140. * Sets whether the admin menu is in compact mode or not.
  141. *
  142. * @param string $mode
  143. * Valid values are 'on' and 'off'.
  144. *
  145. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  146. */
  147. public function compactPage($mode) {
  148. user_cookie_save(['admin_compact_mode' => ($mode == 'on')]);
  149. return $this->redirect('<front>');
  150. }
  151. /**
  152. * Provides a single block from the administration menu as a page.
  153. */
  154. public function systemAdminMenuBlockPage() {
  155. return $this->systemManager->getBlockContents();
  156. }
  157. /**
  158. * Returns a theme listing.
  159. *
  160. * @return string
  161. * An HTML string of the theme listing page.
  162. *
  163. * @todo Move into ThemeController.
  164. */
  165. public function themesPage() {
  166. $config = $this->config('system.theme');
  167. // Get all available themes.
  168. $themes = $this->themeHandler->rebuildThemeData();
  169. uasort($themes, 'system_sort_modules_by_info_name');
  170. $theme_default = $config->get('default');
  171. $theme_groups = ['installed' => [], 'uninstalled' => []];
  172. $admin_theme = $config->get('admin');
  173. $admin_theme_options = [];
  174. foreach ($themes as &$theme) {
  175. if (!empty($theme->info['hidden'])) {
  176. continue;
  177. }
  178. $theme->is_default = ($theme->getName() == $theme_default);
  179. $theme->is_admin = ($theme->getName() == $admin_theme || ($theme->is_default && empty($admin_theme)));
  180. $theme->is_experimental = isset($theme->info['experimental']) && $theme->info['experimental'];
  181. // Identify theme screenshot.
  182. $theme->screenshot = NULL;
  183. // Create a list which includes the current theme and all its base themes.
  184. if (isset($themes[$theme->getName()]->base_themes)) {
  185. $theme_keys = array_keys($themes[$theme->getName()]->base_themes);
  186. $theme_keys[] = $theme->getName();
  187. }
  188. else {
  189. $theme_keys = [$theme->getName()];
  190. }
  191. // Look for a screenshot in the current theme or in its closest ancestor.
  192. foreach (array_reverse($theme_keys) as $theme_key) {
  193. if (isset($themes[$theme_key]) && file_exists($themes[$theme_key]->info['screenshot'])) {
  194. $theme->screenshot = [
  195. 'uri' => $themes[$theme_key]->info['screenshot'],
  196. 'alt' => $this->t('Screenshot for @theme theme', ['@theme' => $theme->info['name']]),
  197. 'title' => $this->t('Screenshot for @theme theme', ['@theme' => $theme->info['name']]),
  198. 'attributes' => ['class' => ['screenshot']],
  199. ];
  200. break;
  201. }
  202. }
  203. if (empty($theme->status)) {
  204. // Require the 'content' region to make sure the main page
  205. // content has a common place in all themes.
  206. $theme->incompatible_region = !isset($theme->info['regions']['content']);
  207. $theme->incompatible_php = version_compare(phpversion(), $theme->info['php']) < 0;
  208. // Confirm that all base themes are available.
  209. $theme->incompatible_base = (isset($theme->info['base theme']) && !($theme->base_themes === array_filter($theme->base_themes)));
  210. // Confirm that the theme engine is available.
  211. $theme->incompatible_engine = isset($theme->info['engine']) && !isset($theme->owner);
  212. }
  213. $theme->operations = [];
  214. if (!empty($theme->status) || !$theme->info['core_incompatible'] && !$theme->incompatible_php && !$theme->incompatible_base && !$theme->incompatible_engine) {
  215. // Create the operations links.
  216. $query['theme'] = $theme->getName();
  217. if ($this->themeAccess->checkAccess($theme->getName())) {
  218. $theme->operations[] = [
  219. 'title' => $this->t('Settings'),
  220. 'url' => Url::fromRoute('system.theme_settings_theme', ['theme' => $theme->getName()]),
  221. 'attributes' => ['title' => $this->t('Settings for @theme theme', ['@theme' => $theme->info['name']])],
  222. ];
  223. }
  224. if (!empty($theme->status)) {
  225. if (!$theme->is_default) {
  226. $theme_uninstallable = TRUE;
  227. if ($theme->getName() == $admin_theme) {
  228. $theme_uninstallable = FALSE;
  229. }
  230. // Check it isn't the base of theme of an installed theme.
  231. foreach ($theme->required_by as $themename => $dependency) {
  232. if (!empty($themes[$themename]->status)) {
  233. $theme_uninstallable = FALSE;
  234. }
  235. }
  236. if ($theme_uninstallable) {
  237. $theme->operations[] = [
  238. 'title' => $this->t('Uninstall'),
  239. 'url' => Url::fromRoute('system.theme_uninstall'),
  240. 'query' => $query,
  241. 'attributes' => ['title' => $this->t('Uninstall @theme theme', ['@theme' => $theme->info['name']])],
  242. ];
  243. }
  244. $theme->operations[] = [
  245. 'title' => $this->t('Set as default'),
  246. 'url' => Url::fromRoute('system.theme_set_default'),
  247. 'query' => $query,
  248. 'attributes' => ['title' => $this->t('Set @theme as default theme', ['@theme' => $theme->info['name']])],
  249. ];
  250. }
  251. $admin_theme_options[$theme->getName()] = $theme->info['name'] . ($theme->is_experimental ? ' (' . t('Experimental') . ')' : '');
  252. }
  253. else {
  254. $theme->operations[] = [
  255. 'title' => $this->t('Install'),
  256. 'url' => Url::fromRoute('system.theme_install'),
  257. 'query' => $query,
  258. 'attributes' => ['title' => $this->t('Install @theme theme', ['@theme' => $theme->info['name']])],
  259. ];
  260. $theme->operations[] = [
  261. 'title' => $this->t('Install and set as default'),
  262. 'url' => Url::fromRoute('system.theme_set_default'),
  263. 'query' => $query,
  264. 'attributes' => ['title' => $this->t('Install @theme as default theme', ['@theme' => $theme->info['name']])],
  265. ];
  266. }
  267. }
  268. // Add notes to default theme, administration theme and experimental
  269. // themes.
  270. $theme->notes = [];
  271. if ($theme->is_default) {
  272. $theme->notes[] = $this->t('default theme');
  273. }
  274. if ($theme->is_admin) {
  275. $theme->notes[] = $this->t('administration theme');
  276. }
  277. if ($theme->is_experimental) {
  278. $theme->notes[] = $this->t('experimental theme');
  279. }
  280. // Sort installed and uninstalled themes into their own groups.
  281. $theme_groups[$theme->status ? 'installed' : 'uninstalled'][] = $theme;
  282. }
  283. // There are two possible theme groups.
  284. $theme_group_titles = [
  285. 'installed' => $this->formatPlural(count($theme_groups['installed']), 'Installed theme', 'Installed themes'),
  286. ];
  287. if (!empty($theme_groups['uninstalled'])) {
  288. $theme_group_titles['uninstalled'] = $this->formatPlural(count($theme_groups['uninstalled']), 'Uninstalled theme', 'Uninstalled themes');
  289. }
  290. uasort($theme_groups['installed'], 'system_sort_themes');
  291. $this->moduleHandler()->alter('system_themes_page', $theme_groups);
  292. $build = [];
  293. $build[] = [
  294. '#theme' => 'system_themes_page',
  295. '#theme_groups' => $theme_groups,
  296. '#theme_group_titles' => $theme_group_titles,
  297. ];
  298. $build[] = $this->formBuilder->getForm('Drupal\system\Form\ThemeAdminForm', $admin_theme_options);
  299. return $build;
  300. }
  301. }