From 8dc26fdf05c7eb7d5ad0c9a1b93763388569cc00 Mon Sep 17 00:00:00 2001 From: Justus Moroni Date: Thu, 4 Apr 2024 11:59:32 +0200 Subject: [PATCH] wip:feat: Content block support --- .../ResponsiveImagesProcessor.php | 106 +++++++----------- Classes/Service/ResponsiveImageService.php | 93 +++++++++++++++ Configuration/Services.yaml | 7 ++ composer.json | 3 +- 4 files changed, 144 insertions(+), 65 deletions(-) create mode 100644 Classes/Service/ResponsiveImageService.php diff --git a/Classes/DataProcessing/ResponsiveImagesProcessor.php b/Classes/DataProcessing/ResponsiveImagesProcessor.php index 0d4dc8b..d5566d8 100644 --- a/Classes/DataProcessing/ResponsiveImagesProcessor.php +++ b/Classes/DataProcessing/ResponsiveImagesProcessor.php @@ -23,30 +23,23 @@ * 02110-1301, USA. */ -use Codappix\ResponsiveImages\Configuration\ConfigurationManager; -use Codappix\ResponsiveImages\Sizes\Breakpoint; -use Codappix\ResponsiveImages\Sizes\Rootline; -use TYPO3\CMS\Core\Resource\FileInterface; -use TYPO3\CMS\Core\Utility\GeneralUtility; +use Codappix\ResponsiveImages\Service\ResponsiveImageService; +use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData; +use TYPO3\CMS\Core\Resource\FileRepository; +use TYPO3\CMS\Extbase\Utility\DebuggerUtility; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; final class ResponsiveImagesProcessor implements DataProcessorInterface { - private readonly ConfigurationManager $configurationManager; + private array $processedData; - /** - * @var FileInterface[] - */ - private array $files = []; + private ContentBlockData|array $data; - private array $calculatedFiles = []; - - private array $contentElementSizes = []; - - public function __construct() - { - $this->configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); + public function __construct( + private readonly ResponsiveImageService $responsiveImageService, + private readonly FileRepository $fileRepository, + ) { } public function process( @@ -59,6 +52,9 @@ public function process( return $processedData; } + $this->processedData = $processedData; + $this->data = $processedData['data']; + $filesDataKey = (string) $cObj->stdWrapValue( 'filesDataKey', $processorConfiguration, @@ -69,72 +65,54 @@ public function process( $processorConfiguration, 'image' ); - if (isset($processedData[$filesDataKey]) && is_array($processedData[$filesDataKey])) { - $this->files = $processedData[$filesDataKey]; - } else { - // Files key is empty or not configured. - return $processedData; - } - - $this->contentElementSizes = (new Rootline($processedData['data'], $fieldName))->getFinalSizes(); - $this->calculateFileDimensions(); - $targetFieldName = (string) $cObj->stdWrapValue( 'as', $processorConfiguration, 'responsiveImages' ); - $processedData[$targetFieldName] = $this->calculatedFiles; - - return $processedData; - } + $files = $this->getFiles($filesDataKey, $fieldName); - private function calculateFileDimensions(): void - { - foreach ($this->files as $file) { - $calculatedFile = [ - 'media' => $file, - 'sizes' => $this->calculateFileDimensionForBreakpoints(), - ]; + if (empty($files)) { + $processedData[$targetFieldName] = []; - $this->calculatedFiles[] = $calculatedFile; + return $processedData; } - } - private function calculateFileDimensionForBreakpoints(): array - { - $fileDimensions = []; - - $breakpoints = $this->getBreakpoints(); + $processedData[$targetFieldName] = $this->responsiveImageService->getCalculatedFiles($files, $this->data, $fieldName); - /** @var Breakpoint $breakpoint */ - foreach ($breakpoints as $breakpoint) { - if (isset($this->contentElementSizes[$breakpoint->getIdentifier()]) === false) { - continue; - } + return $processedData; + } - $fileDimensions[$breakpoint->getIdentifier()] = [ - 'breakpoint' => $breakpoint, - 'size' => $this->contentElementSizes[$breakpoint->getIdentifier()], - ]; + private function getFromData(string $key): mixed + { + if ($this->data instanceof ContentBlockData) { + return $this->data->{$key}; } - return $fileDimensions; + return $this->data[$key]; } - private function getBreakpoints(): array + private function getFiles(string $filesDataKey, string $fieldName): array { - $breakpoints = []; - - $breakpointsByPath = $this->configurationManager->getByPath('breakpoints'); + if ( + isset($this->processedData[$filesDataKey]) + && is_array($this->processedData[$filesDataKey]) + ) { + return $this->processedData[$filesDataKey]; + } - if (is_iterable($breakpointsByPath)) { - foreach ($breakpointsByPath as $breakpointIdentifier => $breakpointData) { - $breakpoints[$breakpointIdentifier] = new Breakpoint($breakpointIdentifier, $breakpointData); - } + if ($fieldName === '') { + return []; } - return $breakpoints; + $uid = $this->getFromData('uid'); + assert(is_int($uid)); + + return $this->fileRepository->findByRelation( + 'tt_content', + $fieldName, + $uid + ); } } diff --git a/Classes/Service/ResponsiveImageService.php b/Classes/Service/ResponsiveImageService.php new file mode 100644 index 0000000..da93f89 --- /dev/null +++ b/Classes/Service/ResponsiveImageService.php @@ -0,0 +1,93 @@ +files = $files; + + if ( + $data instanceof ContentBlockData + ) { + assert(is_array($data->_raw)); + $data = $data->_raw; + } + + $this->contentElementSizes = (new Rootline($data, $fieldName))->getFinalSizes(); + + $this->calculateFileDimensions(); + + return $this->calculatedFiles; + } + + private function calculateFileDimensions(): void + { + foreach ($this->files as $file) { + $calculatedFile = [ + 'media' => $file, + 'sizes' => $this->calculateFileDimensionForBreakpoints(), + ]; + + $this->calculatedFiles[] = $calculatedFile; + } + } + + private function calculateFileDimensionForBreakpoints(): array + { + $fileDimensions = []; + + foreach ($this->getBreakpoints() as $breakpoint) { + if (isset($this->contentElementSizes[$breakpoint->getIdentifier()]) === false) { + continue; + } + + $fileDimensions[$breakpoint->getIdentifier()] = [ + 'breakpoint' => $breakpoint, + 'size' => $this->contentElementSizes[$breakpoint->getIdentifier()], + ]; + } + + return $fileDimensions; + } + + /** + * @return Breakpoint[] + */ + private function getBreakpoints(): array + { + $breakpoints = []; + + $breakpointsByPath = $this->configurationManager->getByPath('breakpoints'); + + if (is_iterable($breakpointsByPath)) { + foreach ($breakpointsByPath as $breakpointIdentifier => $breakpointData) { + $breakpoints[$breakpointIdentifier] = new Breakpoint($breakpointIdentifier, $breakpointData); + } + } + + return $breakpoints; + } +} diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index ade47c2..3a03030 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -18,6 +18,13 @@ services: $extensionName: 'ResponsiveImages' $pluginName: '' + Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor: + public: true + tags: + - + name: 'data.processor' + identifier: 'responsive-images' + Codappix\ResponsiveImages\Configuration\ConfigurationManager: public: true arguments: diff --git a/composer.json b/composer.json index 7c382ad..38692f2 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,8 @@ "staabm/phpstan-todo-by": "^0.1.20", "typo3/cms-fluid-styled-content": "^12.4", "typo3/testing-framework": "^8.0", - "tomasvotruba/cognitive-complexity": "^0.2.2" + "tomasvotruba/cognitive-complexity": "^0.2.2", + "contentblocks/content-blocks": "^0.7" }, "config": { "allow-plugins": {