Skip to content

Instantly share code, notes, and snippets.

@sbuerk
Created September 7, 2024 18:32
Show Gist options
  • Save sbuerk/7a83fdad71c002796903738f7c2e5f90 to your computer and use it in GitHub Desktop.
Save sbuerk/7a83fdad71c002796903738f7c2e5f90 to your computer and use it in GitHub Desktop.
TYPO3 (v13) ImageContentObject rendering early in middleware example (styguide)
<?php
// EXT:myext/Classes/Middleware/DemoImageContentObjectRenderingMiddleware.php
declare(strict_types=1);
namespace MyVendor\MyExtension\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\EventDispatcher\ListenerProvider;
use TYPO3\CMS\Core\Http\ResponseFactory;
use TYPO3\CMS\Core\Http\StreamFactory;
use TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Resource\PublicUrlPrefixer;
final class DemoImageContentObjectRenderingMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly ListenerProvider $listenerProvider,
) {
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// to trigger this middleware, simple add `?middleware-trigger=image-content-object` to a valid
// request along with configure `$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'middleware-trigger';`
// to avoud cHash calculation issues.
if (($request->getQueryParams()['middleware-trigger'] ?? '') !== 'image-content-object') {
return $handler->handle($request);
}
$controller = $request->getAttribute('frontend.controller');
if ($controller instanceof TypoScriptFrontendController) {
// required to calculate/set absRefPrefix correctly
$controller->preparePageContentGeneration($request);
}
$this->listenerProvider->addListener(
GeneratePublicUrlForResourceEvent::class,
PublicUrlPrefixer::class,
'prefixWithAbsRefPrefix'
);
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$contentObjectRenderer->setRequest($request);
$imageContent = $contentObjectRenderer->getContentObject('IMAGE')->render([
'file' => '/fileadmin/styleguide/bus_lane.jpg',
// 'file' => '1:/styleguide/bus_lane.jpg',
'file.' => [
'width' => 100,
],
]);
$streamFactory = GeneralUtility::makeInstance(StreamFactory::class);
$responceFactory = GeneralUtility::makeInstance(ResponseFactory::class);
$response = $responceFactory->createResponse(
200,
'',
)->withBody($streamFactory->createStream($imageContent));
return $response;
}
}
<?php
// EXT:myext/ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']['excludedParameters'][] = 'middleware-trigger';
<?php
// EXT:myext/Configuration/RequestMiddlewares.php
return [
'frontend' => [
'demo-imagecontentobjectrendering' => [
'target' => \MyVendor\MyExtension\Middleware\DemoImageContentObjectRenderingMiddleware::class,
'after' => [
'typo3/cms-frontend/prepare-tsfe-rendering'
],
'before' => [
'typo3/cms-frontend/content-length-headers',
],
],
],
];
# EXT:myext/Configuration/Services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
MyVendor\MyExtension\:
resource: '../Classes/*'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment