Skip to content

Commit

Permalink
[TASK] Add possibility to override for sizes
Browse files Browse the repository at this point in the history
Related: #16
  • Loading branch information
d-g-codappix committed Apr 3, 2024
1 parent 14ad109 commit 1aefb4e
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 24 deletions.
23 changes: 21 additions & 2 deletions Classes/Sizes/BackendLayout/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@ class Column
/**
* @var float[]
*/
private readonly array $multiplier;
private array $multiplier = [];

/**
* @var int[]
*/
private array $sizes = [];

public function __construct(
private readonly int $identifier,
array $data
) {
$this->multiplier = array_map(static fn ($multiplier): float => Multiplier::parse($multiplier), $data['multiplier']);
if (isset($data['multiplier'])) {
$this->multiplier = array_map(static fn ($multiplier): float => Multiplier::parse($multiplier), $data['multiplier']);
}

if (isset($data['sizes'])) {
$this->sizes = array_map(static fn ($size): int => (int)$size, $data['sizes']);
}
}

public function getIdentifier(): int
Expand All @@ -51,4 +62,12 @@ public function getMultiplier(): array
{
return $this->multiplier;
}

/**
* @return float[]
*/
public function getSizes(): array
{
return $this->sizes;
}
}
14 changes: 10 additions & 4 deletions Classes/Sizes/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,12 @@ final class Container extends ContentElement

private Column $activeColumn;

private readonly ConfigurationManager $configurationManager;

public function __construct(array $data)
{
parent::__construct($data);

$this->layout = $data['CType'];

$this->configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);

$this->determineColumns();
}

Expand Down Expand Up @@ -82,4 +78,14 @@ private function determineColumns(): void
}
}
}

public function getSizes(): array
{
return $this->getActiveColumn()->getSizes();
}

public function getMultiplier(): array
{
return $this->getActiveColumn()->getMultiplier();
}
}
51 changes: 51 additions & 0 deletions Classes/Sizes/ContentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,43 @@
* 02110-1301, USA.
*/

use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Error\Exception;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* This class represents the content elements in the rootline of the current
* content element which is rendered.
*/
class ContentElement implements ContentElementInterface
{
protected readonly ConfigurationManager $configurationManager;

protected readonly string $contentType;

protected readonly int $colPos;

protected ContentElementInterface $parent;

/**
* @var float[]
*/
private array $multiplier = [];

/**
* @var int[]
*/
private array $sizes = [];

public function __construct(
private readonly array $data
) {
$this->configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);

$this->contentType = $data['CType'];
$this->colPos = $data['colPos'];

$this->readConfiguration();
}

public function getData(?string $dataIdentifier = null): mixed
Expand Down Expand Up @@ -80,4 +98,37 @@ public function getParent(): ?ContentElementInterface
{
return $this->parent;
}

public function getSizes(): array
{
return $this->sizes;
}

public function getMultiplier(): array
{
return $this->multiplier;
}

public function readConfiguration(): void
{
if (get_class($this) === ContentElement::class) {
$configurationPath = implode('.', [
'contentelements',
$this->contentType,
'image',
]);

$configuration = $this->configurationManager->getByPath($configurationPath);

if (is_array($configuration)) {
if (isset($configuration['multiplier'])) {
$this->multiplier = array_map(static fn($multiplier): float => Multiplier::parse($multiplier), $configuration['multiplier']);
}

if (isset($configuration['sizes'])) {
$this->sizes = array_map(static fn($size): int => (int)$size, $configuration['sizes']);
}
}
}
}
}
4 changes: 4 additions & 0 deletions Classes/Sizes/ContentElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public function getColPos(): int;
public function setParent(self $contentElement): void;

public function getParent(): ?self;

public function getSizes(): array;

public function getMultiplier(): array;
}
44 changes: 27 additions & 17 deletions Classes/Sizes/Rootline.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ public function getFinalSizes(): array
return $this->finalSizes;
}

public function getMultiplier(): array
{
$multiplier = [
$this->backendLayout->getActiveColumn()->getMultiplier(),
];

foreach (array_reverse($this->rootline) as $contentElement) {
if ($contentElement instanceof Container) {
$multiplier[] = $contentElement->getActiveColumn()->getMultiplier();
}
}

return $multiplier;
}

private function determineBackendLayout(): void
{
$typoscriptFrontendController = $GLOBALS['TSFE'];
Expand Down Expand Up @@ -144,10 +129,35 @@ private function fetchContentElementFromDatabase(int $identifier): ContentElemen

private function calculateSizes(): void
{
$sizes = $this->backendLayout->getSizes();
list($sizes, $multiplier) = $this->getSizesAndMultiplierFromRootline();

if (empty($sizes)) {
$sizes = $this->backendLayout->getSizes();
}

$this->calculateFinalSizes($sizes, $multiplier);
}

private function getSizesAndMultiplierFromRootline(): array
{
$multiplier = [];
$sizes = [];

foreach ($this->rootline as $contentElement) {
if ($contentElement instanceof ContentElement) {
$sizes = $contentElement->getSizes();
if (!empty($sizes)) {
break;
}
$multiplier[] = $contentElement->getMultiplier();
}
}

$multiplier = $this->getMultiplier();
return array($sizes, $multiplier);
}

private function calculateFinalSizes(array $sizes, array $multiplier): void
{
foreach ($sizes as $sizeName => &$size) {
foreach ($multiplier as $multiplierItem) {
if (isset($multiplierItem[$sizeName]) === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tt_content.imageFixWidth < tt_content.image
tt_content.imageFixWidth {
templateRootPaths {
110 = EXT:base_example/Resources/Private/Templates/FluidStyledContent
}
partialRootPaths {
110 = EXT:base_example/Resources/Private/Partials/FluidStyledContent
}

dataProcessing {
20 >
20 = Codappix\ResponsiveImages\DataProcessing\ResponsiveImagesProcessor
20 {
fieldName = image
filesDataKey = files
}
}
}

plugin.tx_responsiveimages {
settings {
contentelements {
imageFixWidth {
image {
sizes {
xs = 600
sm = 900
md = 1200
lg = 1600
xl = 1600
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
page = PAGE
page.10 < styles.content.get
page.10 < styles.content.get
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

return [
'tt_content' => [
1 => [
'uid' => '1',
'pid' => '2',
'hidden' => '0',
'sorting' => '1',
'CType' => 'imageFixWidth',
'header' => 'imageFixWidth',
'deleted' => '0',
'starttime' => '0',
'endtime' => '0',
'colPos' => '0',
'sys_language_uid' => '0',
'image' => '1',
],
],
'sys_file_reference' => [
0 => [
'uid' => '1',
'pid' => '2',
'uid_local' => '1',
'uid_foreign' => '1',
'tablenames' => 'tt_content',
'fieldname' => 'image',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use B13\Container\Tca\ContainerConfiguration;
use B13\Container\Tca\Registry;
use TYPO3\CMS\Core\Utility\GeneralUtility;

(static function (string $cType = 'example_container-1col-full-width'): void {
GeneralUtility::makeInstance(Registry::class)->configureContainer(new ContainerConfiguration(
$cType,
'1 Column: 100',
'(100%)',
[
[
[
'name' => 'Column 101',
'colPos' => 101,
],
],
]
));
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugin.tx_responsiveimages {
settings {
example_container-1col-full-width {
columns {
101 {
sizes {
xs = 1254
sm = 1203
md = 1578
lg = 1920
xl = 1920
}
}
}
}
}
}

tt_content.example_container-1col-full-width < lib.containerElement
tt_content.example_container-1col-full-width {
templateName = 1col
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

return [
'tt_content' => [
1 => [
'uid' => '1',
'pid' => '2',
'hidden' => '0',
'sorting' => '1',
'CType' => 'imageFixWidth',
'header' => 'imageFixWidth',
'deleted' => '0',
'starttime' => '0',
'endtime' => '0',
'colPos' => '0',
'sys_language_uid' => '0',
'image' => '1',
'tx_container_parent' => '0',
],
],
'sys_file_reference' => [
0 => [
'uid' => '1',
'pid' => '2',
'uid_local' => '1',
'uid_foreign' => '1',
'tablenames' => 'tt_content',
'fieldname' => 'image',
],
],
];
Loading

0 comments on commit 1aefb4e

Please sign in to comment.