Skip to content

Backend CSS and JS don't get added when widget isn't loaded #249

@gmpf

Description

@gmpf

system/modules/multicolumnwizard/html/js/multicolumnwizard_be(_src).js
and
system/modules/multicolumnwizard/html/css/multicolumnwizard(_src).css
are intended to be loaded in the backend no matter whether a MultiColumnWizard widget is loaded or not, for example in case the field is initially hidden because it is part of a subpalette.

However, this doesn't happen because the addition of CSS and JS was rendered ineffectual by #213.

The asset paths are added to $GLOBALS['TL_CSS'] and $GLOBALS['TL_JAVASCRIPT'] in a parseTemplate hook:

public function addScriptsAndStyles(&$objTemplate)
{
//do not allow version information to be leaked in the backend login and install tool (#184)
if ($objTemplate->getName() != 'be_login' && $objTemplate->getName() != 'be_install') {
$GLOBALS['TL_JAVASCRIPT']['mcw'] = $GLOBALS['TL_CONFIG']['debugMode']
? 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be_src.js'
: 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be.js';
$GLOBALS['TL_CSS']['mcw'] = $GLOBALS['TL_CONFIG']['debugMode']
? 'system/modules/multicolumnwizard/html/css/multicolumnwizard_src.css'
: 'system/modules/multicolumnwizard/html/css/multicolumnwizard.css';
$objTemplate->ua .= ' version_' . str_replace('.', '-', VERSION) . '-' . str_replace(
'.',
'-',
BUILD
);
}
}

However, that hook is called from Template::parseTemplate after the head tags have already been rendered and added to the template in BackendTemplate::output:
https://github.com/contao/core/blob/d883f04807500b5f5c65bf9dd927c38e60812e42/system/modules/core/classes/BackendTemplate.php#L61-L92
Adding anything to those asset arrays in parseTemplate therefore has no effect.

I'm not sure about which way to go in solving this. I can see two approaches at the moment:

  1. The addition of CSS and JS gets moved back to config.php and we detect the install/login pages differently. Something like this (untested):
// config.php
// …
$version = VERSION.'.'.BUILD;
$addBackendAssets = false;
if (version_compare($version, '4.0', '>='))
{
    $route = System::getContainer()->get('request_stack')->getCurrentRequest()->get('_route');
    if ($route !== 'contao_install' && $route !== 'contao_backend_login')
    {
        $addBackendAssets = true;
    }
}
elseif (TL_SCRIPT !== 'contao/install.php' && TL_SCRIPT !== 'contao/index.php')
{
    $addBackendAssets = true;
}

if (TL_MODE == 'BE' && $addBackendAssets)
{
    $GLOBALS['TL_JAVASCRIPT']['mcw'] = $GLOBALS['TL_CONFIG']['debugMode']
        ? 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be_src.js'
        : 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be.js';
    $GLOBALS['TL_CSS']['mcw']        = $GLOBALS['TL_CONFIG']['debugMode']
        ? 'system/modules/multicolumnwizard/html/css/multicolumnwizard_src.css'
        : 'system/modules/multicolumnwizard/html/css/multicolumnwizard.css';
}

Disadvantages: That's quite a few lines of logic for a config.php. Also, we'd be using deprecated constants (VERSION, BUILD), but then again, so does the rest of MCW. ;)

  1. Essentially duplicate some of BackendTemplate::output to render the head tags and add them to the template after the fact:
// MultiColumnWizardHelper
// …
public function addScriptsAndStyles(&$objTemplate)
{
// …
        $js  = $GLOBALS['TL_CONFIG']['debugMode']
            ? 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be_src.js'
            : 'system/modules/multicolumnwizard/html/js/multicolumnwizard_be.js';
        $css = $GLOBALS['TL_CONFIG']['debugMode']
            ? 'system/modules/multicolumnwizard/html/css/multicolumnwizard_src.css'
            : 'system/modules/multicolumnwizard/html/css/multicolumnwizard.css';
        $objTemplate->javascripts .= \Template::generateScriptTag($js);
        $objTemplate->stylesheets .= \Template::generateStyleTag($css);
// …
}

Disadvantages: Duplication of core code. Also, I think we end up with those tags appearing twice if the widget is loaded as well.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions