typo3-rector icon indicating copy to clipboard operation
typo3-rector copied to clipboard

Deprecation: #95235 - Public getter of services in ModuleTemplate

Open simonschaufi opened this issue 9 months ago • 0 comments

Deprecation: #95235 - Public getter of services in ModuleTemplate

https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.5/Deprecation-95235-PublicGetterOfServicesInModuleTemplate.html .. include:: /Includes.rst.txt

.. _deprecation-95235:

================================================================= Deprecation: #95235 - Public getter of services in ModuleTemplate

See :issue:95235

Description

The public methods :php:getIconFactory and :php:getPageRenderer in :php:TYPO3\CMS\Backend\Template\ModuleTemplate have been marked as deprecated, since using this getters only hides the dependencies to those services.

Impact

Calling either :php:getIconFactory or :php:getPageRenderer will trigger a PHP :php:E_USER_DEPRECATED error. The extension scanner also detects such calls as weak match.

Affected Installations

All installations calling the methods in custom extension code.

Migration

Inject the corresponding services :php:TYPO3\CMS\Core\Imaging\IconFactory and :php:TYPO3\CMS\Core\Page\PageRenderer directly in your class.

A current Extbase backend controller might look like:

.. code-block:: php

class MyController extends ActionController
{
    protected ModuleTemplateFactory $moduleTemplateFactory;

    public function __construct(ModuleTemplateFactory $moduleTemplateFactory)
    {
        $this->moduleTemplateFactory = $moduleTemplateFactory;
    }

    public function myAction(): ResponseInterface
    {
        $moduleTemplate = $this->moduleTemplateFactory->create($this->request);
        $moduleTemplate->getPageRenderer()->loadRequireJsModule('Vendor/Extension/MyJsModule');
        $moduleTemplate->setContent($moduleTemplate->getIconFactory()->getIcon('some-icon', Icon::SIZE_SMALL)->render());
        return $this->htmlResponse($moduleTemplate->renderContent());
    }
}

This should be migrated to:

.. code-block:: php

class MyController extends ActionController
{
    protected ModuleTemplateFactory $moduleTemplateFactory;
    protected IconFactory $iconFactory;
    protected PageRenderer $pageRenderer;

    public function __construct(
        ModuleTemplateFactory $moduleTemplateFactory,
        IconFactory $iconFactory,
        PageRenderer $pageRenderer
    ) {
        $this->moduleTemplateFactory = $moduleTemplateFactory;
        $this->iconFactory = $iconFactory;
        $this->pageRenderer = $pageRenderer;
    }

    public function myAction(): ResponseInterface
    {
        $moduleTemplate = $this->moduleTemplateFactory->create($this->request);
        $this->pageRenderer->loadRequireJsModule('Vendor/Extension/MyJsModule');
        $moduleTemplate->setContent($this->iconFactory->getIcon('some-icon', Icon::SIZE_SMALL)->render());
        return $this->htmlResponse($moduleTemplate->renderContent());
    }
}

.. index:: Backend, PHP-API, FullyScanned, ext:backend

simonschaufi avatar Nov 03 '23 10:11 simonschaufi