2

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

Philipp M
  • 3,306
  • 5
  • 36
  • 90
  • Possible duplicate of [TYPO3: How could I add css and js files via controller initialize action and page renderer?](https://stackoverflow.com/questions/45440189/typo3-how-could-i-add-css-and-js-files-via-controller-initialize-action-and-pag) – Mathias Brodala Aug 22 '17 at 13:58
  • Please see [my answer to this question here](https://stackoverflow.com/questions/45440189/typo3-how-could-i-add-css-and-js-files-via-controller-initialize-action-and-pag/45440378#45440378). – Mathias Brodala Aug 22 '17 at 13:58
  • 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 Aug 22 '17 at 14:04
  • 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/plugin – nbar Aug 22 '17 at 15:55
  • @nbar because it would be nice to solve such things in a controller, rather then in a template – feeela May 09 '18 at 11:34

1 Answers1

2

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.

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34