Skip to content

Commit

Permalink
Detect currently active theme in EncoreLookupCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Jan 29, 2021
1 parent 3b278d8 commit e66e241
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Contao\CoreBundle\BackendTheme;

use Contao\Config;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
Expand Down Expand Up @@ -46,8 +47,13 @@ public function __construct(BackendThemes $backendThemes, string $projectDir, ar
*/
public function getEntrypointLookup(string $buildName = null): EntrypointLookupInterface
{
if (null === $buildName) {
$buildName = 'contao';
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)
Expand Down
2 changes: 0 additions & 2 deletions core-bundle/src/Resources/contao/controllers/BackendIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ public function run()
return $this->Template->getResponse();
}

$objTemplate->theme = 'flexible' === $themeName ? 'contao' : $themeName;

$twig = $container->get('twig');

return $objTemplate->getResponse()->setContent(
Expand Down
2 changes: 0 additions & 2 deletions core-bundle/src/Resources/contao/controllers/BackendMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ protected function output()
return $this->Template->getResponse();
}

$this->Template->theme = 'flexible' === $themeName ? 'contao' : $themeName;

return $this->Template->getResponse()->setContent(
$twig->render('@ContaoCore/Backend/Layout/main.html.twig', $this->Template->getData())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
{% endblock %}

{% block stylesheets %}
{{ contao_backend_link_tags('app', null, theme) }}
{{ contao_backend_link_tags('app') }}
{% endblock %}

{% block javascripts %}
{{ contao_backend_script_tags('app', null, theme) }}
{{ contao_backend_script_tags('app') }}
{% endblock %}
{% endblock %}
</head>
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/views/Backend/Layout/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% endblock %}

{% block stylesheets %}
{{ contao_backend_link_tags('app', null, theme) }}
{{ contao_backend_link_tags('app') }}

{# deprecated entries #}
<link rel="stylesheet" href="system/themes/flexible/fonts.css">
Expand All @@ -26,7 +26,7 @@
{% endblock %}

{% block javascripts %}
{{ contao_backend_script_tags('app', null, theme) }}
{{ contao_backend_script_tags('app') }}

{# deprecated entries #}
<script>{{ localeString|raw }}</script>
Expand Down
13 changes: 9 additions & 4 deletions core-bundle/src/Twig/Extension/EncoreEntryFilesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* This class provides twig functions to render theme assets in the page layout.
*
* Adapted from \Symfony\WebpackEncoreBundle\Twig\EntryFilesTwigExtension but uses a custom lookup collection.
*
* Per default, the Encore entrypoint of the currently active backend theme is used with a fallback to the Contao core
* backend theme. Use $entrypointName = 'foobar' to explicitly load files from a backend theme with the "foobar" alias.
*
* To register a theme, use the `contao.backend_theme` servie tag.
*/
class EncoreEntryFilesTwigExtension extends AbstractExtension
{
Expand All @@ -44,28 +49,28 @@ public function getFunctions()
];
}

public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array
public function getWebpackJsFiles(string $entryName, string $entrypointName = null): array
{
return $this->getEntrypointLookup($entrypointName)
->getJavaScriptFiles($entryName)
;
}

public function getWebpackCssFiles(string $entryName, string $entrypointName = '_default'): array
public function getWebpackCssFiles(string $entryName, string $entrypointName = null): array
{
return $this->getEntrypointLookup($entrypointName)
->getCssFiles($entryName)
;
}

public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default', array $attributes = []): string
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = null, array $attributes = []): string
{
return $this->getTagRenderer()
->renderWebpackScriptTags($entryName, $packageName, $entrypointName, $attributes)
;
}

public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = '_default', array $attributes = []): string
public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = null, array $attributes = []): string
{
return $this->getTagRenderer()
->renderWebpackLinkTags($entryName, $packageName, $entrypointName, $attributes)
Expand Down

0 comments on commit e66e241

Please sign in to comment.