2
votes

I used to load a css file when my extension gets loaded like this in 7LTS:

/**
 * Init
 *
 * @return void
 */
public function initializeAction() {

    $GLOBALS['TSFE']->getPageRenderer()->addCssFile('typo3conf/ext/myextension/Resources/Public/Css/myextension.css');

}

In 8LTS I get an error:

Uncaught TYPO3 Exception Call to undefined method TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::getPageRenderer()

Supposedly getPageRenderer() is depcreciated:

https://docs.typo3.org/typo3cms/extensions/core/8.7/Changelog/8.0/Breaking-72424-RemovedDeprecatedTypoScriptFrontendControllerOptionsAndMethods.html?highlight=getpagerenderer

How can I load a css file when my extension gets loaded now?

I tried this but it's not working:

/**
 * Init
 *
 * @return void
 */
public function initializeAction() {

    $pageRender = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
    $pageRender->addCssFile('typo3conf/ext/myextension/Resources/Public/Css/basic.css');       

}

The duplicate link describes an alternative way (via templating) ... but not via an action ... so in my opinion no duplicate

1
Oh I didn't see that ... thanks. In meantime I tried to do it in the controller like also described in your link ... would you know why it is not working?Philipp M
Why not using vhs:asset? (Only using version 7, not sure if vhs:asset is still a way to go in version 8). I use vhs:asset to merge some CSS files together. And to load some other CSS files only when I use a specific template/pluginnbar
@nbar because it would be nice to solve such things in a controller, rather then in a templatefeeela

1 Answers

2
votes

The PageRenderer itself is not deprecated, just the method ->getPageRenderer() as the PageRenderer is now a Singleton.

So what you do is $pageRenderer = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class); and you are fine.