Skip to content

Commit

Permalink
BUGFIX: Get data for ContentBlockData from processedData
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-codappix committed Jun 12, 2024
1 parent 1b7e309 commit 57307e1
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions Classes/DataProcessing/ResponsiveImagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use Codappix\ResponsiveImages\Domain\Factory\BreakpointFactory;
use Codappix\ResponsiveImages\Domain\Factory\RootlineFactory;
use RuntimeException;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand All @@ -42,11 +44,13 @@ final class ResponsiveImagesProcessor implements DataProcessorInterface

private array $contentElementSizes = [];

private array $processedData = [];

public function __construct(
private readonly FileRepository $fileRepository,
private readonly BreakpointFactory $breakpointFactory,
private readonly RootlineFactory $rootlineFactory
) {
}
) {}

public function process(
ContentObjectRenderer $cObj,
Expand All @@ -60,6 +64,8 @@ public function process(
return $processedData;
}

$this->processedData = $processedData;

$filesDataKey = (string) $cObj->stdWrapValue(
'filesDataKey',
$processorConfiguration,
Expand All @@ -70,10 +76,17 @@ 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.
$targetFieldName = (string) $cObj->stdWrapValue(
'as',
$processorConfiguration,
'responsiveImages'
);

$this->files = $this->getFiles($filesDataKey, $fieldName);

if (empty($this->files)) {
$processedData[$targetFieldName] = [];

return $processedData;
}

Expand All @@ -82,16 +95,10 @@ public function process(
throw new RuntimeException('Could not fetch TypoScriptFrontendController from request.', 1712819889);
}

$rootline = $this->rootlineFactory->create($processedData['data'], $fieldName, $tsfe);
$rootline = $this->rootlineFactory->create($this->getData(), $fieldName, $tsfe);
$this->contentElementSizes = $rootline->getFinalSize();
$this->calculateFileDimensions();

$targetFieldName = (string) $cObj->stdWrapValue(
'as',
$processorConfiguration,
'responsiveImages'
);

$processedData[$targetFieldName] = $this->calculatedFiles;

return $processedData;
Expand Down Expand Up @@ -128,4 +135,43 @@ private function calculateFileDimensionForBreakpoints(): array

return $fileDimensions;
}

public function getData(): array
{
if (
$this->processedData['data'] instanceof ContentBlockData

Check failure on line 142 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.

Check failure on line 142 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.

Check failure on line 142 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.
) {
assert(is_array($this->processedData['data']->_raw));

Check failure on line 144 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.

Check failure on line 144 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.

Check failure on line 144 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.
$data = $this->processedData['data']->_raw;

Check failure on line 145 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.

Check failure on line 145 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.

Check failure on line 145 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Access to property $_raw on an unknown class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData.
} else {
assert(is_array($this->processedData['data']));
$data = $this->processedData['data'];
}

return $data;
}

private function getFiles(string $filesDataKey, string $fieldName): array
{
if (
isset($this->processedData[$filesDataKey])
&& is_array($this->processedData[$filesDataKey])
) {
return $this->processedData[$filesDataKey];
}

if ($fieldName === '') {
return [];
}

if ($this->processedData['data'] instanceof ContentBlockData) {

Check failure on line 167 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.

Check failure on line 167 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.

Check failure on line 167 in Classes/DataProcessing/ResponsiveImagesProcessor.php

View workflow job for this annotation

GitHub Actions / code-quality (8.3)

Class TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData not found.
return $this->processedData['data']->{$fieldName};
}

return $this->fileRepository->findByRelation(
'tt_content',
$fieldName,
$this->processedData['data']['uid']
);
}
}

0 comments on commit 57307e1

Please sign in to comment.