Extbase addAdditionalHeaderData(...) in actioncontroller, include same CSS twice

6.6k views Asked by At

For my extbase extension I need a CSS that I include in all actioncontrollers with:

$this->response->addAdditionalHeaderData('<link rel="stylesheet" type="text/css" href="' . t3lib_extMgm::siteRelPath($this->request->getControllerExtensionKey()) . 'Resources/Public/css/mystyle.css" />');  

If I now have a page where multible plugins of my extensions are included, I have multible CSS includes:

<link rel="stylesheet" type="text/css" href="typo3conf/ext/myext/Resources/Public/css/mystyle.css">
<link rel="stylesheet" type="text/css" href="typo3conf/ext/myext/Resources/Public/css/mystyle.css">

Edit

I could also do the following:

$GLOBALS['TSFE']->additionalHeaderData[100] = '<link ...>';

But as far as I know is that just the old way.

5

There are 5 answers

0
nbar On BEST ANSWER

After years I use vhs:asset now to add CSS/JS to extensions:

<v:asset.script path="EXT:my_extension/Resources/Public/js/javascript.js" name="collectionjs" standalone="1" />
<v:asset.style path="EXT:my_extension/Resources/Public/css/style.css" name="collectioncss" standalone="1" />

The name attribute is unique, So if you have another include with the same name, it will overwrite the old include.

1
nbar On

Solved it with using the old way:

$GLOBALS['TSFE']->additionalHeaderData[$this->request->getControllerExtensionKey()."CSS1"] = '<link rel="stylesheet" type="text/css" href="' . t3lib_extMgm::siteRelPath($this->request->getControllerExtensionKey()) . 'Resources/Public/css/mystyle.css" />';  

For every CSS or JS i include I use another sufix: CSS2, CSS3, JS1, JS2 and so on. So if another controller will include the same file it will just overwrite the old entry in the additionalHeaderData Array.

0
Jay Dinse On

Use can use a class variable to mark that you already included your stylesheet. The value of this variable should be alive the whole request.

In your controller class, define your class variable / static property:

public static $includedMyStyle;

in your controller action:

if (!self::$includedMyStyle) {
  $this->response->addAdditionalHeaderData('<link rel="stylesheet" type="text/css" href="' . t3lib_extMgm::siteRelPath($this->request->getControllerExtensionKey()) . 'Resources/Public/css/mystyle.css" />');
  self::$includedMyStyle = true;
}
0
ch. vonrohr On

I usually prefer to include CSS and JS with static TypoScript, as it easier adjustable, if someone would like to have its own CSS. You should exclude it from the TYPO3 CSS-Merger if you include this TS only on the Extension page.

Just use the following TypoScript

page.includeCSS.myExtCss = EXT:myext/Resources/Public/css/mystyle.css
page.includeCSS.myExtCss.excludeFromConcatenation = 1
3
biesior On

Old question, but it's worth to mention...

You need to change approach here. If you have several instances of the same plugin on the same page which should behave differently, you just need to use FlexForm for adding options for each instance, they should start with settings and each part separated by dot is next level of an associative array, for an example:

<settings.includeHeaderData>
    <TCEforms>
        <exclude>0</exclude>
        <label>Include header data...</label>
        <config>
            <type>check</type>
            <default>0</default>
        </config>
    </TCEforms>
</settings.includeHeaderData>

so in your action you can use it like:

if (intval($this->settings['includeHeaderData']) > 0){
    $this->response->addAdditionalHeaderData('<link ...>');
}

or if node where:

<settings.foo.bar.baz.something>
    ...
</settings.foo.bar.baz.something>

you can access it in the controller like:

$mySetting = $this->settings['foo']['bar']['baz']['something'];