Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Add ContentBlocks Support #31

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 44 additions & 51 deletions Classes/DataProcessing/ResponsiveImagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,21 @@
* 02110-1301, USA.
*/

use Codappix\ResponsiveImages\Domain\Factory\BreakpointFactory;
use Codappix\ResponsiveImages\Domain\Factory\RootlineFactory;
use Codappix\ResponsiveImages\Service\ResponsiveImageService;
use RuntimeException;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockData;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class ResponsiveImagesProcessor implements DataProcessorInterface
{
/**
* @var FileInterface[]
*/
private array $files = [];

private array $calculatedFiles = [];

private array $contentElementSizes = [];
private array $processedData;

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

Expand All @@ -58,6 +51,8 @@ public function process(
return $processedData;
}

$this->processedData = $processedData;

$filesDataKey = (string) $cObj->stdWrapValue(
'filesDataKey',
$processorConfiguration,
Expand All @@ -68,10 +63,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'
);

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

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

return $processedData;
}

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

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

$targetFieldName = (string) $cObj->stdWrapValue(
'as',
$processorConfiguration,
'responsiveImages'
);
if (
$this->processedData['data'] instanceof ContentBlockData
) {
assert(is_array($this->processedData['data']->_raw));
$data = $this->processedData['data']->_raw;
} else {
$data = $this->processedData['data'];
}

$processedData[$targetFieldName] = $this->calculatedFiles;
$processedData[$targetFieldName] = $this->responsiveImageService->getCalculatedFiles($files, $data, $fieldName, $tsfe);

return $processedData;
}

private function calculateFileDimensions(): void
private function getFiles(string $filesDataKey, string $fieldName): array
{
foreach ($this->files as $file) {
$calculatedFile = [
'media' => $file,
'sizes' => $this->calculateFileDimensionForBreakpoints(),
];

$this->calculatedFiles[] = $calculatedFile;
if (
isset($this->processedData[$filesDataKey])
&& is_array($this->processedData[$filesDataKey])
) {
return $this->processedData[$filesDataKey];
}
}

private function calculateFileDimensionForBreakpoints(): array
{
$fileDimensions = [];

$breakpoints = $this->breakpointFactory->getByConfigurationPath(['breakpoints']);

foreach ($breakpoints as $breakpoint) {
if (isset($this->contentElementSizes[$breakpoint->getIdentifier()]) === false) {
continue;
}
if ($fieldName === '') {
return [];
}

$fileDimensions[$breakpoint->getIdentifier()] = [
'breakpoint' => $breakpoint,
'size' => $this->contentElementSizes[$breakpoint->getIdentifier()],
];
if ($this->processedData['data'] instanceof ContentBlockData) {
return $this->processedData['data']->{$fieldName};
}

return $fileDimensions;
return $this->fileRepository->findByRelation(
'tt_content',
$fieldName,
$this->processedData['data']['uid']
);
}
}
70 changes: 70 additions & 0 deletions Classes/Service/ResponsiveImageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Service;

use Codappix\ResponsiveImages\Domain\Factory\BreakpointFactory;
use Codappix\ResponsiveImages\Domain\Factory\RootlineFactory;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

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

private array $contentElementSizes = [];

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

public function getCalculatedFiles(
array $files,
array $data,
string $fieldName,
TypoScriptFrontendController $tsfe
): array {
$this->files = $files;

$rootline = $this->rootlineFactory->create($data, $fieldName, $tsfe);
$this->contentElementSizes = $rootline->getFinalSize();

return $this->calculateFileDimensions();
}

private function calculateFileDimensions(): array
{
$calculatedFiles = [];

foreach ($this->files as $file) {
$calculatedFiles[] = [
'media' => $file,
'sizes' => $this->calculateFileDimensionForBreakpoints(),
];
}

return $calculatedFiles;
}

private function calculateFileDimensionForBreakpoints(): array
{
$fileDimensions = [];

$breakpoints = $this->breakpointFactory->getByConfigurationPath(['breakpoints']);

foreach ($breakpoints as $breakpoint) {
if (isset($this->contentElementSizes[$breakpoint->getIdentifier()]) === false) {
continue;
}

$fileDimensions[$breakpoint->getIdentifier()] = [
'breakpoint' => $breakpoint,
'size' => $this->contentElementSizes[$breakpoint->getIdentifier()],
];
}

return $fileDimensions;
}
}
4 changes: 4 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ services:

Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor:
public: true
tags:
-
name: 'data.processor'
identifier: 'responsive-images'

Codappix\ResponsiveImages\Configuration\ConfigurationManager:
arguments:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_imagefixedwidth';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][1]['CType'] = 'codappix_image';

return $configuration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

$pathinfo = pathinfo(__FILE__);
$path = $pathinfo['dirname'] . '/../Content/' . $pathinfo['basename'];
$configuration = include $path;

$configuration['tt_content'][2]['CType'] = 'codappix_imagefixedwidth';

return $configuration;
Loading