Skip to content

Commit

Permalink
wip:feat: Content block support
Browse files Browse the repository at this point in the history
  • Loading branch information
justusmoroni committed Apr 4, 2024
1 parent b6aeb22 commit 8dc26fd
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 65 deletions.
106 changes: 42 additions & 64 deletions Classes/DataProcessing/ResponsiveImagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -59,6 +52,9 @@ public function process(
return $processedData;
}

$this->processedData = $processedData;
$this->data = $processedData['data'];

$filesDataKey = (string) $cObj->stdWrapValue(
'filesDataKey',
$processorConfiguration,
Expand All @@ -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
);
}
}
93 changes: 93 additions & 0 deletions Classes/Service/ResponsiveImageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Service;

use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Sizes\Breakpoint;
use Codappix\ResponsiveImages\Sizes\Rootline;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData;

final class ResponsiveImageService
{
private array $files = [];

private array $calculatedFiles = [];

private array $contentElementSizes = [];

public function __construct(
private readonly ConfigurationManager $configurationManager
) {
}

public function getCalculatedFiles(
array $files,
ContentBlockData|array $data,
string $fieldName
): array {
$this->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;
}
}
7 changes: 7 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 8dc26fd

Please sign in to comment.