View source
<?php
declare (strict_types=1);
namespace Drupal\theme_permission\Controller;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\system\Controller\SystemController;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Theme\ThemeAccessCheck;
use Drupal\system\SystemManager;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
class AccessController extends SystemController {
protected $currentUser;
protected $requestStack;
public function __construct(AccountInterface $current_user, RequestStack $request_stack, SystemManager $systemManager, ThemeAccessCheck $theme_access, FormBuilderInterface $form_builder, ThemeHandlerInterface $theme_handler, MenuLinkTreeInterface $menu_link_tree) {
parent::__construct($systemManager, $theme_access, $form_builder, $theme_handler, $menu_link_tree);
$this->currentUser = $current_user;
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'), $container
->get('request_stack'), $container
->get('system.manager'), $container
->get('access_check.theme'), $container
->get('form_builder'), $container
->get('theme_handler'), $container
->get('menu.link_tree'));
}
public function access(AccountInterface $account, $theme = NULL) {
if (empty($theme)) {
$theme = $this->requestStack
->getCurrentRequest()->query
->get('theme');
}
$auth = $account
->hasPermission("administer themes {$theme}");
if ($auth) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}
public function themesPage() {
$config = $this
->config('system.theme');
$themes = $this->themeHandler
->rebuildThemeData();
uasort($themes, 'system_sort_modules_by_info_name');
$theme_default = $config
->get('default');
$theme_groups = [
'installed' => [],
'uninstalled' => [],
];
$admin_theme = $config
->get('admin');
$admin_theme_options = [];
foreach ($themes as &$theme) {
$theme_name = $theme
->getName();
$auth = $this->currentUser
->hasPermission("administer themes {$theme_name}");
$uninstall_theme = $this->currentUser
->hasPermission("uninstall themes {$theme_name}");
if ($auth) {
if (!empty($theme->info['hidden'])) {
continue;
}
$theme->is_default = $theme
->getName() == $theme_default;
$theme->is_admin = $theme
->getName() == $admin_theme || $theme->is_default && $admin_theme == '0';
$theme->screenshot = NULL;
if (isset($themes[$theme
->getName()]->base_themes)) {
$theme_keys = array_keys($themes[$theme
->getName()]->base_themes);
$theme_keys[] = $theme
->getName();
}
else {
$theme_keys = [
$theme
->getName(),
];
}
foreach (array_reverse($theme_keys) as $theme_key) {
if (isset($themes[$theme_key]) && file_exists($themes[$theme_key]->info['screenshot'])) {
$theme->screenshot = [
'uri' => $themes[$theme_key]->info['screenshot'],
'alt' => $this
->t('Screenshot for @theme theme', [
'@theme' => $theme->info['name'],
]),
'title' => $this
->t('Screenshot for @theme theme', [
'@theme' => $theme->info['name'],
]),
'attributes' => [
'class' => [
'screenshot',
],
],
];
break;
}
}
if (empty($theme->status)) {
$theme->incompatible_core = !isset($theme->info['core']) || $theme->info['core'] != \DRUPAL::CORE_COMPATIBILITY;
$theme->incompatible_region = !isset($theme->info['regions']['content']);
$theme->incompatible_php = version_compare(phpversion(), $theme->info['php']) < 0;
$theme->incompatible_base = isset($theme->info['base theme']) && !($theme->base_themes === array_filter($theme->base_themes));
$theme->incompatible_engine = isset($theme->info['engine']) && !isset($theme->owner);
}
$theme->operations = [];
if (!empty($theme->status) || !$theme->incompatible_core && !$theme->incompatible_php && !$theme->incompatible_base && !$theme->incompatible_engine) {
$query['theme'] = $theme
->getName();
if ($this->themeAccess
->checkAccess($theme
->getName()) && $auth) {
$theme->operations[] = [
'title' => $this
->t('Settings'),
'url' => Url::fromRoute('system.theme_settings_theme', [
'theme' => $theme
->getName(),
]),
'attributes' => [
'title' => $this
->t('Settings for @theme theme', [
'@theme' => $theme->info['name'],
]),
],
];
}
if (!empty($theme->status)) {
if (!$theme->is_default) {
$theme_uninstallable = TRUE;
if ($theme
->getName() == $admin_theme) {
$theme_uninstallable = FALSE;
}
foreach ($theme->required_by as $themename => $dependency) {
if (!empty($themes[$themename]->status)) {
$theme_uninstallable = FALSE;
}
}
if ($theme_uninstallable && $uninstall_theme) {
$theme->operations[] = [
'title' => $this
->t('Uninstall'),
'url' => Url::fromRoute('system.theme_uninstall'),
'query' => $query,
'attributes' => [
'title' => $this
->t('Uninstall @theme theme', [
'@theme' => $theme->info['name'],
]),
],
];
}
$theme->operations[] = [
'title' => $this
->t('Set as default'),
'url' => Url::fromRoute('system.theme_set_default'),
'query' => $query,
'attributes' => [
'title' => $this
->t('Set @theme as default theme', [
'@theme' => $theme->info['name'],
]),
],
];
}
$admin_theme_options[$theme
->getName()] = $theme->info['name'];
}
else {
$theme->operations[] = [
'title' => $this
->t('Install'),
'url' => Url::fromRoute('system.theme_install'),
'query' => $query,
'attributes' => [
'title' => $this
->t('Install @theme theme', [
'@theme' => $theme->info['name'],
]),
],
];
$theme->operations[] = [
'title' => $this
->t('Install and set as default'),
'url' => Url::fromRoute('system.theme_set_default'),
'query' => $query,
'attributes' => [
'title' => $this
->t('Install @theme as default theme', [
'@theme' => $theme->info['name'],
]),
],
];
}
}
$theme->notes = [];
if ($theme->is_default) {
$theme->notes[] = $this
->t('default theme');
}
if ($theme->is_admin) {
$theme->notes[] = $this
->t('administration theme');
}
$theme_groups[$theme->status ? 'installed' : 'uninstalled'][] = $theme;
}
}
$theme_group_titles = [
'installed' => $this
->formatPlural(count($theme_groups['installed']), 'Installed theme', 'Installed themes'),
];
if (!empty($theme_groups['uninstalled'])) {
$theme_group_titles['uninstalled'] = $this
->formatPlural(count($theme_groups['uninstalled']), 'Uninstalled theme', 'Uninstalled themes');
}
uasort($theme_groups['installed'], 'system_sort_themes');
$this
->moduleHandler()
->alter('system_themes_page', $theme_groups);
$build = [];
$build[] = [
'#theme' => 'system_themes_page',
'#theme_groups' => $theme_groups,
'#theme_group_titles' => $theme_group_titles,
];
$admin_theme_auth = $this->currentUser
->hasPermission("Edit Administration theme");
if ($admin_theme_auth) {
$build[] = $this->formBuilder
->getForm('Drupal\\system\\Form\\ThemeAdminForm', $admin_theme_options);
}
return $build;
}
}