PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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