MediaProcessor
The media data processor resolves a file
resource and hands it to whichever registered media processor's
supports method matches it first — the built-in
\CPSIT\ is one such
implementation. Implement the interface yourself to support other resource
kinds (documents, videos, download links, ...) or to replace the built-in
image handling.
The interface
-
interface
MediaProcessor -
- Fully qualified name
-
\CPSIT\Typo3Handlebars\ Data Processing\ Media\ Media Processor
process(contentObjectRenderer, resource, configuration = [])-
Process the given resource and return the resulting array, which is stored under
media'saskey, or merged recursively into the processed data ifasis omitted.- param ContentObjectRenderer contentObjectRenderer
-
The current content object renderer.
- param resource
-
The resolved resource — a core
Resourceor an ExtbaseInterface File/File.Reference - param array configuration
-
This processor's slice of
config.<name>. - returntype
-
array
Implement a media processor
Implementations are auto-registered because the interface itself carries
#. The
# attribute on the implementing class both
determines matching order among several processors (higher priority is tried
first, default 0, same as PathProvider and VariableProvider) and gives the processor its
<name>
, i.e. the key under which
media
looks
up its
config.<name>
block.
namespace Vendor\Extension\DataProcessing\Media;
use CPSIT\Typo3Handlebars\DataProcessing\Media\MediaProcessor;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\ResourceInterface;
use TYPO3\CMS\Extbase\Domain\Model\File;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
#[AsTaggedItem('download')]
final readonly class DownloadProcessor implements MediaProcessor
{
public function process(
ContentObjectRenderer $contentObjectRenderer,
ResourceInterface|File|FileReference $resource,
array $configuration = [],
): array {
return [
'url' => $resource->getPublicUrl(),
'label' => $configuration['label'] ?? $resource->getName(),
];
}
public function supports(mixed $resource): bool
{
return $resource instanceof AbstractFile && !$resource->isImage();
}
}
With this registered,
config. becomes available
next to the built-in
config. options wherever
media
is used.
Typed configuration with ConfigurableProcessor
Mapping
configuration
by hand, as above, is fine for a couple
of options. The built-in
Image instead extends the abstract
\CPSIT\,
which uses cuyz/valinor to map the raw
configuration array onto a typed, immutable configuration object before
process is called:
/**
* @extends ConfigurableProcessor<MyConfiguration>
*/
final class MyProcessor extends ConfigurableProcessor
{
public function processFile(
ContentObjectRenderer $contentObjectRenderer,
ResourceInterface|File|FileReference $resource,
Configuration $configuration,
): array {
// $configuration is an instance of MyConfiguration
}
public function supports(mixed $resource): bool
{
// ...
}
protected function getConfigurationClass(): string
{
return MyConfiguration::class;
}
}
My only needs to implement the empty marker interface
\CPSIT\
and declare its accepted options as constructor-promoted properties — see
ImageConfiguration
for reference.
See also
MediaProcessor interface source on GitHub.