From 32f5916512112047c36f69ea290c4c45c002490f Mon Sep 17 00:00:00 2001 From: David Mellen Date: Tue, 27 Aug 2024 13:01:11 +0200 Subject: [PATCH] feat: allow non-standard file attributes --- .../Listener/EnrichFileDataEventListener.php | 27 ++++++++++++++++++- .../AssetProcessingConfiguration.typoscript | 4 +-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Classes/Event/Listener/EnrichFileDataEventListener.php b/Classes/Event/Listener/EnrichFileDataEventListener.php index f3f773f..7b97cae 100644 --- a/Classes/Event/Listener/EnrichFileDataEventListener.php +++ b/Classes/Event/Listener/EnrichFileDataEventListener.php @@ -5,6 +5,7 @@ namespace Remind\Headless\Event\Listener; use FriendsOfTYPO3\Headless\Event\EnrichFileDataEvent; +use TYPO3\CMS\Core\Utility\GeneralUtility; class EnrichFileDataEventListener { @@ -12,7 +13,31 @@ public function __invoke(EnrichFileDataEvent $event): void { $originalFile = $event->getOriginal(); $properties = $event->getProperties(); - $properties['lazyLoading'] = (bool) $originalFile->getProperty('tx_headless_lazy_loading'); + $processingConfiguration = $event->getProcessingConfiguration(); + + $defaultFields = $processingConfiguration->defaultFieldsByType ?? []; + $imageFields = array_merge($defaultFields, $processingConfiguration->defaultImageFields ?? []); + $videoFields = array_merge($defaultFields, $processingConfiguration->defaultVideoFields ?? []); + + $fields = match ($properties['type']) { + 'image' => $imageFields, + 'video' => $videoFields, + default => $defaultFields, + }; + + foreach ($fields as $field) { + $as = $field; + if (str_contains($field, ' as ')) { + [$field, $as] = GeneralUtility::trimExplode(' as ', $field, true); + } + if ( + $originalFile->hasProperty($field) && + !array_key_exists($as, $properties) + ) { + $properties[$as] = $originalFile->getProperty($field); + } + } + $event->setProperties($properties); } } diff --git a/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript b/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript index 251dba3..1f9ac84 100644 --- a/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript +++ b/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript @@ -4,8 +4,8 @@ lib.assetProcessingConfiguration { linkResult = 1 properties { byType = 1 - defaultFieldsByType = uidLocal,fileReferenceUid,type,extension,title,description,lazyLoading - defaultImageFields = alternative,link,dimensions + defaultFieldsByType = uidLocal,fileReferenceUid,type,extension,title,description,tx_headless_lazy_loading as lazyLoading + defaultImageFields = alternative,link,dimensions defaultVideoFields = autoplay } } \ No newline at end of file