Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gmpf opened this issue Mar 2, 2018 · 1 comment
Open

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

gmpf opened this issue Mar 2, 2018 · 1 comment
Assignees

Comments

@gmpf
Copy link

gmpf commented Mar 2, 2018

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.

@gmpf
Copy link
Author

gmpf commented Mar 29, 2018

How to reproduce:

Add this to system/config/dcaconfig.php or app/Resources/contao/dca/tl_page.php:

$GLOBALS['TL_DCA']['tl_page']['subpalettes']['protected'] = 'groups,mcwIssue';

$GLOBALS['TL_DCA']['tl_page']['fields']['mcwIssue'] = [
 	'label'     => ['Test MCW issue',''],
	'inputType' => 'multiColumnWizard',
	'eval'      => [
		'columnFields' => [
			'col1'      => [
				'label'            => 'Foo or bar?',
				'inputType'        => 'select',
				'options'          => ['foo', 'bar']
			],
			'col2' => [
				'label'            => 'Baz or boing?',
				'inputType'        => 'select',
				'options'          => ['baz', 'boing']
			]
		]
	]
];

Edit any regular, non-protected page. If you look at the page source, you can see that the MultiColumnWizard CSS and JS aren't there.

Toggle the protected subpalette by selecting "Protect page" (de: "Seite schützen").

It looks like this in Contao 4.4:
4-4-mcw-without-assets

Contao 3.5:
3-5-mcw-without-assets

When the subpalette-triggering field is already saved as active, the MCW widget is generated and the CSS is loaded:

Contao 4.4 with CSS loaded:
4-4-mcw-with-assets

Contao 3.5:
3-5-mcw-with-assets

(The selects look kind of wrong as well, but that's a different issue.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants