Skip to content

Commit

Permalink
Add contao.backend.theme config for setting a global theme
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Jan 29, 2021
1 parent e66e241 commit ce63100
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 42 deletions.
24 changes: 2 additions & 22 deletions core-bundle/src/BackendTheme/BackendThemes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

namespace Contao\CoreBundle\BackendTheme;

use Contao\Folder;
use Contao\System;

/**
* This class holds all installed backend themes that can be fetched by their alias.
*
Expand All @@ -38,32 +35,15 @@ public function addTheme(BackendThemeInterface $backendTheme, string $alias): vo

public function getThemeNames(): array
{
return array_merge($this->getLegacyThemeNames(), array_keys($this->themes));
return array_keys($this->themes);
}

public function getTheme($name): ?BackendThemeInterface
public function getTheme(string $name): ?BackendThemeInterface
{
if (!\array_key_exists($name, $this->themes)) {
return null;
}

return $this->themes[$name];
}

private function getLegacyThemeNames(): array
{
$retrurn = [];
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
$themes = Folder::scan($projectDir.'/system/themes');

foreach ($themes as $name) {
if (0 === strncmp($name, '.', 1) || !is_dir($projectDir.'/system/themes/'.$name)) {
continue;
}

$retrurn[] = $name;
}

return $retrurn;
}
}
29 changes: 18 additions & 11 deletions core-bundle/src/BackendTheme/EncoreEntrypointLookupCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace Contao\CoreBundle\BackendTheme;

use Contao\BackendUser;
use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
Expand All @@ -30,12 +32,14 @@ class EncoreEntrypointLookupCollection implements EntrypointLookupCollectionInte
private BackendThemes $backendThemes;
private string $projectDir;
private array $backendConfig;
private ContaoFramework $framework;

public function __construct(BackendThemes $backendThemes, string $projectDir, array $backendConfig)
public function __construct(BackendThemes $backendThemes, string $projectDir, array $backendConfig, ContaoFramework $framework)
{
$this->backendThemes = $backendThemes;
$this->projectDir = $projectDir;
$this->backendConfig = $backendConfig;
$this->framework = $framework;
}

/**
Expand All @@ -47,22 +51,25 @@ public function __construct(BackendThemes $backendThemes, string $projectDir, ar
*/
public function getEntrypointLookup(string $buildName = null): EntrypointLookupInterface
{
if (null === $buildName || '_default' === $buildName) {
$buildName = Config::get('backendTheme') ?: 'contao';

// BC
if ('flexible' === $buildName) {
$buildName = 'contao';
}
}

// A custom theme path is defined in the app config (via contao.backend.theme_path)
if ($themePath = $this->backendConfig['theme_path']) {
return new EntrypointLookup($themePath);
}

// A global theme is set
if ($themeName = $this->backendConfig['theme']) {
$buildName = $themeName;
}

// The user has a theme defined
$user = $this->framework->createInstance(BackendUser::class);

if (($themeName = $user->backendTheme) && \in_array($themeName, $this->backendThemes->getThemeNames(), true)) {
$buildName = $themeName;
}

// Use the default theme
if ('contao' === $buildName) {
if (null === $buildName || '_default' === $buildName) {
$themePath = sprintf('%s/web/bundles/contaocore/theme/entrypoints.json', $this->projectDir);

return new EntrypointLookup($themePath);
Expand Down
4 changes: 4 additions & 0 deletions core-bundle/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ static function (array $attributes): array {
->useAttributeAsKey('name')
->scalarPrototype()->end()
->end()
->scalarNode('theme')
->info('Set a backend theme for all users.')
->defaultNull()
->end()
->scalarNode('theme_path')
->info('Point to Encore\'s entrypoints.json of your custom backend theme.')
->validate()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\EventListener\DataContainer;

use Contao\Backend;
use Contao\CoreBundle\BackendTheme\BackendThemes;
use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;

class BackendThemesOptionsListener
{
private BackendThemes $backendThemes;
private array $backendConfig;

public function __construct(BackendThemes $backendThemes, array $backendConfig)
{
$this->backendThemes = $backendThemes;
$this->backendConfig = $backendConfig;
}

/**
* @Callback(table="tl_user", target="fields.backendTheme.options")
*/
public function options(DataContainer $dc): array
{
$themes = $this->backendThemes->getThemeNames();
$themes = array_merge($themes, array_values(Backend::getThemes()));

return array_combine($themes, $themes);
}

/**
* @Callback(table="tl_user", target="config.onload")
*/
public function onload($dc): void
{
if (!$dc instanceof DataContainer) {
return;
}

if (null === $this->backendConfig['theme']) {
return;
}

$GLOBALS['TL_DCA']['tl_user']['fields']['backendTheme']['eval']['disabled'] = true;
}
}
5 changes: 5 additions & 0 deletions core-bundle/src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ services:
- '@?event_dispatcher'
public: true

Contao\CoreBundle\EventListener\DataContainer\BackendThemesOptionsListener:
arguments:
- '@Contao\CoreBundle\BackendTheme\BackendThemes'
- '%contao.backend%'

Contao\CoreBundle\EventListener\DataContainer\PageUrlListener:
arguments:
- '@contao.framework'
Expand Down
1 change: 1 addition & 0 deletions core-bundle/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ services:
- '@Contao\CoreBundle\BackendTheme\BackendThemes'
- '%kernel.project_dir%'
- '%contao.backend%'
- '@contao.framework'

contao.backend_theme.tag_renderer:
class: Symfony\WebpackEncoreBundle\Asset\TagRenderer
Expand Down
19 changes: 16 additions & 3 deletions core-bundle/src/Resources/contao/classes/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,26 @@ public static function getTheme()
*
* @return array An array of available back end themes
*
* @deprecated
* @deprecated Returns only legacy backend themes. To be removed in Contao 5.0.
* Use BackendThemes::getThemeNames() instead.
*/
public static function getThemes()
{
$themes = System::getContainer()->get(BackendThemes::class)->getThemeNames();
$retrurn = array();
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
$themes = Folder::scan($projectDir . '/system/themes');

foreach ($themes as $name)
{
if (0 === strncmp($name, '.', 1) || !is_dir($projectDir . '/system/themes/' . $name))
{
continue;
}

$retrurn[$name] = $name;
}

return array_combine($themes, $themes);
return $retrurn;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/classes/BackendUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ protected function setUserFromDb()
Config::set('useRTE', $this->useRTE);
Config::set('useCE', $this->useCE);
Config::set('thumbnails', $this->thumbnails);
Config::set('backendTheme', $this->backendTheme);
Config::set('backendTheme', $this->backendTheme); // BC
Config::set('fullscreen', false); // BC

// Inherit permissions
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
. 'mp3,mp4,m4a,m4v,webm,ogg,ogv,wma,wmv,ram,rm,mov,'
. 'zip,rar,7z';
$GLOBALS['TL_CONFIG']['installPassword'] = '';
$GLOBALS['TL_CONFIG']['backendTheme'] = 'flexible';
$GLOBALS['TL_CONFIG']['backendTheme'] = 'flexible'; // BC
$GLOBALS['TL_CONFIG']['fullscreen'] = false; // BC
$GLOBALS['TL_CONFIG']['disableInsertTags'] = false;
$GLOBALS['TL_CONFIG']['rootFiles'] = array();
Expand Down
4 changes: 0 additions & 4 deletions core-bundle/src/Resources/contao/dca/tl_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@
(
'exclude' => true,
'inputType' => 'select',
'options_callback' => static function ()
{
return Backend::getThemes();
},
'eval' => array('tl_class'=>'w50'),
'sql' => "varchar(32) NOT NULL default ''"
),
Expand Down

0 comments on commit ce63100

Please sign in to comment.