Deprecation: #104684 - Fluid RenderingContext->getRequest()

See forge#104684

Description

The following methods have been marked as deprecated in TYPO3 v13 and will be removed with TYPO3 v14:

  • \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext->setRequest()
  • \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext->getRequest()

Impact

Calling above methods triggers a deprecation level log entry in TYPO3 v13 and will trigger a fatal PHP error with TYPO3 v14.

Affected installations

RenderingContext->getRequest() is a relatively common call in custom view helpers. Instances with extensions that deliver custom view helpers may be affected. The extension scanner is not configured to find potential places since the method names are common and would lead to too many false positives.

Migration

Class \TYPO3\CMS\Fluid\Core\Rendering\RenderingContext of the Core extension Fluid extends class \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext of Fluid standalone and adds the methods setRequest() and getRequest(). These methods are however not part of \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface .

Fluid standalone will not add these methods, since the view of this library should stay free from direct PSR-7 \ServerRequestInterface dependencies. Having those methods in ext:fluid RenderingContext however collides with \RenderingContextInterface , which is type hinted in Fluid view helper method signatures.

Fluid standalone instead added three methods to handle arbitrary additional data in \RenderingContextInterface : setAttribute(), hasAttribute() and getAttribute(). Those should be used instead.

A typical usage in a view helper before:

/** @var \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext $renderingContext */
$renderingContext = $this->renderingContext;
$request = $renderingContext->getRequest();
Copied!

After:

// use Psr\Http\Message\ServerRequestInterface

$request = null;
if ($renderingContext->hasAttribute(ServerRequestInterface::class)) {
    $request = $renderingContext->getAttribute(ServerRequestInterface::class);
}
Copied!

To stay compatible to previous TYPO3 versions while avoiding deprecation notices, the following code can be used:

// use Psr\Http\Message\ServerRequestInterface

if (
    method_exists($renderingContext, 'getAttribute') &&
    method_exists($renderingContext, 'hasAttribute') &&
    $renderingContext->hasAttribute(ServerRequestInterface::class)
) {
    $request = $renderingContext->getAttribute(ServerRequestInterface::class);
} else {
    $request = $renderingContext->getRequest();
}
Copied!