Philipp M
Philipp M

Reputation: 3498

TYPO3 - Load css file on init when extensions gets loaded in 8LTS? getPageRenderer() is depreciated

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

Upvotes: 2

Views: 759

Answers (1)

Georg Ringer
Georg Ringer

Reputation: 7939

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.

Upvotes: 2

Related Questions