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

Feature: Support for TYPO3 ContentBlocks #32

Merged
merged 6 commits into from
Aug 30, 2024
Merged
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
71 changes: 60 additions & 11 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,7 +44,10 @@ 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
) {
Expand All @@ -54,10 +59,14 @@ public function process(
array $processorConfiguration,
array $processedData
): array {
$this->calculatedFiles = [];

if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}

$this->processedData = $processedData;

$filesDataKey = (string) $cObj->stdWrapValue(
'filesDataKey',
$processorConfiguration,
Expand All @@ -68,10 +77,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 @@ -80,16 +96,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 @@ -126,4 +136,43 @@ private function calculateFileDimensionForBreakpoints(): array

return $fileDimensions;
}

private function getData(): array
{
if (
$this->processedData['data'] instanceof ContentBlockData
) {
assert(is_array($this->processedData['data']->_raw));
$data = $this->processedData['data']->_raw;
} 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) {
return $this->processedData['data']->{$fieldName};
}

return $this->fileRepository->findByRelation(
'tt_content',
$fieldName,
$this->processedData['data']['uid']
);
}
}
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod.web_layout.BackendLayouts {
MainTemplate {
title = MainTemplate
name = MainTemplate
config {
backend_layout {
colCount = 1
rowCount = 1
rows {
1 {
columns {
1 {
name = Main Content
colPos = 0
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
tt_content.codappix_image {
dataProcessing {
100 = Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor
100 {
fieldName = image
}
}
}

plugin.tx_responsiveimages {
settings {
contentelements {
codappix_image {
image {
multiplier {
xs = 1
sm = 1
md = 1
lg = 1
xl = 0.5
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
tt_content.codappix_imagefixedwidth {
dataProcessing {
100 = Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor
100 {
fieldName = image
}
}
}

plugin.tx_responsiveimages {
settings {
contentelements {
codappix_imagefixedwidth {
image {
sizes {
xs = 600
sm = 900
md = 1200
lg = 1600
xl = 1600
}
}
}
}
}
}
Loading