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 possibility to override for sizes #17

Merged
merged 7 commits into from
Apr 3, 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
58 changes: 7 additions & 51 deletions Classes/DataProcessing/ResponsiveImagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Sizes\Breakpoint;
use Codappix\ResponsiveImages\Sizes\Multiplier;
use Codappix\ResponsiveImages\Sizes\Rootline;
use TYPO3\CMS\Core\Error\Exception;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -37,24 +35,15 @@ final class ResponsiveImagesProcessor implements DataProcessorInterface
{
private readonly ConfigurationManager $configurationManager;

/**
* The processor configuration
*/
private array $processorConfiguration;

/**
* @var FileInterface[]
*/
private array $files = [];

private array $calculatedFiles = [];

private array $contentElementData = [];

private array $contentElementSizes = [];

private array $contentElementFieldConfiguration = [];

public function __construct()
{
$this->configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
Expand All @@ -69,23 +58,25 @@ public function process(
if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}
$this->processorConfiguration = $processorConfiguration;
$this->contentElementData = $processedData['data'];

$filesDataKey = (string) $cObj->stdWrapValue(
'filesDataKey',
$processorConfiguration,
'files'
);
$fieldName = (string) $cObj->stdWrapValue(
'fieldName',
$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']))->getFinalSizes();
$this->fetchContentElementFieldConfiguration();
$this->contentElementSizes = (new Rootline($processedData['data'], $fieldName))->getFinalSizes();
$this->calculateFileDimensions();

$targetFieldName = (string) $cObj->stdWrapValue(
Expand All @@ -99,23 +90,6 @@ public function process(
return $processedData;
}

private function fetchContentElementFieldConfiguration(): void
{
$contentElementFieldPath = implode('.', [
'contentelements',
$this->contentElementData['CType'],
$this->processorConfiguration['fieldName'],
]);

if ($this->configurationManager->isValidPath($contentElementFieldPath) === false) {
throw new Exception("Field configuration '" . $contentElementFieldPath . "' missing.");
}

if (is_array($this->configurationManager->getByPath($contentElementFieldPath))) {
$this->contentElementFieldConfiguration = $this->configurationManager->getByPath($contentElementFieldPath);
}
}

private function calculateFileDimensions(): void
{
foreach ($this->files as $file) {
Expand All @@ -131,7 +105,6 @@ private function calculateFileDimensions(): void
private function calculateFileDimensionForBreakpoints(): array
{
$fileDimensions = [];
$fieldConfiguration = $this->contentElementFieldConfiguration;

$breakpoints = $this->getBreakpoints();

Expand All @@ -141,27 +114,10 @@ private function calculateFileDimensionForBreakpoints(): array
continue;
}

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

if (isset($fieldConfiguration['multiplier'])) {
$fileDimensions[$breakpoint->getIdentifier()]['size'] = $contentElementSize
* Multiplier::parse(
$fieldConfiguration['multiplier'][$breakpoint->getIdentifier()]
);

continue;
}

if (isset($fieldConfiguration['sizes'])) {
$fileDimensions[$breakpoint->getIdentifier()]['size'] = Multiplier::parse(
$fieldConfiguration['sizes'][$breakpoint->getIdentifier()]
);

continue;
}
}

return $fileDimensions;
Expand Down
87 changes: 87 additions & 0 deletions Classes/Sizes/AbstractContentElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Codappix\ResponsiveImages\Sizes;

/*
* Copyright (C) 2024 Daniel Gohlke <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

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

abstract class AbstractContentElement implements ContentElementInterface
{
protected ConfigurationManager $configurationManager;

protected int $colPos;

protected string $contentType;

protected array $data;

protected ContentElementInterface $parent;

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

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

public function getData(?string $dataIdentifier = null): mixed
{
if ($dataIdentifier === null) {
return $this->data;
}

if (isset($this->data[$dataIdentifier]) === false) {
throw new Exception('No data found for key ' . $dataIdentifier . ' in $this->data.');
}

return $this->data[$dataIdentifier];
}

public function getColPos(): int
{
return $this->colPos;
}

public function getContentType(): string
{
return $this->contentType;
}

public function getParent(): ?ContentElementInterface
{
return $this->parent;
}

public function setParent(ContentElementInterface $contentElement): void
{
if ($contentElement instanceof Container) {
$contentElement->setActiveColumn($contentElement->getColumn($this->colPos));
}

$this->parent = $contentElement;
}
}
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 int[]
*/
public function getSizes(): array
{
return $this->sizes;
}
}
31 changes: 19 additions & 12 deletions Classes/Sizes/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,18 @@
* 02110-1301, USA.
*/

use Codappix\ResponsiveImages\Configuration\ConfigurationManager;
use Codappix\ResponsiveImages\Sizes\BackendLayout\Column;
use TYPO3\CMS\Core\Utility\GeneralUtility;

final class Container extends ContentElement
final class Container extends AbstractContentElement
{
private readonly string $layout;

private array $columns = [];

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 All @@ -68,10 +58,27 @@ public function getActiveColumn(): Column
return $this->activeColumn;
}

/**
* @return float[]
*/
public function getMultiplier(): array
{
return $this->getActiveColumn()->getMultiplier();
}

/**
* @return int[]
*/
public function getSizes(): array
{
return $this->getActiveColumn()->getSizes();
}

private function determineColumns(): void
{
$sizesPath = implode('.', [
$this->layout,
'container',
$this->contentType,
'columns',
]);

Expand Down
Loading