Limit to 13.x

This commit is contained in:
Sebastian Fischer 2024-05-05 20:05:32 +02:00
parent f45e6b5c3b
commit 6cd429c808
3 changed files with 2 additions and 308 deletions

View File

@ -1,304 +0,0 @@
<?php
declare(strict_types=1);
namespace Evoweb\EwBase\Form\Element;
/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\ImageManipulation\Area;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3Fluid\Fluid\View\TemplateView;
use TYPO3Fluid\Fluid\View\ViewInterface;
class PickColorFromImagePre13 extends AbstractFormElement
{
/**
* Default element configuration
*
* @var array
*/
protected static array $defaultConfig = [
'imageField' => 'image',
'cropVariants' => [
'default' => [
'title' =>
'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.crop_variant.default',
'allowedAspectRatios' => [
'16:9' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.16_9',
'value' => 16 / 9,
],
'3:2' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.3_2',
'value' => 3 / 2,
],
'4:3' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.4_3',
'value' => 4 / 3,
],
'1:1' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.1_1',
'value' => 1.0,
],
'NaN' => [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_wizards.xlf:imwizard.ratio.free',
'value' => 0.0,
],
],
'selectedRatio' => 'NaN',
'cropArea' => [
'x' => 0.0,
'y' => 0.0,
'width' => 1.0,
'height' => 1.0,
],
],
],
];
/**
* Default field information enabled for this element.
*
* @var array
*/
protected $defaultFieldInformation = [
'tcaDescription' => [
'renderType' => 'tcaDescription',
],
];
/**
* Default field wizards enabled for this element.
*
* @var array
*/
protected $defaultFieldWizard = [
'localizationStateSelector' => [
'renderType' => 'localizationStateSelector',
],
'otherLanguageContent' => [
'renderType' => 'otherLanguageContent',
'after' => [
'localizationStateSelector',
],
],
'defaultLanguageDifferences' => [
'renderType' => 'defaultLanguageDifferences',
'after' => [
'otherLanguageContent',
],
],
];
protected ViewInterface $templateView;
public function __construct(NodeFactory $nodeFactory, array $data)
{
parent::__construct($nodeFactory, $data);
// Would be great, if we could inject the view here, but since the constructor is in the interface, we can't
// @todo: It's unfortunate we're using Typo3Fluid TemplateView directly here. We can't
// inject BackendViewFactory here since __construct() is polluted by NodeInterface.
// Remove __construct() from NodeInterface to have DI, then use BackendViewFactory here.
$view = GeneralUtility::makeInstance(TemplateView::class);
$templatePaths = $view->getRenderingContext()->getTemplatePaths();
$templatePaths
->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:ew_base/Resources/Private/Templates')]);
$templatePaths
->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:ew_base/Resources/Private/Partials')]);
$this->templateView = $view;
}
public function render(): array
{
$resultArray = $this->initializeResultArray();
$parameterArray = $this->data['parameterArray'];
$config = $this->populateConfiguration($parameterArray['fieldConf']['config']);
$fieldId = StringUtility::getUniqueId('formengine-color-');
$file = $this->getFileObject($this->data['databaseRow'], $config['imageField']);
if (!$file) {
$languageService = $this->getLanguageService();
$label = $languageService->sL(
'LLL:EXT:ew_base/Resources/Private/Language/locallang_db.xlf:imageField.empty'
);
$fieldLabel = $this->data['processedTca']['columns'][$config['imageField']]['label'];
$resultArray['html'] = '
<div class="form-control-wrap">
<div class="form-wizards-wrap">
<div class="form-wizards-element">
<typo3-formengine-element-pick-color recordFieldId="' . htmlspecialchars($fieldId) . '">
' . sprintf($label, '<strong>' . $fieldLabel . '</strong>') . '
</typo3-formengine-element-pick-color>
</div>
</div>
</div>
';
// Early return in case we do not find a file
return $resultArray;
}
$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
$fieldControlResult = $this->renderFieldControl();
$fieldControlHtml = $fieldControlResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
$fieldWizardResult = $this->renderFieldWizard();
$fieldWizardHtml = $fieldWizardResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
$width = $this->formMaxWidth(
MathUtility::forceIntegerInRange(
$config['size'] ?? $this->defaultInputWidth,
$this->minimumInputWidth,
$this->maxInputWidth
)
);
$arguments = [
'fieldInformation' => $fieldInformationHtml,
'fieldControl' => $fieldControlHtml,
'fieldWizard' => $fieldWizardHtml,
'isAllowedFileExtension' => in_array(
strtolower($file->getExtension()),
GeneralUtility::trimExplode(',', strtolower($config['allowedExtensions'])),
true
),
'image' => $file,
'formEngine' => [
'field' => [
'value' => $parameterArray['itemFormElValue'],
'name' => $parameterArray['itemFormElName'],
],
'validation' => '[]',
],
'config' => $config,
'width' => $width,
];
if ($arguments['isAllowedFileExtension']) {
$resultArray['stylesheetFiles'][] =
'EXT:ew_base/Resources/Public/JavaScript/form-engine/element/pick-color-from-image.css';
$resultArray['javaScriptModules'][] = JavaScriptModuleInstruction::create(
'@evoweb/ew-base/form-engine/element/pick-color-from-image.js',
'PickColorFromImage'
)->instance(
$fieldId,
$parameterArray['itemFormElValue'],
($parameterArray['fieldConf']['config']['readOnly'] ?? false)
);
$arguments['formEngine']['field']['id'] = $fieldId;
if ($config['required'] ?? false) {
$arguments['formEngine']['validation'] = $this->getValidationDataAsJsonString(['required' => true]);
}
}
$this->templateView->assignMultiple($arguments);
$resultArray['html'] = $this->templateView->render('Form/ImageManipulationElement');
return $resultArray;
}
protected function getFileObject(array $row, string $fieldName): ?File
{
$file = null;
$fileUid = !empty($row[$fieldName]) ? $row[$fieldName] : null;
if (is_array($fileUid) && isset($fileUid[0]['uid'])) {
$fileUid = $fileUid[0]['uid'];
}
if (MathUtility::canBeInterpretedAsInteger($fileUid)) {
try {
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
$fileReference = $resourceFactory->getFileReferenceObject($fileUid);
$file = $fileReference->getOriginalFile();
} catch (FileDoesNotExistException | \InvalidArgumentException) {
}
}
return $file;
}
protected function populateConfiguration(array $baseConfiguration): array
{
$defaultConfig = self::$defaultConfig;
$config = array_replace_recursive($defaultConfig, $baseConfiguration);
$imageConfig = $this->data['processedTca']['columns'][$config['imageField']];
$config['cropVariants'] = $imageConfig['config']['cropVariants'] ?? $defaultConfig['cropVariants'];
if (!is_array($config['cropVariants'])) {
throw new InvalidConfigurationException('Crop variants configuration must be an array', 1485377267);
}
$cropVariants = [];
foreach ($config['cropVariants'] as $id => $cropVariant) {
// Filter allowed aspect ratios
$cropVariant['allowedAspectRatios'] = array_filter(
$cropVariant['allowedAspectRatios'] ?? [],
static function ($aspectRatio) {
return !(bool)($aspectRatio['disabled'] ?? false);
}
);
// Ignore disabled crop variants
if (!empty($cropVariant['disabled'])) {
continue;
}
if (empty($cropVariant['allowedAspectRatios'])) {
throw new InvalidConfigurationException(
'Crop variants configuration ' . $id . ' contains no allowed aspect ratios',
1620147893
);
}
// Enforce a crop area (default is full image)
if (empty($cropVariant['cropArea'])) {
$cropVariant['cropArea'] = Area::createEmpty()->asArray();
}
$cropVariants[$id] = $cropVariant;
}
$config['cropVariants'] = $cropVariants;
$config['allowedExtensions'] ??= $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'];
return $config;
}
protected function processConfiguration(array $config, string &$elementValue, File $file): array
{
$cropVariantCollection = CropVariantCollection::create($elementValue, $config['cropVariants']);
if (empty($config['readOnly']) && !empty($file->getProperty('width'))) {
$cropVariantCollection = $cropVariantCollection->applyRatioRestrictionToSelectedCropArea($file);
$elementValue = (string)$cropVariantCollection;
}
$config['cropVariants'] = $cropVariantCollection->asArray();
$config['allowedExtensions'] = implode(
', ',
GeneralUtility::trimExplode(',', $config['allowedExtensions'], true)
);
return $config;
}
}

View File

@ -7,7 +7,7 @@
}
},
"require": {
"typo3/cms-core": "^12.4 || 12.4.x-dev || dev-main",
"typo3/cms-core": "^13.0 || 13.0.x-dev || dev-main",
"typo3/cms-backend": "*",
"typo3/cms-extbase": "*",

View File

@ -3,7 +3,6 @@
defined('TYPO3') or die('access denied');
use Evoweb\EwBase\Form\Element\PickColorFromImage;
use Evoweb\EwBase\Form\Element\PickColorFromImagePre13;
use Evoweb\EwBase\Form\FormDataProvider\UsercentricsDatabaseEditRow;
use Evoweb\EwBase\Form\FormDataProvider\UsercentricsTcaInline;
use Evoweb\EwBase\Hooks\UsercentricsPostRenderHook;
@ -22,11 +21,10 @@ call_user_func(function () {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess'][] =
UsercentricsPostRenderHook::class . '->executePostRenderHook';
$versionPre13 = (GeneralUtility::makeInstance(Typo3Version::class))->getMajorVersion() < 13;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][1681197508] = [
'nodeName' => 'pick-color-from-image',
'priority' => '70',
'class' => $versionPre13 ? PickColorFromImagePre13::class : PickColorFromImage::class,
'class' => PickColorFromImage::class,
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][BaseSiteDatabaseEditRow::class] = [