TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Condition;
|
||||
|
||||
use TYPO3\CMS\Core\ExpressionLanguage\AbstractProvider;
|
||||
use TYPO3\CMS\Form\Domain\Condition\Functions\FormConditionFunctionsProvider;
|
||||
|
||||
/**
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ConditionProvider extends AbstractProvider
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->expressionLanguageProviders = [
|
||||
FormConditionFunctionsProvider::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Condition\Functions;
|
||||
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
||||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
||||
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class FormConditionFunctionsProvider implements ExpressionFunctionProviderInterface
|
||||
{
|
||||
/**
|
||||
* @return ExpressionFunction[] An array of Function instances
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
$this->getFormValueFunction(),
|
||||
$this->getRootFormPropertyFunction(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut function to access field values
|
||||
*/
|
||||
protected function getFormValueFunction(): ExpressionFunction
|
||||
{
|
||||
return new ExpressionFunction(
|
||||
'getFormValue',
|
||||
static fn() => null, // Not implemented, we only use the evaluator
|
||||
static function ($arguments, $field, $default = null) {
|
||||
return $arguments['formValues'][$field] ?? $default;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRootFormPropertyFunction(): ExpressionFunction
|
||||
{
|
||||
return new ExpressionFunction(
|
||||
'getRootFormProperty',
|
||||
static fn() => null, // Not implemented, we only use the evaluator
|
||||
static function ($arguments, $property) {
|
||||
$formDefinition = $arguments['formRuntime']->getFormDefinition();
|
||||
try {
|
||||
$value = ObjectAccess::getPropertyPath($formDefinition, $property);
|
||||
} catch (\Exception) {
|
||||
$value = null;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing;
|
||||
|
||||
/**
|
||||
* Helper for array processing
|
||||
*
|
||||
* Scope: frontend / backend
|
||||
* @internal
|
||||
*/
|
||||
class ArrayProcessing
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $expression;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
protected $processor;
|
||||
|
||||
public function __construct(string $identifier, string $expression, callable $processor)
|
||||
{
|
||||
$this->identifier = $identifier;
|
||||
$this->expression = $expression;
|
||||
$this->processor = $processor;
|
||||
}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
public function getExpression(): string
|
||||
{
|
||||
return $this->expression;
|
||||
}
|
||||
|
||||
public function getProcessor(): callable
|
||||
{
|
||||
return $this->processor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\ArrayProcessorException;
|
||||
|
||||
/**
|
||||
* Helper for array processing
|
||||
*
|
||||
* Scope: frontend / backend
|
||||
* @internal
|
||||
*/
|
||||
class ArrayProcessor
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->data = ArrayUtility::flattenPlain($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ArrayProcessing[] $processings
|
||||
*/
|
||||
public function forEach(...$processings): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
$processings = $this->getValidProcessings($processings);
|
||||
foreach ($this->data as $key => $value) {
|
||||
foreach ($processings as $processing) {
|
||||
// explicitly escaping non-escaped '#' which is used
|
||||
// as PCRE delimiter in the following processing
|
||||
$expression = preg_replace(
|
||||
'/(?<!\\\\)#/',
|
||||
'\\#',
|
||||
$processing->getExpression()
|
||||
);
|
||||
|
||||
if (preg_match('#' . $expression . '#', $key, $matches)) {
|
||||
$identifier = $processing->getIdentifier();
|
||||
$processor = $processing->getProcessor();
|
||||
$result[$identifier] = $result[$identifier] ?? [];
|
||||
$result[$identifier][$key] = $processor($key, $value, $matches);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayProcessing[]
|
||||
* @throws ArrayProcessorException
|
||||
*/
|
||||
protected function getValidProcessings(array $allProcessings): array
|
||||
{
|
||||
$validProcessings = [];
|
||||
$identifiers = [];
|
||||
foreach ($allProcessings as $processing) {
|
||||
if ($processing instanceof ArrayProcessing) {
|
||||
if (in_array($processing->getIdentifier(), $identifiers, true)) {
|
||||
throw new ArrayProcessorException(
|
||||
'ArrayProcessing identifier must be unique.',
|
||||
1528638085
|
||||
);
|
||||
}
|
||||
$identifiers[] = $processing->getIdentifier();
|
||||
$validProcessings[] = $processing;
|
||||
}
|
||||
}
|
||||
return $validProcessings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,676 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface as ExtbaseConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PrototypeNotFoundException;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AdditionalElementPropertyPathsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\ExtractorDto;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\IsCreatableFormElementExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\MultiValuePropertiesExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\PredefinedDefaultsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\PropertyPathsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\SelectOptionsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\IsCreatablePropertyCollectionElementExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\MultiValuePropertiesExtractor as CollectionMultiValuePropertiesExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\PredefinedDefaultsExtractor as CollectionPredefinedDefaultsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\PropertyPathsExtractor as CollectionPropertyPathsExtractor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\SelectOptionsExtractor as CollectionSelectOptionsExtractor;
|
||||
use TYPO3\CMS\Form\Event\AfterFormDefinitionValidationConfigurationIsBuiltEvent;
|
||||
use TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManagerInterface as ExtFormConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Form\Service\TranslationService;
|
||||
|
||||
/**
|
||||
* Helper for configuration settings
|
||||
* Scope: frontend / backend
|
||||
*
|
||||
* @todo: Get rid of ConfigurationManagerInterface by handing over $formSettings to
|
||||
* methods instead to make the indirect dependency to Request explicit in consuming classes.
|
||||
* @todo: Declare readonly when ConfigurationService is no longer injected lazy, or wait
|
||||
* for PHP 8.3 minimum for symfony to allow both lazy and readonly dependencies.
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
class ConfigurationService
|
||||
{
|
||||
public function __construct(
|
||||
protected ExtbaseConfigurationManagerInterface $extbaseConfigurationManager,
|
||||
protected ExtFormConfigurationManagerInterface $extFormConfigurationManager,
|
||||
protected TranslationService $translationService,
|
||||
#[Autowire(service: 'cache.assets')]
|
||||
protected FrontendInterface $assetsCache,
|
||||
#[Autowire(service: 'cache.runtime')]
|
||||
protected FrontendInterface $runtimeCache,
|
||||
protected EventDispatcherInterface $eventDispatcher,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get the prototype configuration
|
||||
*
|
||||
* @param string $prototypeName name of the prototype to get the configuration for
|
||||
* @return array the prototype configuration
|
||||
* @throws PrototypeNotFoundException if prototype with the name $prototypeName was not found
|
||||
*/
|
||||
public function getPrototypeConfiguration(string $prototypeName): array
|
||||
{
|
||||
$formSettings = $this->getFormSettings();
|
||||
if (!isset($formSettings['prototypes'][$prototypeName])) {
|
||||
throw new PrototypeNotFoundException(sprintf('The Prototype "%s" was not found.', $prototypeName), 1475924277);
|
||||
}
|
||||
return $formSettings['prototypes'][$prototypeName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all prototype names which are defined within "formManager.selectablePrototypesConfiguration.*.identifier"
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getSelectablePrototypeNamesDefinedInFormEditorSetup(): array
|
||||
{
|
||||
$formSettings = $this->getFormSettings();
|
||||
$returnValue = GeneralUtility::makeInstance(
|
||||
ArrayProcessor::class,
|
||||
$formSettings['formManager']['selectablePrototypesConfiguration'] ?? []
|
||||
)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'selectablePrototypeNames',
|
||||
'^([\d]+)\.identifier$',
|
||||
static function ($_, $value) {
|
||||
return $value;
|
||||
}
|
||||
)
|
||||
);
|
||||
return array_values($returnValue['selectablePrototypeNames'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a form element property is defined in the form setup.
|
||||
* If a form element property is defined in the form setup then it
|
||||
* means that the form element property can be written by the form editor.
|
||||
* A form element property can be written if the property path is defined within
|
||||
* the following form editor properties:
|
||||
* * formElementsDefinition.<formElementType>.formEditor.editors.<index>.propertyPath
|
||||
* * formElementsDefinition.<formElementType>.formEditor.editors.<index>.*.propertyPath
|
||||
* * formElementsDefinition.<formElementType>.formEditor.editors.<index>.additionalElementPropertyPaths
|
||||
* * formElementsDefinition.<formElementType>.formEditor.propertyCollections.<finishers|validators>.<index>.editors.<index>.additionalElementPropertyPaths
|
||||
* If a form editor property "templateName" is
|
||||
* "Inspector-PropertyGridEditor" or "Inspector-MultiSelectEditor" or "Inspector-ValidationErrorMessageEditor"
|
||||
* it means that the form editor property "propertyPath" is interpreted as a so called "multiValueProperty".
|
||||
* A "multiValueProperty" can contain any subproperties relative to the value from "propertyPath" which are valid.
|
||||
* If "formElementsDefinition.<formElementType>.formEditor.editors.<index>.templateName = Inspector-PropertyGridEditor"
|
||||
* and
|
||||
* "formElementsDefinition.<formElementType>.formEditor.editors.<index>.propertyPath = options.xxx"
|
||||
* then (for example) "options.xxx.yyy" is a valid property path to write.
|
||||
* If you use a custom form editor "inspector editor" implementation which does not define the writable
|
||||
* property paths by one of the above described inspector editor properties (e.g "propertyPath") within
|
||||
* the form setup, you must provide the writable property paths via the
|
||||
* AfterFormDefinitionValidationConfigurationIsBuiltEvent PSR-14 event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isFormElementPropertyDefinedInFormEditorSetup(ValidationDto $dto): bool
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$subConfig = $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()] ?? [];
|
||||
return $this->isPropertyDefinedInFormEditorSetup($dto->getPropertyPath(), $subConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a form elements finisher|validator property is defined in the form setup.
|
||||
* If a form elements finisher|validator property is defined in the form setup then it
|
||||
* means that the form elements finisher|validator property can be written by the form editor.
|
||||
* A form elements finisher|validator property can be written if the property path is defined within
|
||||
* the following form editor properties:
|
||||
* * formElementsDefinition.<formElementType>.formEditor.propertyCollections.<finishers|validators>.<index>.editors.<index>.propertyPath
|
||||
* * formElementsDefinition.<formElementType>.formEditor.propertyCollections.<finishers|validators>.<index>.editors.<index>.*.propertyPath
|
||||
* If a form elements finisher|validator property "templateName" is
|
||||
* "Inspector-PropertyGridEditor" or "Inspector-MultiSelectEditor" or "Inspector-ValidationErrorMessageEditor"
|
||||
* it means that the form editor property "propertyPath" is interpreted as a so called "multiValueProperty".
|
||||
* A "multiValueProperty" can contain any subproperties relative to the value from "propertyPath" which are valid.
|
||||
* If "formElementsDefinition.<formElementType>.formEditor.propertyCollections.<finishers|validators>.<index>.editors.<index>.templateName = Inspector-PropertyGridEditor"
|
||||
* and
|
||||
* "formElementsDefinition.<formElementType>.formEditor.propertyCollections.<finishers|validators>.<index>.editors.<index>.propertyPath = options.xxx"
|
||||
* that (for example) "options.xxx.yyy" is a valid property path to write.
|
||||
* If you use a custom form elements finisher|validator editor implementation which does not define the writable
|
||||
* property paths by one of the above described inspector editor properties (e.g "propertyPath") within
|
||||
* the form setup, you must provide the writable property paths via the
|
||||
* AfterFormDefinitionValidationConfigurationIsBuiltEvent PSR-14 event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isPropertyCollectionPropertyDefinedInFormEditorSetup(ValidationDto $dto): bool
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$subConfig = $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()] ?? [];
|
||||
return $this->isPropertyDefinedInFormEditorSetup($dto->getPropertyPath(), $subConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* If a form element editor has a property called "selectOptions"
|
||||
* (e.g. editors with templateName "Inspector-SingleSelectEditor" or "Inspector-MultiSelectEditor")
|
||||
* then only the defined values within the selectOptions are allowed to be written
|
||||
* by the form editor.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function formElementPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup(
|
||||
ValidationDto $dto
|
||||
): bool {
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$propertyPath = $this->getBasePropertyPathFromMultiValueFormElementProperty($dto);
|
||||
return isset(
|
||||
$formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['selectOptions'][$propertyPath]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "selectOptions" value for a form element property from the form setup.
|
||||
*
|
||||
* @throws PropertyException
|
||||
* @internal
|
||||
*/
|
||||
public function getAllowedValuesForFormElementPropertyFromFormEditorSetup(
|
||||
ValidationDto $dto,
|
||||
bool $translated = true
|
||||
): array {
|
||||
if (!$this->formElementPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup($dto)) {
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
'No selectOptions found for form element type "%s" and property path "%s"',
|
||||
$dto->getFormElementType(),
|
||||
$dto->getPropertyPath()
|
||||
),
|
||||
1614264312
|
||||
);
|
||||
}
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$property = $translated ? 'selectOptions' : 'untranslatedSelectOptions';
|
||||
$propertyPath = $this->getBasePropertyPathFromMultiValueFormElementProperty($dto);
|
||||
return $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()][$property][$propertyPath];
|
||||
}
|
||||
|
||||
/**
|
||||
* If a form elements finisher|validator editor has a property called "selectOptions"
|
||||
* (e.g. editors with templateName "Inspector-SingleSelectEditor" or "Inspector-MultiSelectEditor")
|
||||
* then only the defined values within the selectOptions are allowed to be written
|
||||
* by the form editor.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function propertyCollectionPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup(
|
||||
ValidationDto $dto
|
||||
): bool {
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$propertyPath = $this->getBasePropertyPathFromMultiValuePropertyCollectionElement($dto);
|
||||
return isset(
|
||||
$formDefinitionValidationConfiguration['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()]['selectOptions'][$propertyPath]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "selectOptions" value for a form elements finisher|validator property from the form setup.
|
||||
*
|
||||
* @throws PropertyException
|
||||
* @internal
|
||||
*/
|
||||
public function getAllowedValuesForPropertyCollectionPropertyFromFormEditorSetup(
|
||||
ValidationDto $dto,
|
||||
bool $translated = true
|
||||
): array {
|
||||
if (!$this->propertyCollectionPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup($dto)) {
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
'No selectOptions found for property collection "%s" and identifier "%s" and property path "%s"',
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier(),
|
||||
$dto->getPropertyPath()
|
||||
),
|
||||
1614264313
|
||||
);
|
||||
}
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$property = $translated ? 'selectOptions' : 'untranslatedSelectOptions';
|
||||
$propertyPath = $this->getBasePropertyPathFromMultiValuePropertyCollectionElement($dto);
|
||||
return $formDefinitionValidationConfiguration['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()][$property][$propertyPath];
|
||||
}
|
||||
|
||||
protected function getBasePropertyPathFromMultiValueFormElementProperty(ValidationDto $dto): string
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$propertyPath = $dto->getPropertyPath();
|
||||
$multiValueProperties = $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['multiValueProperties'] ?? [];
|
||||
foreach ($multiValueProperties as $multiValueProperty) {
|
||||
if (str_starts_with($propertyPath, $multiValueProperty)) {
|
||||
$propertyPath = $multiValueProperty;
|
||||
}
|
||||
}
|
||||
return $propertyPath;
|
||||
}
|
||||
|
||||
protected function getBasePropertyPathFromMultiValuePropertyCollectionElement(ValidationDto $dto): string
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$propertyPath = $dto->getPropertyPath();
|
||||
$multiValueProperties = $formDefinitionValidationConfiguration['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()]['multiValueProperties'] ?? [];
|
||||
foreach ($multiValueProperties as $multiValueProperty) {
|
||||
if (str_starts_with($propertyPath, $multiValueProperty)) {
|
||||
$propertyPath = $multiValueProperty;
|
||||
}
|
||||
}
|
||||
return $propertyPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a form element property is defined in "predefinedDefaults" in the form setup.
|
||||
* If a form element property is defined in the "predefinedDefaults" in the form setup then it
|
||||
* means that the form element property can be written by the form editor.
|
||||
* A form element default property is defined within the following form editor properties:
|
||||
* * formElementsDefinition.<formElementType>.formEditor.predefinedDefaults.<propertyPath> = "default value"
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup(ValidationDto $dto): bool
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
return isset(
|
||||
$formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['predefinedDefaults'][$dto->getPropertyPath()]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "predefinedDefaults" value for a form element property from the form setup.
|
||||
* A form element default property is defined within the following form editor properties:
|
||||
* * formElementsDefinition.<formElementType>.formEditor.predefinedDefaults.<propertyPath> = "default value"
|
||||
*
|
||||
* @throws PropertyException
|
||||
* @internal
|
||||
*/
|
||||
public function getFormElementPredefinedDefaultValueFromFormEditorSetup(ValidationDto $dto, bool $translated = true): mixed
|
||||
{
|
||||
if (!$this->isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup($dto)) {
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
'No predefinedDefaults found for form element type "%s" and property path "%s"',
|
||||
$dto->getFormElementType(),
|
||||
$dto->getPropertyPath()
|
||||
),
|
||||
1528578401
|
||||
);
|
||||
}
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$property = $translated ? 'predefinedDefaults' : 'untranslatedPredefinedDefaults';
|
||||
return $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()][$property][$dto->getPropertyPath()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a form elements finisher|validator property is defined in "predefinedDefaults" in the form setup.
|
||||
* If a form elements finisher|validator property is defined in "predefinedDefaults" in the form setup then it
|
||||
* means that the form elements finisher|validator property can be written by the form editor.
|
||||
* A form elements finisher|validator default property is defined within the following form editor properties:
|
||||
* * <validatorsDefinition|finishersDefinition>.<index>.formEditor.predefinedDefaults.<propertyPath> = "default value"
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup(ValidationDto $dto): bool
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
return isset(
|
||||
$formDefinitionValidationConfiguration['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()]['predefinedDefaults'][$dto->getPropertyPath()]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "predefinedDefaults" value for a form elements finisher|validator property from the form setup.
|
||||
* A form elements finisher|validator default property is defined within the following form editor properties:
|
||||
* * <validatorsDefinition|finishersDefinition>.<index>.formEditor.predefinedDefaults.<propertyPath> = "default value"
|
||||
*
|
||||
* @throws PropertyException
|
||||
* @internal
|
||||
*/
|
||||
public function getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup(ValidationDto $dto, bool $translated = true): mixed
|
||||
{
|
||||
if (!$this->isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup($dto)) {
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
'No predefinedDefaults found for property collection "%s" and identifier "%s" and property path "%s"',
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier(),
|
||||
$dto->getPropertyPath()
|
||||
),
|
||||
1528578402
|
||||
);
|
||||
}
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
$property = $translated ? 'predefinedDefaults' : 'untranslatedPredefinedDefaults';
|
||||
return $formDefinitionValidationConfiguration['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()][$property][$dto->getPropertyPath()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the form element is creatable through the form editor.
|
||||
* A form element is creatable if the following properties are set:
|
||||
* * formElementsDefinition.<formElementType>.formEditor.group
|
||||
* * formElementsDefinition.<formElementType>.formEditor.groupSorting
|
||||
* And the value from "formElementsDefinition.<formElementType>.formEditor.group" is
|
||||
* one of the keys within "formEditor.formElementGroups"
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isFormElementTypeCreatableByFormEditor(ValidationDto $dto): bool
|
||||
{
|
||||
if ($dto->getFormElementType() === 'Form') {
|
||||
return true;
|
||||
}
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
return $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['creatable'] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the form elements finisher|validator is creatable through the form editor.
|
||||
* A form elements finisher|validator is creatable if the following conditions are true:
|
||||
* "formElementsDefinition.<formElementType>.formEditor.editors.<index>.templateName = Inspector-FinishersEditor"
|
||||
* or
|
||||
* "formElementsDefinition.<formElementType>.formEditor.editors.<index>.templateName = Inspector-ValidatorsEditor"
|
||||
* and
|
||||
* "formElementsDefinition.<formElementType>.formEditor.editors.<index>.selectOptions.<index>.value = <finisherIdentifier|validatorIdentifier>"
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isPropertyCollectionElementIdentifierCreatableByFormEditor(ValidationDto $dto): bool
|
||||
{
|
||||
$formDefinitionValidationConfiguration = $this->buildFormDefinitionValidationConfigurationFromFormEditorSetup(
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
return $formDefinitionValidationConfiguration['formElements'][$dto->getFormElementType()]['collections'][$dto->getPropertyCollectionName()][$dto->getPropertyCollectionElementIdentifier()]['creatable'] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the form elements type is defined within the form setup.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isFormElementTypeDefinedInFormSetup(ValidationDto $dto): bool
|
||||
{
|
||||
$prototypeConfiguration = $this->getPrototypeConfiguration($dto->getPrototypeName());
|
||||
return ArrayUtility::isValidPath(
|
||||
$prototypeConfiguration,
|
||||
'formElementsDefinition.' . $dto->getFormElementType(),
|
||||
'.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getAllBackendTranslationsForTranslationKeys(array $keys, string $prototypeName): array
|
||||
{
|
||||
$translations = [];
|
||||
foreach ($keys as $key) {
|
||||
if (!is_string($key)) {
|
||||
continue;
|
||||
}
|
||||
$translations[$key] = $this->getAllBackendTranslationsForTranslationKey($key, $prototypeName);
|
||||
}
|
||||
return $translations;
|
||||
}
|
||||
|
||||
public function getAllBackendTranslationsForTranslationKey(string $key, string $prototypeName): array
|
||||
{
|
||||
$prototypeConfiguration = $this->getPrototypeConfiguration($prototypeName);
|
||||
return $this->translationService->translateToAllBackendLanguages(
|
||||
$key,
|
||||
[],
|
||||
$prototypeConfiguration['formEditor']['translationFiles'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
protected function getFormSettings(): array
|
||||
{
|
||||
// @todo: This is needed for extFormConfigurationManager to apply stdWrap on TS configuration.
|
||||
// Find a way to get rid of this.
|
||||
$isFrontend = false;
|
||||
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
|
||||
if ($request instanceof ServerRequestInterface) {
|
||||
$isFrontend = ApplicationType::fromRequest($request)->isFrontend();
|
||||
}
|
||||
// @todo: Note this code relies on the fact that the request has been set to ExtbaseConfigurationManagerInterface already.
|
||||
$typoScriptSettings = $this->extbaseConfigurationManager->getConfiguration(ExtbaseConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'form');
|
||||
return $this->extFormConfigurationManager->getYamlConfiguration($typoScriptSettings, $isFrontend, $isFrontend ? $request : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all the form editor configurations which are needed to check if a
|
||||
* form definition property can be written or not.
|
||||
*/
|
||||
protected function buildFormDefinitionValidationConfigurationFromFormEditorSetup(string $prototypeName): array
|
||||
{
|
||||
$cacheKey = implode('_', ['buildFormDefinitionValidationConfigurationFromFormEditorSetup', $prototypeName]);
|
||||
$configuration = $this->getCacheEntry($cacheKey);
|
||||
if ($configuration === null) {
|
||||
$prototypeConfiguration = $this->getPrototypeConfiguration($prototypeName);
|
||||
$extractorDto = GeneralUtility::makeInstance(ExtractorDto::class, $prototypeConfiguration);
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $prototypeConfiguration)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementPropertyPaths',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.editors\.([\d]+)\.(propertyPath|.*\.propertyPath)$',
|
||||
GeneralUtility::makeInstance(PropertyPathsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementAdditionalElementPropertyPaths',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.editors\.([\d]+)\.additionalElementPropertyPaths\.([\d]+)',
|
||||
GeneralUtility::makeInstance(AdditionalElementPropertyPathsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementRelativeMultiValueProperties',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.editors\.([\d]+)\.templateName$',
|
||||
GeneralUtility::makeInstance(MultiValuePropertiesExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementSelectOptions',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.editors\.([\d]+)\.selectOptions\.([\d]+)\.value$',
|
||||
GeneralUtility::makeInstance(SelectOptionsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementPredefinedDefaults',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.predefinedDefaults\.(.+)$',
|
||||
GeneralUtility::makeInstance(PredefinedDefaultsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'formElementCreatable',
|
||||
'^formElementsDefinition\.(.*)\.formEditor.group$',
|
||||
GeneralUtility::makeInstance(IsCreatableFormElementExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionCreatable',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.editors\.([\d]+)\.templateName$',
|
||||
GeneralUtility::makeInstance(IsCreatablePropertyCollectionElementExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionPropertyPaths',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.propertyCollections\.(finishers|validators)\.([\d]+)\.editors\.([\d]+)\.(propertyPath|.*\.propertyPath)$',
|
||||
GeneralUtility::makeInstance(CollectionPropertyPathsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionAdditionalElementPropertyPaths',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.propertyCollections\.(finishers|validators)\.([\d]+)\.editors\.([\d]+)\.additionalElementPropertyPaths\.([\d]+)',
|
||||
GeneralUtility::makeInstance(AdditionalElementPropertyPathsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionRelativeMultiValueProperties',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.propertyCollections\.(finishers|validators)\.([\d]+)\.editors\.([\d]+)\.templateName$',
|
||||
GeneralUtility::makeInstance(CollectionMultiValuePropertiesExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionSelectOptions',
|
||||
'^formElementsDefinition\.(.*)\.formEditor\.propertyCollections\.(finishers|validators)\.([\d]+)\.editors\.([\d]+)\.selectOptions\.([\d]+)\.value$',
|
||||
GeneralUtility::makeInstance(CollectionSelectOptionsExtractor::class, $extractorDto)
|
||||
),
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'propertyCollectionPredefinedDefaults',
|
||||
'^(validatorsDefinition|finishersDefinition)\.(.*)\.formEditor\.predefinedDefaults\.(.+)$',
|
||||
GeneralUtility::makeInstance(CollectionPredefinedDefaultsExtractor::class, $extractorDto)
|
||||
)
|
||||
);
|
||||
$configuration = $extractorDto->getResult();
|
||||
$configuration = $this->translateValues($prototypeConfiguration, $configuration);
|
||||
$configuration = $this->eventDispatcher
|
||||
->dispatch(new AfterFormDefinitionValidationConfigurationIsBuiltEvent($prototypeName, $configuration))
|
||||
->getConfiguration();
|
||||
$this->setCacheEntry($cacheKey, $configuration);
|
||||
}
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
protected function isPropertyDefinedInFormEditorSetup(string $propertyPath, array $subConfig): bool
|
||||
{
|
||||
if (empty($subConfig)) {
|
||||
return false;
|
||||
}
|
||||
if (in_array($propertyPath, $subConfig['propertyPaths'] ?? [], true)
|
||||
|| in_array($propertyPath, $subConfig['additionalElementPropertyPaths'] ?? [], true)
|
||||
|| in_array($propertyPath, $subConfig['additionalPropertyPaths'] ?? [], true)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
foreach ($subConfig['multiValueProperties'] ?? [] as $relativeMultiValueProperty) {
|
||||
if (str_starts_with($propertyPath, $relativeMultiValueProperty)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function translateValues(array $prototypeConfiguration, array $configuration): array
|
||||
{
|
||||
if (isset($configuration['formElements'])) {
|
||||
$configuration['formElements'] = $this->translatePredefinedDefaults(
|
||||
$prototypeConfiguration,
|
||||
$configuration['formElements']
|
||||
);
|
||||
$configuration['formElements'] = $this->translateSelectOptions(
|
||||
$prototypeConfiguration,
|
||||
$configuration['formElements']
|
||||
);
|
||||
}
|
||||
foreach ($configuration['collections'] ?? [] as $name => $collections) {
|
||||
$configuration['collections'][$name] = $this->translatePredefinedDefaults($prototypeConfiguration, $collections);
|
||||
$configuration['collections'][$name] = $this->translateSelectOptions($prototypeConfiguration, $configuration['collections'][$name]);
|
||||
}
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
protected function translatePredefinedDefaults(array $prototypeConfiguration, array $formElements): array
|
||||
{
|
||||
foreach ($formElements as $name => $formElement) {
|
||||
if (!isset($formElement['predefinedDefaults'])) {
|
||||
continue;
|
||||
}
|
||||
$formElement['untranslatedPredefinedDefaults'] = $formElement['predefinedDefaults'];
|
||||
$formElement['predefinedDefaults'] = $this->translationService->translateValuesRecursive(
|
||||
$formElement['predefinedDefaults'],
|
||||
$prototypeConfiguration['formEditor']['translationFiles'] ?? []
|
||||
);
|
||||
$formElements[$name] = $formElement;
|
||||
}
|
||||
return $formElements;
|
||||
}
|
||||
|
||||
protected function translateSelectOptions(array $prototypeConfiguration, array $formElements): array
|
||||
{
|
||||
foreach ($formElements as $name => $formElement) {
|
||||
if (empty($formElement['selectOptions']) || !is_array($formElement['selectOptions'])) {
|
||||
continue;
|
||||
}
|
||||
$formElement['untranslatedSelectOptions'] = $formElement['selectOptions'];
|
||||
$formElement['selectOptions'] = $this->translationService->translateValuesRecursive(
|
||||
$formElement['selectOptions'],
|
||||
$prototypeConfiguration['formEditor']['translationFiles'] ?? []
|
||||
);
|
||||
$formElements[$name] = $formElement;
|
||||
}
|
||||
return $formElements;
|
||||
}
|
||||
|
||||
protected function getCacheEntry(string $cacheKey): mixed
|
||||
{
|
||||
$cacheKey = 'form_' . $cacheKey;
|
||||
if ($this->runtimeCache->has($cacheKey)) {
|
||||
return $this->runtimeCache->get($cacheKey);
|
||||
}
|
||||
if ($this->assetsCache->has($cacheKey)) {
|
||||
return $this->assetsCache->get($cacheKey);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function setCacheEntry(string $cacheKey, mixed $value): void
|
||||
{
|
||||
$cacheKey = 'form_' . $cacheKey;
|
||||
$this->runtimeCache->set($cacheKey, $value);
|
||||
$this->assetsCache->set($cacheKey, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ArrayProcessorException extends Exception {}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a form setup property was not found.
|
||||
*/
|
||||
class PropertyException extends Exception {}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a form prototype for a given name was not found.
|
||||
*/
|
||||
class PrototypeNotFoundException extends Exception {}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FlexformConfiguration\Processors;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractProcessor implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @var ProcessorDto
|
||||
*/
|
||||
protected $converterDto;
|
||||
|
||||
public function __construct(ProcessorDto $converterDto)
|
||||
{
|
||||
$this->converterDto = $converterDto;
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FlexformConfiguration\Processors;
|
||||
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
|
||||
|
||||
/**
|
||||
* Generate a FlexForm element for a finisher option
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class FinisherOptionGenerator extends AbstractProcessor
|
||||
{
|
||||
/**
|
||||
* @param string $_ unused in this context
|
||||
* @param mixed $__ unused in this context
|
||||
* @param array $matches the expression matches from the ArrayProcessor - for example matches of ^(.*)\.config\.type$
|
||||
*/
|
||||
public function __invoke(string $_, $__, array $matches)
|
||||
{
|
||||
[, $optionKey] = $matches;
|
||||
|
||||
$finisherIdentifier = $this->converterDto->getFinisherIdentifier();
|
||||
$finisherDefinitionFromSetup = $this->converterDto->getFinisherDefinitionFromSetup();
|
||||
$finisherDefinitionFromFormDefinition = $this->converterDto->getFinisherDefinitionFromFormDefinition();
|
||||
|
||||
try {
|
||||
$elementConfiguration = ArrayUtility::getValueByPath(
|
||||
$finisherDefinitionFromSetup['FormEngine']['elements'],
|
||||
$optionKey,
|
||||
'.'
|
||||
);
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
return;
|
||||
}
|
||||
|
||||
// use the option value from the ext:form setup from the current finisher as default value
|
||||
try {
|
||||
$optionValue = ArrayUtility::getValueByPath(
|
||||
$finisherDefinitionFromSetup,
|
||||
sprintf('options.%s', $optionKey),
|
||||
'.'
|
||||
);
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
$optionValue = null;
|
||||
}
|
||||
|
||||
// use the option value from the form definition from the current finisher (if exists) as default value
|
||||
try {
|
||||
$optionValue = ArrayUtility::getValueByPath(
|
||||
$finisherDefinitionFromFormDefinition,
|
||||
sprintf('options.%s', $optionKey),
|
||||
'.'
|
||||
);
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
}
|
||||
|
||||
if (isset($elementConfiguration['config'])) {
|
||||
$elementConfiguration['config']['default'] = $optionValue;
|
||||
}
|
||||
|
||||
$languageService = $this->getLanguageService();
|
||||
$elementConfiguration['label'] = (string)($elementConfiguration['label'] ?? '');
|
||||
if (empty($optionValue)) {
|
||||
$optionValue = $languageService->sL('LLL:EXT:form/Resources/Private/Language/Database.xlf:empty');
|
||||
} elseif (is_array($optionValue)) {
|
||||
$optionValue = implode(',', $optionValue);
|
||||
}
|
||||
$elementConfiguration['label'] .= sprintf(' (%s: "%s")', $languageService->sL('LLL:EXT:form/Resources/Private/Language/Database.xlf:default'), $optionValue);
|
||||
|
||||
$sheetElements = $this->converterDto->getResult();
|
||||
$sheetElements['settings.finishers.' . $finisherIdentifier . '.' . $optionKey] = $elementConfiguration;
|
||||
|
||||
$this->converterDto->setResult($sheetElements);
|
||||
}
|
||||
|
||||
protected function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FlexformConfiguration\Processors;
|
||||
|
||||
/**
|
||||
* Data container for finisher FlexForm processing
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ProcessorDto
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $finisherIdentifier;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $finisherDefinitionFromSetup;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $finisherDefinitionFromFormDefinition;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $result = [];
|
||||
|
||||
public function __construct(
|
||||
string $finisherIdentifier,
|
||||
array $finisherDefinitionFromSetup,
|
||||
array $finisherDefinitionFromFormDefinition
|
||||
) {
|
||||
$this->finisherIdentifier = $finisherIdentifier;
|
||||
$this->finisherDefinitionFromSetup = $finisherDefinitionFromSetup;
|
||||
$this->finisherDefinitionFromFormDefinition = $finisherDefinitionFromFormDefinition;
|
||||
}
|
||||
|
||||
public function getFinisherIdentifier(): string
|
||||
{
|
||||
return $this->finisherIdentifier;
|
||||
}
|
||||
|
||||
public function getFinisherDefinitionFromSetup(): array
|
||||
{
|
||||
return $this->finisherDefinitionFromSetup;
|
||||
}
|
||||
|
||||
public function getFinisherDefinitionFromFormDefinition(): array
|
||||
{
|
||||
return $this->finisherDefinitionFromFormDefinition;
|
||||
}
|
||||
|
||||
public function getResult(): array
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function setResult(array $result): ProcessorDto
|
||||
{
|
||||
$this->result = $result;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FlexformConfiguration\Processors;
|
||||
|
||||
/**
|
||||
* Interface for FlexForm processors
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
interface ProcessorInterface
|
||||
{
|
||||
public function __construct(ProcessorDto $converterDto);
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value, array $matches);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractConverter implements ConverterInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $sessionToken;
|
||||
|
||||
/**
|
||||
* @var ConverterDto
|
||||
*/
|
||||
protected $converterDto;
|
||||
|
||||
public function __construct(ConverterDto $converterDto, string $sessionToken = '')
|
||||
{
|
||||
$this->converterDto = $converterDto;
|
||||
$this->sessionToken = $sessionToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class AddHmacDataConverter extends AbstractConverter
|
||||
{
|
||||
/**
|
||||
* Add a new value "_orig_<propertyName>" as a sibling of the property key.
|
||||
* "_orig_<propertyName>" is an array which contains the property value
|
||||
* and a hmac hash for the property value.
|
||||
* "_orig_<propertyName>" will be used to validate the form definition on saving.
|
||||
* @see \TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService::validateFormDefinitionProperties()
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$formDefinition = $this->converterDto->getFormDefinition();
|
||||
|
||||
$renderablePathParts = explode('.', $key);
|
||||
array_pop($renderablePathParts);
|
||||
|
||||
if (count($renderablePathParts) > 1) {
|
||||
$renderablePath = implode('.', $renderablePathParts);
|
||||
$currentFormElement = ArrayUtility::getValueByPath($formDefinition, $renderablePath, '.');
|
||||
} else {
|
||||
$currentFormElement = $formDefinition;
|
||||
}
|
||||
|
||||
$propertyCollectionElements = $currentFormElement['finishers'] ?? $currentFormElement['validators'] ?? [];
|
||||
$propertyCollectionName = $currentFormElement['type'] === 'Form' ? 'finishers' : 'validators';
|
||||
unset($currentFormElement['renderables'], $currentFormElement['finishers'], $currentFormElement['validators']);
|
||||
|
||||
$this->converterDto
|
||||
->setRenderablePathParts($renderablePathParts)
|
||||
->setFormElementIdentifier($value);
|
||||
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $currentFormElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'addHmacData',
|
||||
'^.*',
|
||||
GeneralUtility::makeInstance(
|
||||
AddHmacDataToFormElementPropertyConverter::class,
|
||||
$this->converterDto,
|
||||
$this->sessionToken
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->converterDto->setPropertyCollectionName($propertyCollectionName);
|
||||
foreach ($propertyCollectionElements as $propertyCollectionIndex => $propertyCollectionElement) {
|
||||
$this->converterDto
|
||||
->setPropertyCollectionIndex((int)$propertyCollectionIndex)
|
||||
->setPropertyCollectionElementIdentifier($propertyCollectionElement['identifier']);
|
||||
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $propertyCollectionElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'addHmacData',
|
||||
'^(?!(.*\._label|.*\._value)$).*',
|
||||
GeneralUtility::makeInstance(
|
||||
AddHmacDataToPropertyCollectionElementConverter::class,
|
||||
$this->converterDto,
|
||||
$this->sessionToken
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class AddHmacDataToFormElementPropertyConverter extends AbstractConverter
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$formDefinition = $this->converterDto->getFormDefinition();
|
||||
|
||||
$propertyPathParts = explode('.', $key);
|
||||
$lastKeySegment = array_pop($propertyPathParts);
|
||||
$propertyPathParts[] = '_orig_' . $lastKeySegment;
|
||||
|
||||
$hashService = GeneralUtility::makeInstance(HashService::class);
|
||||
$hmacValuePath = implode('.', array_merge($this->converterDto->getRenderablePathParts(), $propertyPathParts));
|
||||
$hmacValue = [
|
||||
'value' => $value,
|
||||
'hmac' => $hashService->hmac(serialize([$this->converterDto->getFormElementIdentifier(), $key, $value]), $this->sessionToken),
|
||||
];
|
||||
|
||||
$formDefinition = ArrayUtility::setValueByPath($formDefinition, $hmacValuePath, $hmacValue, '.');
|
||||
|
||||
$this->converterDto->setFormDefinition($formDefinition);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class AddHmacDataToPropertyCollectionElementConverter extends AbstractConverter
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$formDefinition = $this->converterDto->getFormDefinition();
|
||||
|
||||
$propertyPathParts = explode('.', $key);
|
||||
$lastKeySegment = array_pop($propertyPathParts);
|
||||
$propertyPathParts[] = '_orig_' . $lastKeySegment;
|
||||
|
||||
$hmacValuePath = implode('.', array_merge(
|
||||
$this->converterDto->getRenderablePathParts(),
|
||||
[$this->converterDto->getPropertyCollectionName(), $this->converterDto->getPropertyCollectionIndex()],
|
||||
$propertyPathParts
|
||||
));
|
||||
|
||||
$hashService = GeneralUtility::makeInstance(HashService::class);
|
||||
$hmacValue = [
|
||||
'value' => $value,
|
||||
'hmac' => $hashService->hmac(
|
||||
serialize([
|
||||
$this->converterDto->getFormElementIdentifier(),
|
||||
$this->converterDto->getPropertyCollectionName(),
|
||||
$this->converterDto->getPropertyCollectionElementIdentifier(),
|
||||
$key,
|
||||
$value,
|
||||
]),
|
||||
$this->sessionToken
|
||||
),
|
||||
];
|
||||
|
||||
$formDefinition = ArrayUtility::setValueByPath($formDefinition, $hmacValuePath, $hmacValue, '.');
|
||||
|
||||
$this->converterDto->setFormDefinition($formDefinition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ConverterDto
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $formDefinition = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $renderablePathParts = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $formElementIdentifier = '';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $propertyCollectionIndex = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $propertyCollectionName = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $propertyCollectionElementIdentifier = '';
|
||||
|
||||
public function __construct(array $formDefinition)
|
||||
{
|
||||
$this->formDefinition = $formDefinition;
|
||||
}
|
||||
|
||||
public function getFormDefinition(): array
|
||||
{
|
||||
return $this->formDefinition;
|
||||
}
|
||||
|
||||
public function setFormDefinition(array $formDefinition): ConverterDto
|
||||
{
|
||||
$this->formDefinition = $formDefinition;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRenderablePathParts(): array
|
||||
{
|
||||
return $this->renderablePathParts;
|
||||
}
|
||||
|
||||
public function setRenderablePathParts(array $renderablePathParts): ConverterDto
|
||||
{
|
||||
$this->renderablePathParts = $renderablePathParts;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFormElementIdentifier(): string
|
||||
{
|
||||
return $this->formElementIdentifier;
|
||||
}
|
||||
|
||||
public function setFormElementIdentifier(string $formElementIdentifier): ConverterDto
|
||||
{
|
||||
$this->formElementIdentifier = $formElementIdentifier;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPropertyCollectionIndex(): int
|
||||
{
|
||||
return $this->propertyCollectionIndex;
|
||||
}
|
||||
|
||||
public function setPropertyCollectionIndex(int $propertyCollectionIndex): ConverterDto
|
||||
{
|
||||
$this->propertyCollectionIndex = $propertyCollectionIndex;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPropertyCollectionName(): string
|
||||
{
|
||||
return $this->propertyCollectionName;
|
||||
}
|
||||
|
||||
public function setPropertyCollectionName(string $propertyCollectionName): ConverterDto
|
||||
{
|
||||
$this->propertyCollectionName = $propertyCollectionName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPropertyCollectionElementIdentifier(): string
|
||||
{
|
||||
return $this->propertyCollectionElementIdentifier;
|
||||
}
|
||||
|
||||
public function setPropertyCollectionElementIdentifier(string $propertyCollectionElementIdentifier): ConverterDto
|
||||
{
|
||||
$this->propertyCollectionElementIdentifier = $propertyCollectionElementIdentifier;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface ConverterInterface
|
||||
{
|
||||
public function __construct(ConverterDto $converterDto, string $sessionToken = '');
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value);
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
|
||||
|
||||
/**
|
||||
* Apply FlexForm finisher option overrides
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class FinisherOptionsFlexFormOverridesConverter
|
||||
{
|
||||
/**
|
||||
* @var FlexFormFinisherOverridesConverterDto
|
||||
*/
|
||||
protected $converterDto;
|
||||
|
||||
public function __construct(FlexFormFinisherOverridesConverterDto $converterDto)
|
||||
{
|
||||
$this->converterDto = $converterDto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for overriding finisher options with flexform settings
|
||||
* Flexform settings "win": When a setting is set in the form
|
||||
* definition and in flexform the one in flexform will overwrite the
|
||||
* one defined in the form definition.
|
||||
*
|
||||
* Here we adjust the parsed configuration and apply the overrides.
|
||||
*
|
||||
* @param string $_ unused in this context
|
||||
* @param mixed $__ unused in this context
|
||||
* @param array $matches the expression matches from the ArrayProcessor - for example matches of ^(.*)\.config\.type$
|
||||
*/
|
||||
public function __invoke(string $_, $__, array $matches): void
|
||||
{
|
||||
[, $optionKey] = $matches;
|
||||
$prototypeFinisherDefinition = $this->converterDto->getPrototypeFinisherDefinition();
|
||||
$finisherDefinition = $this->converterDto->getFinisherDefinition();
|
||||
$finisherIdentifier = $this->converterDto->getFinisherIdentifier();
|
||||
$flexFormSheetSettings = $this->converterDto->getFlexFormSheetSettings();
|
||||
|
||||
try {
|
||||
$value = ArrayUtility::getValueByPath(
|
||||
$flexFormSheetSettings['finishers'][$finisherIdentifier],
|
||||
$optionKey,
|
||||
'.'
|
||||
);
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fieldConfiguration = $prototypeFinisherDefinition['FormEngine']['elements'][$optionKey] ?? [];
|
||||
|
||||
if ($fieldConfiguration['section'] ?? false) {
|
||||
if (!is_array($value) || $value === []) {
|
||||
// Do not process empty values for sections
|
||||
return;
|
||||
}
|
||||
|
||||
$processedOptionValue = [];
|
||||
|
||||
foreach ($value as $optionListValue) {
|
||||
$key = $optionListValue[$fieldConfiguration['sectionItemKey']];
|
||||
$value = $optionListValue[$fieldConfiguration['sectionItemValue']];
|
||||
$processedOptionValue[$key] = $value;
|
||||
}
|
||||
|
||||
$value = $processedOptionValue;
|
||||
}
|
||||
|
||||
$optionPath = 'options.' . $optionKey;
|
||||
|
||||
// Skip additional translation for finisher options that were changed via flexform
|
||||
if ($this->optionValueHasChanged($finisherDefinition, $optionPath, $value)) {
|
||||
$finisherDefinition['options']['translation']['propertiesExcludedFromTranslation'][] = $optionKey;
|
||||
}
|
||||
|
||||
$finisherDefinition = ArrayUtility::setValueByPath($finisherDefinition, $optionPath, $value, '.');
|
||||
|
||||
$this->converterDto->setFinisherDefinition($finisherDefinition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if finisher option value differs from finisher definition.
|
||||
*
|
||||
* Compares the given finisher option value with the corresponding value in the
|
||||
* finisher definition. Returns `true` if both values are equal, `false` otherwise.
|
||||
*
|
||||
* @param array<string, mixed> $finisherDefinition
|
||||
*/
|
||||
protected function optionValueHasChanged(array $finisherDefinition, string $optionPath, mixed $value): bool
|
||||
{
|
||||
try {
|
||||
return $value !== ArrayUtility::getValueByPath($finisherDefinition, $optionPath, '.');
|
||||
} catch (MissingArrayPathException) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class FinisherTranslationLanguageConverter extends AbstractConverter
|
||||
{
|
||||
/**
|
||||
* If "finishers.x.options.translation.language" is empty then set the value to "default" and remove
|
||||
* the hmac.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
if (!empty($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formDefinition = $this->converterDto->getFormDefinition();
|
||||
|
||||
$formDefinition = ArrayUtility::setValueByPath($formDefinition, $key, 'default', '.');
|
||||
|
||||
$hmacPropertyPathParts = explode('.', $key);
|
||||
$lastKeySegment = array_pop($hmacPropertyPathParts);
|
||||
$hmacPropertyPathParts[] = '_orig_' . $lastKeySegment;
|
||||
$hmacValuePath = implode('.', $hmacPropertyPathParts);
|
||||
|
||||
if (ArrayUtility::isValidPath($formDefinition, $hmacValuePath, '.')) {
|
||||
$formDefinition = ArrayUtility::removeByPath($formDefinition, $hmacValuePath, '.');
|
||||
}
|
||||
|
||||
$this->converterDto->setFormDefinition($formDefinition);
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class FlexFormFinisherOverridesConverterDto
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $prototypeFinisherDefinition = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $finisherDefinition = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $finisherIdentifier = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $flexFormSheetSettings = [];
|
||||
|
||||
public function __construct(
|
||||
array $prototypeFinisherDefinition,
|
||||
array $finisherDefinition,
|
||||
string $finisherIdentifier,
|
||||
array $flexFormSheetSettings
|
||||
) {
|
||||
$this->prototypeFinisherDefinition = $prototypeFinisherDefinition;
|
||||
$this->finisherDefinition = $finisherDefinition;
|
||||
$this->finisherIdentifier = $finisherIdentifier;
|
||||
$this->flexFormSheetSettings = $flexFormSheetSettings;
|
||||
}
|
||||
|
||||
public function getPrototypeFinisherDefinition(): array
|
||||
{
|
||||
return $this->prototypeFinisherDefinition;
|
||||
}
|
||||
|
||||
public function getFinisherDefinition(): array
|
||||
{
|
||||
return $this->finisherDefinition;
|
||||
}
|
||||
|
||||
public function setFinisherDefinition(array $finisherDefinition): FlexFormFinisherOverridesConverterDto
|
||||
{
|
||||
$this->finisherDefinition = $finisherDefinition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFinisherIdentifier(): string
|
||||
{
|
||||
return $this->finisherIdentifier;
|
||||
}
|
||||
|
||||
public function getFlexFormSheetSettings(): array
|
||||
{
|
||||
return $this->flexFormSheetSettings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class RemoveHmacDataConverter extends AbstractConverter
|
||||
{
|
||||
/**
|
||||
* Remove the hmac data ("_orig_<propertyName>") for the corresponding property.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$formDefinition = $this->converterDto->getFormDefinition();
|
||||
|
||||
$propertyPathParts = explode('.', $key);
|
||||
array_pop($propertyPathParts);
|
||||
$propertyPath = implode('.', $propertyPathParts);
|
||||
$formDefinition = ArrayUtility::removeByPath($formDefinition, $propertyPath, '.');
|
||||
|
||||
$this->converterDto->setFormDefinition($formDefinition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ConfigurationService;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractValidator implements ValidatorInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $currentElement;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $sessionToken;
|
||||
|
||||
/**
|
||||
* @var ValidationDto
|
||||
*/
|
||||
protected $validationDto;
|
||||
|
||||
public function __construct(array $currentElement, string $sessionToken, ValidationDto $validationDto)
|
||||
{
|
||||
$this->currentElement = $currentElement;
|
||||
$this->sessionToken = $sessionToken;
|
||||
$this->validationDto = $validationDto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the path in which the hmac value is expected based on the property path.
|
||||
*/
|
||||
protected function buildHmacDataPath(string $propertyPath): string
|
||||
{
|
||||
$pathParts = explode('.', $propertyPath);
|
||||
$lastPathSegment = array_pop($pathParts);
|
||||
$pathParts[] = '_orig_' . $lastPathSegment;
|
||||
|
||||
return implode('.', $pathParts);
|
||||
}
|
||||
|
||||
protected function getFormDefinitionValidationService(): FormDefinitionValidationService
|
||||
{
|
||||
return GeneralUtility::makeInstance(FormDefinitionValidationService::class);
|
||||
}
|
||||
|
||||
protected function getConfigurationService(): ConfigurationService
|
||||
{
|
||||
return GeneralUtility::makeInstance(ConfigurationService::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class CollectionBasedValidator extends AbstractValidator
|
||||
{
|
||||
/**
|
||||
* Throws an exception if value from a property collection property
|
||||
* does not match its hmac hash or if there is no hmac hash
|
||||
* available for the value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
public function validatePropertyCollectionElementPropertyValueByHmacData(
|
||||
array $currentElement,
|
||||
$value,
|
||||
string $sessionToken,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
$hmacDataPath = $this->buildHmacDataPath($dto->getPropertyPath());
|
||||
if (ArrayUtility::isValidPath($currentElement, $hmacDataPath, '.')) {
|
||||
$hmacData = ArrayUtility::getValueByPath($currentElement, $hmacDataPath, '.');
|
||||
|
||||
$hmacContent = [
|
||||
$dto->getFormElementIdentifier(),
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier(),
|
||||
$dto->getPropertyPath(),
|
||||
];
|
||||
|
||||
if (!$this->getFormDefinitionValidationService()->isPropertyValueEqualToHistoricalValue($hmacContent, $value, $hmacData, $sessionToken)) {
|
||||
$message = 'The value "%s" of property "%s" (form element "%s" / "%s.%s") is not equal to the historical value "%s" #1528591586';
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
$message,
|
||||
$value,
|
||||
$dto->getPropertyPath(),
|
||||
$dto->getFormElementIdentifier(),
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier(),
|
||||
$hmacData['value'] ?? ''
|
||||
),
|
||||
1528591586
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$message = 'No hmac found for property "%s" (form element "%s" / "%s.%s") #1528591585';
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
$message,
|
||||
$dto->getPropertyPath(),
|
||||
$dto->getFormElementIdentifier(),
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier()
|
||||
),
|
||||
1528591585
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CreatableFormElementPropertiesValidator extends ElementBasedValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the form element property is defined within the form editor setup
|
||||
* or if the property is defined within the "predefinedDefaults" in the form editor setup
|
||||
* and the property value matches the predefined value
|
||||
* or if there is a valid hmac hash for the value.
|
||||
* If the form element property is defined within the form editor setup
|
||||
* and there is no valid hmac hash for the value
|
||||
* and is the form element property configured to only allow a limited set of values,
|
||||
* check the current (submitted) value against the allowed set of values (defined within the form setup).
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value)
|
||||
{
|
||||
$dto = $this->validationDto->withPropertyPath($key);
|
||||
|
||||
if ($this->getConfigurationService()->isFormElementPropertyDefinedInFormEditorSetup($dto)) {
|
||||
if ($this->getConfigurationService()->formElementPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup($dto)) {
|
||||
$this->validateFormElementValue($value, $dto);
|
||||
}
|
||||
} elseif (
|
||||
$this->getConfigurationService()->isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup($dto)
|
||||
&& !ArrayUtility::isValidPath($this->currentElement, $this->buildHmacDataPath($dto->getPropertyPath()), '.')
|
||||
) {
|
||||
$this->validateFormElementPredefinedDefaultValue($value, $dto);
|
||||
} else {
|
||||
$this->validateFormElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if the value from a form element property
|
||||
* does not match the default value from the form editor setup.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
protected function validateFormElementPredefinedDefaultValue(
|
||||
$value,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
// If the form element is newly created, we have to compare the $value (form definition) with $predefinedDefaultValue (form setup)
|
||||
// to check the integrity (at this time we don't have a hmac for the $value to check the integrity)
|
||||
$predefinedDefaultValue = $this->getConfigurationService()->getFormElementPredefinedDefaultValueFromFormEditorSetup($dto);
|
||||
if ($value !== $predefinedDefaultValue) {
|
||||
$throwException = true;
|
||||
|
||||
if (is_string($predefinedDefaultValue)) {
|
||||
// Last chance:
|
||||
// Get all translations (from all backend languages) for the untranslated! $predefinedDefaultValue and
|
||||
// compare the (already translated) $value (from the form definition) against the possible
|
||||
// translations from $predefinedDefaultValue.
|
||||
// Usecase:
|
||||
// * backend language is EN
|
||||
// * open the form editor and add a ContentElement form element
|
||||
// * switch to another browser tab and change the backend language to DE
|
||||
// * clear the cache
|
||||
// * go back to the form editor and click the save button
|
||||
// Out of scope:
|
||||
// * the same scenario as above + delete the previous chosen backend language within the maintenance tool
|
||||
$untranslatedPredefinedDefaultValue = $this->getConfigurationService()->getFormElementPredefinedDefaultValueFromFormEditorSetup($dto, false);
|
||||
$translations = $this->getConfigurationService()->getAllBackendTranslationsForTranslationKey(
|
||||
$untranslatedPredefinedDefaultValue,
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
|
||||
if (in_array($value, $translations, true)) {
|
||||
$throwException = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($throwException) {
|
||||
$message = 'The value "%s" of property "%s" (form element "%s") is not equal to the default value "%s" #1528588035';
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
$message,
|
||||
$value,
|
||||
$dto->getPropertyPath(),
|
||||
$dto->getFormElementIdentifier(),
|
||||
$predefinedDefaultValue
|
||||
),
|
||||
1528588035
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if the value from a form element property
|
||||
* does not match the allowed set of values (defined within the form setup).
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
protected function validateFormElementValue(
|
||||
$value,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
$allowedValues = $this->getConfigurationService()->getAllowedValuesForFormElementPropertyFromFormEditorSetup($dto);
|
||||
|
||||
if (!in_array($value, $allowedValues, true)) {
|
||||
$untranslatedAllowedValues = $this->getConfigurationService()->getAllowedValuesForFormElementPropertyFromFormEditorSetup($dto, false);
|
||||
// Compare the $value against the untranslated set of allowed values
|
||||
if (in_array($value, $untranslatedAllowedValues, true)) {
|
||||
// All good, $value is within the untranslated set of allowed values
|
||||
return;
|
||||
}
|
||||
// Get all translations (from all backend languages) for the untranslated! $allowedValues and
|
||||
// compare the (already translated) $value (from the form definition) against all possible
|
||||
// translations for $untranslatedAllowedValues.
|
||||
$allPossibleAllowedValuesTranslations = $this->getConfigurationService()->getAllBackendTranslationsForTranslationKeys(
|
||||
$untranslatedAllowedValues,
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
|
||||
foreach ($allPossibleAllowedValuesTranslations as $translations) {
|
||||
if (in_array($value, $translations, true)) {
|
||||
// All good, $value is within the set of translated allowed values
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Last chance:
|
||||
// If $value is not configured within the form setup as an allowed value
|
||||
// but was written within the form definition by hand (and therefore contains a hmac),
|
||||
// check if $value is manipulated.
|
||||
// If $value has no hmac or if the hmac exists but is not valid,
|
||||
// then $this->validatePropertyCollectionElementPropertyValueByHmacData() will
|
||||
// throw an exception.
|
||||
$this->validateFormElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CreatablePropertyCollectionElementPropertiesValidator extends CollectionBasedValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the property collection element property is defined
|
||||
* within the form editor setup or if the property is defined within
|
||||
* the "predefinedDefaults" in the form editor setup
|
||||
* and the property value matches the predefined value
|
||||
* or if there is a valid hmac hash for the value.
|
||||
* If the property collection element property is defined within the form editor setup
|
||||
* and there is no valid hmac hash for the value
|
||||
* and is the form property collection element property configured to only allow a limited set of values,
|
||||
* check the current (submitted) value against the allowed set of values (defined within the form setup).
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value)
|
||||
{
|
||||
$dto = $this->validationDto->withPropertyPath($key);
|
||||
|
||||
if ($this->getConfigurationService()->isPropertyCollectionPropertyDefinedInFormEditorSetup($dto)) {
|
||||
if ($this->getConfigurationService()->propertyCollectionPropertyHasLimitedAllowedValuesDefinedWithinFormEditorSetup($dto)) {
|
||||
$this->validatePropertyCollectionPropertyValue($value, $dto);
|
||||
}
|
||||
} elseif (
|
||||
$this->getConfigurationService()->isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup($dto)
|
||||
&& !ArrayUtility::isValidPath($this->currentElement, $this->buildHmacDataPath($dto->getPropertyPath()), '.')
|
||||
) {
|
||||
$this->validatePropertyCollectionElementPredefinedDefaultValue($value, $dto);
|
||||
} else {
|
||||
$this->validatePropertyCollectionElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if the value from a property collection property
|
||||
* does not match the default value from the form editor setup.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
protected function validatePropertyCollectionElementPredefinedDefaultValue(
|
||||
$value,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
// If the property collection element is newly created, we have to compare the $value (form definition) with $predefinedDefaultValue (form setup)
|
||||
// to check the integrity (at this time we don't have a hmac on the value to check the integrity)
|
||||
$predefinedDefaultValue = $this->getConfigurationService()->getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup($dto);
|
||||
if ($value !== $predefinedDefaultValue) {
|
||||
$throwException = true;
|
||||
|
||||
if (is_string($predefinedDefaultValue)) {
|
||||
// Last chance:
|
||||
// Get all translations (from all backend languages) for the untranslated! $predefinedDefaultValue and
|
||||
// compare the (already translated) $value (from the form definition) against the possible
|
||||
// translations from $predefinedDefaultValue.
|
||||
$untranslatedPredefinedDefaultValue = $this->getConfigurationService()->getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup($dto, false);
|
||||
$translations = $this->getConfigurationService()->getAllBackendTranslationsForTranslationKey(
|
||||
$untranslatedPredefinedDefaultValue,
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
|
||||
if (in_array($value, $translations, true)) {
|
||||
$throwException = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($throwException) {
|
||||
$message = 'The value "%s" of property "%s" (form element "%s" / "%s.%s") is not equal to the default value "%s" #1528591502';
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
$message,
|
||||
$value,
|
||||
$dto->getPropertyPath(),
|
||||
$dto->getFormElementIdentifier(),
|
||||
$dto->getPropertyCollectionName(),
|
||||
$dto->getPropertyCollectionElementIdentifier(),
|
||||
$predefinedDefaultValue
|
||||
),
|
||||
1528591502
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if the value from a property collection property
|
||||
* does not match the allowed set of values (defined within the form setup).
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
protected function validatePropertyCollectionPropertyValue(
|
||||
$value,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
$allowedValues = $this->getConfigurationService()->getAllowedValuesForPropertyCollectionPropertyFromFormEditorSetup($dto);
|
||||
|
||||
if (!in_array($value, $allowedValues, true)) {
|
||||
$untranslatedAllowedValues = $this->getConfigurationService()->getAllowedValuesForPropertyCollectionPropertyFromFormEditorSetup($dto, false);
|
||||
// Compare the $value against the untranslated set of allowed values
|
||||
if (in_array($value, $untranslatedAllowedValues, true)) {
|
||||
// All good, $value is within the untranslated set of allowed values
|
||||
return;
|
||||
}
|
||||
// Get all translations (from all backend languages) for the untranslated! $allowedValues and
|
||||
// compare the (already translated) $value (from the form definition) against all possible
|
||||
// translations for $untranslatedAllowedValues.
|
||||
$allPossibleAllowedValuesTranslations = $this->getConfigurationService()->getAllBackendTranslationsForTranslationKeys(
|
||||
$untranslatedAllowedValues,
|
||||
$dto->getPrototypeName()
|
||||
);
|
||||
|
||||
foreach ($allPossibleAllowedValuesTranslations as $translations) {
|
||||
if (in_array($value, $translations, true)) {
|
||||
// All good, $value is within the set of translated allowed values
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Last chance:
|
||||
// If $value is not configured within the form setup as an allowed value
|
||||
// but was written within the form definition by hand (and therefore contains a hmac),
|
||||
// check if $value is manipulated.
|
||||
// If $value has no hmac or if the hmac exists but is not valid,
|
||||
// then $this->validatePropertyCollectionElementPropertyValueByHmacData() will
|
||||
// throw an exception.
|
||||
$this->validatePropertyCollectionElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class ElementBasedValidator extends AbstractValidator
|
||||
{
|
||||
/**
|
||||
* Throws an exception if value from a form element property
|
||||
* does not match its hmac hash or if there is no hmac hash
|
||||
* available for the value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @throws PropertyException
|
||||
*/
|
||||
public function validateFormElementPropertyValueByHmacData(
|
||||
array $currentElement,
|
||||
$value,
|
||||
string $sessionToken,
|
||||
ValidationDto $dto
|
||||
): void {
|
||||
$hmacDataPath = $this->buildHmacDataPath($dto->getPropertyPath());
|
||||
if (ArrayUtility::isValidPath($currentElement, $hmacDataPath, '.')) {
|
||||
$hmacData = ArrayUtility::getValueByPath($currentElement, $hmacDataPath, '.');
|
||||
|
||||
$hmacContent = [$dto->getFormElementIdentifier(), $dto->getPropertyPath()];
|
||||
if (!$this->getFormDefinitionValidationService()->isPropertyValueEqualToHistoricalValue($hmacContent, $value, $hmacData, $sessionToken)) {
|
||||
$message = 'The value "%s" of property "%s" (form element "%s") is not equal to the historical value "%s" #1528588036';
|
||||
throw new PropertyException(
|
||||
sprintf(
|
||||
$message,
|
||||
$value,
|
||||
$dto->getPropertyPath(),
|
||||
$dto->getFormElementIdentifier(),
|
||||
$hmacData['value'] ?? ''
|
||||
),
|
||||
1528588036
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$message = 'No hmac found for property "%s" (form element "%s") #1528588037';
|
||||
throw new PropertyException(
|
||||
sprintf($message, $dto->getPropertyPath(), $dto->getFormElementIdentifier()),
|
||||
1528588037
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class FormElementHmacDataValidator extends ElementBasedValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the form element property value matches to its hmac hash.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$dto = $this->validationDto->withPropertyPath($key);
|
||||
$this->validateFormElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PropertyCollectionElementHmacDataValidator extends CollectionBasedValidator
|
||||
{
|
||||
/**
|
||||
* Checks if the property collection element values matches to its hmac hash.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value): void
|
||||
{
|
||||
$dto = $this->validationDto->withPropertyPath($key)->withPropertyCollectionElementIdentifier(
|
||||
$this->currentElement['identifier']
|
||||
);
|
||||
$this->validatePropertyCollectionElementPropertyValueByHmacData(
|
||||
$this->currentElement,
|
||||
$value,
|
||||
$this->sessionToken,
|
||||
$dto
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
class ValidationDto
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $prototypeName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $formElementType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $formElementIdentifier;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $propertyPath;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $propertyCollectionName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $propertyCollectionElementIdentifier;
|
||||
|
||||
public function __construct(
|
||||
?string $prototypeName = null,
|
||||
?string $formElementType = null,
|
||||
?string $formElementIdentifier = null,
|
||||
?string $propertyPath = null,
|
||||
?string $propertyCollectionName = null,
|
||||
?string $propertyCollectionElementIdentifier = null
|
||||
) {
|
||||
$this->prototypeName = $prototypeName;
|
||||
$this->formElementType = $formElementType;
|
||||
$this->formElementIdentifier = $formElementIdentifier;
|
||||
$this->propertyPath = $propertyPath;
|
||||
$this->propertyCollectionName = $propertyCollectionName;
|
||||
$this->propertyCollectionElementIdentifier = $propertyCollectionElementIdentifier;
|
||||
}
|
||||
|
||||
public function getPrototypeName(): string
|
||||
{
|
||||
return $this->prototypeName;
|
||||
}
|
||||
|
||||
public function getFormElementType(): string
|
||||
{
|
||||
return $this->formElementType;
|
||||
}
|
||||
|
||||
public function getFormElementIdentifier(): string
|
||||
{
|
||||
return $this->formElementIdentifier;
|
||||
}
|
||||
|
||||
public function getPropertyPath(): string
|
||||
{
|
||||
return $this->propertyPath;
|
||||
}
|
||||
|
||||
public function getPropertyCollectionName(): string
|
||||
{
|
||||
return $this->propertyCollectionName;
|
||||
}
|
||||
|
||||
public function getPropertyCollectionElementIdentifier(): string
|
||||
{
|
||||
return $this->propertyCollectionElementIdentifier;
|
||||
}
|
||||
|
||||
public function hasPrototypeName(): bool
|
||||
{
|
||||
return !empty($this->prototypeName);
|
||||
}
|
||||
|
||||
public function hasFormElementType(): bool
|
||||
{
|
||||
return !empty($this->formElementType);
|
||||
}
|
||||
|
||||
public function hasFormElementIdentifier(): bool
|
||||
{
|
||||
return !empty($this->formElementIdentifier);
|
||||
}
|
||||
|
||||
public function hasPropertyPath(): bool
|
||||
{
|
||||
return !empty($this->propertyPath);
|
||||
}
|
||||
|
||||
public function hasPropertyCollectionName(): bool
|
||||
{
|
||||
return !empty($this->propertyCollectionName);
|
||||
}
|
||||
|
||||
public function hasPropertyCollectionElementIdentifier(): bool
|
||||
{
|
||||
return !empty($this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withPrototypeName(string $prototypeName): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $prototypeName, $this->formElementType, $this->formElementIdentifier, $this->propertyPath, $this->propertyCollectionName, $this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withFormElementType(string $formElementType): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $this->prototypeName, $formElementType, $this->formElementIdentifier, $this->propertyPath, $this->propertyCollectionName, $this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withFormElementIdentifier(string $formElementIdentifier): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $this->prototypeName, $this->formElementType, $formElementIdentifier, $this->propertyPath, $this->propertyCollectionName, $this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withPropertyPath(string $propertyPath): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $this->prototypeName, $this->formElementType, $this->formElementIdentifier, $propertyPath, $this->propertyCollectionName, $this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withPropertyCollectionName(string $propertyCollectionName): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $this->prototypeName, $this->formElementType, $this->formElementIdentifier, $this->propertyPath, $propertyCollectionName, $this->propertyCollectionElementIdentifier);
|
||||
}
|
||||
|
||||
public function withPropertyCollectionElementIdentifier(string $propertyCollectionElementIdentifier): ValidationDto
|
||||
{
|
||||
return GeneralUtility::makeInstance(self::class, $this->prototypeName, $this->formElementType, $this->formElementIdentifier, $this->propertyPath, $this->propertyCollectionName, $propertyCollectionElementIdentifier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface ValidatorInterface
|
||||
{
|
||||
public function __construct(array $currentElement, string $sessionToken, ValidationDto $validationDto);
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value);
|
||||
}
|
||||
@@ -0,0 +1,524 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Crypto\Random;
|
||||
use TYPO3\CMS\Core\Html\SanitizerBuilderFactory;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\AddHmacDataConverter;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\ConverterDto;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherTranslationLanguageConverter;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\RemoveHmacDataConverter;
|
||||
use TYPO3\CMS\Form\Service\RichTextConfigurationService;
|
||||
use TYPO3\HtmlSanitizer\Sanitizer;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
readonly class FormDefinitionConversionService
|
||||
{
|
||||
public function __construct(
|
||||
private RichTextConfigurationService $richTextConfigurationService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Add a new value "_orig_<propertyName>" for each scalar property value
|
||||
* within the form definition as a sibling of the property key.
|
||||
* "_orig_<propertyName>" is an array which contains the property value
|
||||
* and a hmac hash for the property value.
|
||||
* "_orig_<propertyName>" will be used to validate the form definition on saving.
|
||||
* @see \TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService::validateFormDefinitionProperties()
|
||||
*/
|
||||
public function addHmacData(array $formDefinition, string $formPersistenceIdentifier): array
|
||||
{
|
||||
// Extend the hmac hashing key with a "per form editor session" unique key.
|
||||
$sessionToken = $this->generateSessionToken();
|
||||
$this->persistSessionToken($sessionToken, $formPersistenceIdentifier);
|
||||
|
||||
$converterDto = GeneralUtility::makeInstance(ConverterDto::class, $formDefinition);
|
||||
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $formDefinition)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'addHmacData',
|
||||
'(^identifier$|renderables\.([\d]+)\.identifier$)',
|
||||
GeneralUtility::makeInstance(
|
||||
AddHmacDataConverter::class,
|
||||
$converterDto,
|
||||
$sessionToken
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$result = $converterDto->getFormDefinition();
|
||||
|
||||
// Embed the form persistence identifier so the TypeConverter can
|
||||
// look up the correct per-form session token when saving.
|
||||
$result['_formPersistenceIdentifier'] = $formPersistenceIdentifier;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the "_orig_<propertyName>" values and the
|
||||
* "_formPersistenceIdentifier" marker from the form definition.
|
||||
*/
|
||||
public function removeHmacData(array $formDefinition): array
|
||||
{
|
||||
unset($formDefinition['_formPersistenceIdentifier']);
|
||||
|
||||
$converterDto = GeneralUtility::makeInstance(ConverterDto::class, $formDefinition);
|
||||
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $formDefinition)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'removeHmacData',
|
||||
'(_orig_.*|.*\._orig_.*)\.hmac',
|
||||
GeneralUtility::makeInstance(
|
||||
RemoveHmacDataConverter::class,
|
||||
$converterDto
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return $converterDto->getFormDefinition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate various finisher options
|
||||
*/
|
||||
public function migrateFinisherConfiguration(array $formDefinition): array
|
||||
{
|
||||
$converterDto = GeneralUtility::makeInstance(ConverterDto::class, $formDefinition);
|
||||
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $formDefinition)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'migrateFinisherLanguageSettings',
|
||||
'^finishers\.([\d]+)\.options.translation.language$',
|
||||
GeneralUtility::makeInstance(
|
||||
FinisherTranslationLanguageConverter::class,
|
||||
$converterDto
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return $converterDto->getFormDefinition();
|
||||
}
|
||||
|
||||
protected function persistSessionToken(string $sessionToken, string $formPersistenceIdentifier): void
|
||||
{
|
||||
$tokens = $this->getBackendUser()->getSessionData('extFormProtectionSessionTokens') ?? [];
|
||||
if (!is_array($tokens)) {
|
||||
$tokens = [];
|
||||
}
|
||||
$tokens[$formPersistenceIdentifier] = $sessionToken;
|
||||
$this->getBackendUser()->setAndSaveSessionData('extFormProtectionSessionTokens', $tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the session token for a specific form persistence identifier.
|
||||
*/
|
||||
public function retrieveSessionToken(string $formPersistenceIdentifier): string
|
||||
{
|
||||
$tokens = $this->getBackendUser()->getSessionData('extFormProtectionSessionTokens');
|
||||
if (is_array($tokens) && isset($tokens[$formPersistenceIdentifier]) && is_string($tokens[$formPersistenceIdentifier])) {
|
||||
return $tokens[$formPersistenceIdentifier];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public function sanitizeHtml(array $rawFormDefinitionArray, array $rtePropertyPaths = [], string $defaultBuild = 'default'): array
|
||||
{
|
||||
return $this->sanitizeValuesRecursively($rawFormDefinitionArray, $rtePropertyPaths, $defaultBuild);
|
||||
}
|
||||
|
||||
public function transformRteContentForPersistence(array $formDefinition, array $rtePropertyPaths): array
|
||||
{
|
||||
if ($rtePropertyPaths === []) {
|
||||
return $formDefinition;
|
||||
}
|
||||
|
||||
return $this->transformRteContentRecursively($formDefinition, $rtePropertyPaths, $this->richTextConfigurationService, 'persistence');
|
||||
}
|
||||
|
||||
public function transformRteContentForRichTextEditor(array $formDefinition, array $rtePropertyPaths): array
|
||||
{
|
||||
if ($rtePropertyPaths === []) {
|
||||
return $formDefinition;
|
||||
}
|
||||
|
||||
return $this->transformRteContentRecursively($formDefinition, $rtePropertyPaths, $this->richTextConfigurationService, 'rte');
|
||||
}
|
||||
|
||||
protected function transformRteContentRecursively(
|
||||
array $formDefinition,
|
||||
array $rtePropertyPaths,
|
||||
RichTextConfigurationService $richTextConfigurationService,
|
||||
string $direction = 'persistence'
|
||||
): array {
|
||||
// Get the element type (e.g., 'Checkbox', 'StaticText', 'Form')
|
||||
$elementType = $formDefinition['type'] ?? null;
|
||||
|
||||
// Transform properties for this specific element type
|
||||
if ($elementType !== null && isset($rtePropertyPaths[$elementType])) {
|
||||
foreach ($rtePropertyPaths[$elementType] as $propertyPath => $presetName) {
|
||||
$value = $this->getValueByPath($formDefinition, $propertyPath);
|
||||
if (is_string($value) && $value !== '') {
|
||||
$transformedValue = $direction === 'persistence'
|
||||
? $richTextConfigurationService->transformTextForPersistence($value, $presetName)
|
||||
: $richTextConfigurationService->transformTextForRichTextEditor($value, $presetName);
|
||||
$formDefinition = $this->setValueByPath($formDefinition, $propertyPath, $transformedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into renderables (form elements on pages)
|
||||
if (is_array($formDefinition['renderables'] ?? null)) {
|
||||
foreach ($formDefinition['renderables'] as $key => $renderable) {
|
||||
if (is_array($renderable)) {
|
||||
$formDefinition['renderables'][$key] = $this->transformRteContentRecursively(
|
||||
$renderable,
|
||||
$rtePropertyPaths,
|
||||
$richTextConfigurationService,
|
||||
$direction
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transform finisher options
|
||||
if (is_array($formDefinition['finishers'] ?? null)) {
|
||||
$finisherRtePaths = $rtePropertyPaths['_finishers'] ?? [];
|
||||
foreach ($formDefinition['finishers'] as $key => $finisher) {
|
||||
if (!is_array($finisher)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$finisherIdentifier = $finisher['identifier'] ?? null;
|
||||
if ($finisherIdentifier === null || !isset($finisherRtePaths[$finisherIdentifier])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($finisherRtePaths[$finisherIdentifier] as $propertyPath => $presetName) {
|
||||
// Property path in finisher config is like 'options.message'
|
||||
$value = $this->getValueByPath($finisher, $propertyPath);
|
||||
if (is_string($value) && $value !== '') {
|
||||
$transformedValue = $direction === 'persistence'
|
||||
? $richTextConfigurationService->transformTextForPersistence($value, $presetName)
|
||||
: $richTextConfigurationService->transformTextForRichTextEditor($value, $presetName);
|
||||
$finisher = $this->setValueByPath($finisher, $propertyPath, $transformedValue);
|
||||
$formDefinition['finishers'][$key] = $finisher;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $formDefinition;
|
||||
}
|
||||
|
||||
protected function getValueByPath(array $array, string $path): mixed
|
||||
{
|
||||
$keys = explode('.', $path);
|
||||
$current = $array;
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!is_array($current) || !array_key_exists($key, $current)) {
|
||||
return null;
|
||||
}
|
||||
$current = $current[$key];
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
protected function setValueByPath(array $array, string $path, mixed $value): array
|
||||
{
|
||||
$keys = explode('.', $path);
|
||||
$current = &$array;
|
||||
|
||||
foreach ($keys as $i => $key) {
|
||||
if ($i === count($keys) - 1) {
|
||||
$current[$key] = $value;
|
||||
} else {
|
||||
if (!isset($current[$key]) || !is_array($current[$key])) {
|
||||
$current[$key] = [];
|
||||
}
|
||||
$current = &$current[$key];
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract RTE-enabled property paths from prototype configuration.
|
||||
*
|
||||
* Scans the form editor configuration for all form element types and finishers
|
||||
* to find editors with enableRichtext=true and returns their property paths
|
||||
* along with the RTE preset name, organized by element type.
|
||||
*
|
||||
* @param array $prototypeConfiguration The prototype configuration array
|
||||
* @return array Map of element types to their RTE property paths
|
||||
* Format: [
|
||||
* 'Checkbox' => ['label' => 'form-label'],
|
||||
* 'StaticText' => ['properties.text' => 'form-content'],
|
||||
* '_finishers' => ['Confirmation' => ['options.message' => 'form-label']]
|
||||
* ]
|
||||
*/
|
||||
public function extractRtePropertyPaths(array $prototypeConfiguration): array
|
||||
{
|
||||
$rtePropertyPaths = [];
|
||||
|
||||
// Extract from form elements definition
|
||||
$formElementsDefinition = $prototypeConfiguration['formElementsDefinition'] ?? [];
|
||||
foreach ($formElementsDefinition as $formElementType => $elementConfig) {
|
||||
$editors = $elementConfig['formEditor']['editors'] ?? [];
|
||||
foreach ($editors as $editor) {
|
||||
if ($this->isRteEditor($editor)) {
|
||||
$propertyPath = $editor['propertyPath'] ?? '';
|
||||
$presetName = $editor['richtextConfiguration'] ?? 'form-label';
|
||||
if ($propertyPath !== '') {
|
||||
$rtePropertyPaths[$formElementType][$propertyPath] = $presetName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract from finisher property collections on the Form element
|
||||
// Finisher editors are defined in:
|
||||
// formElementsDefinition.Form.formEditor.propertyCollections.finishers.<index>.editors
|
||||
$finisherCollections = $formElementsDefinition['Form']['formEditor']['propertyCollections']['finishers'] ?? [];
|
||||
foreach ($finisherCollections as $finisherCollection) {
|
||||
$finisherIdentifier = $finisherCollection['identifier'] ?? '';
|
||||
if ($finisherIdentifier === '') {
|
||||
continue;
|
||||
}
|
||||
$editors = $finisherCollection['editors'] ?? [];
|
||||
foreach ($editors as $editor) {
|
||||
if ($this->isRteEditor($editor)) {
|
||||
$propertyPath = $editor['propertyPath'] ?? '';
|
||||
$presetName = $editor['richtextConfiguration'] ?? 'form-label';
|
||||
if ($propertyPath !== '') {
|
||||
$rtePropertyPaths['_finishers'][$finisherIdentifier][$propertyPath] = $presetName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rtePropertyPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an editor configuration represents an RTE-enabled textarea.
|
||||
*/
|
||||
protected function isRteEditor(array $editor): bool
|
||||
{
|
||||
return ($editor['templateName'] ?? '') === 'Inspector-TextareaEditor'
|
||||
&& ($editor['enableRichtext'] ?? false) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively sanitizes values in form definition.
|
||||
*
|
||||
* For RTE-enabled fields: Uses HtmlSanitizer with the preset configured in the RTE configuration
|
||||
* For all other string fields: Uses strip_tags to remove ALL HTML
|
||||
*
|
||||
* @param array $array The array to sanitize
|
||||
* @param array $rtePropertyPaths Map of element types to their RTE property paths with preset names
|
||||
* @param string $defaultBuild Default sanitizer build name for RTE fields without specific preset
|
||||
* @param string|null $currentElementType The current element type being processed
|
||||
* @param string $currentPath The current property path being processed
|
||||
*/
|
||||
protected function sanitizeValuesRecursively(
|
||||
array $array,
|
||||
array $rtePropertyPaths = [],
|
||||
string $defaultBuild = 'default',
|
||||
?string $currentElementType = null,
|
||||
string $currentPath = ''
|
||||
): array {
|
||||
$result = $array;
|
||||
|
||||
// Detect element type from current array (only at element root level)
|
||||
$elementType = $result['type'] ?? $currentElementType;
|
||||
|
||||
// Get RTE property paths for this element type (with their preset names)
|
||||
$elementRtePaths = [];
|
||||
if ($elementType !== null && isset($rtePropertyPaths[$elementType])) {
|
||||
$elementRtePaths = $rtePropertyPaths[$elementType];
|
||||
}
|
||||
|
||||
foreach ($result as $key => $value) {
|
||||
// Build the full property path
|
||||
$propertyPath = $currentPath === '' ? $key : $currentPath . '.' . $key;
|
||||
|
||||
if ($key === 'renderables' && is_array($value)) {
|
||||
// For renderables, process each child element with fresh context
|
||||
foreach ($value as $childKey => $childValue) {
|
||||
if (is_array($childValue)) {
|
||||
$result[$key][$childKey] = $this->sanitizeValuesRecursively(
|
||||
$childValue,
|
||||
$rtePropertyPaths,
|
||||
$defaultBuild
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif ($key === 'finishers' && is_array($value)) {
|
||||
// Handle finishers separately
|
||||
$finisherRtePaths = $rtePropertyPaths['_finishers'] ?? [];
|
||||
foreach ($value as $finisherKey => $finisher) {
|
||||
if (is_array($finisher)) {
|
||||
$finisherIdentifier = $finisher['identifier'] ?? null;
|
||||
$finisherRteFields = [];
|
||||
if ($finisherIdentifier !== null && isset($finisherRtePaths[$finisherIdentifier])) {
|
||||
$finisherRteFields = $finisherRtePaths[$finisherIdentifier];
|
||||
}
|
||||
$result[$key][$finisherKey] = $this->sanitizeFinisherRecursively(
|
||||
$finisher,
|
||||
$finisherRteFields,
|
||||
$defaultBuild
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif (is_array($value)) {
|
||||
// Recurse into nested arrays, keeping the element type and building path
|
||||
$result[$key] = $this->sanitizeValuesRecursively(
|
||||
$value,
|
||||
$rtePropertyPaths,
|
||||
$defaultBuild,
|
||||
$elementType,
|
||||
$propertyPath
|
||||
);
|
||||
} elseif (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
|
||||
$stringValue = (string)$value;
|
||||
|
||||
// Check if this property path is an RTE field for the current element type
|
||||
if (isset($elementRtePaths[$propertyPath])) {
|
||||
// RTE field: use HtmlSanitizer with the configured preset
|
||||
// This ensures sanitization even for form definitions from external sources
|
||||
$presetBuild = $this->resolveSanitizerBuildFromPreset($elementRtePaths[$propertyPath]);
|
||||
$result[$key] = $this->sanitizeWithBuild($stringValue, $presetBuild ?? $defaultBuild);
|
||||
} else {
|
||||
// Non-RTE field: strip ALL HTML tags for security
|
||||
$result[$key] = strip_tags($stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively sanitize finisher values.
|
||||
*
|
||||
* @param array $finisher The finisher configuration
|
||||
* @param array $rteFields Map of RTE field paths to their preset names
|
||||
* @param string $defaultBuild Default sanitizer build name
|
||||
* @param string $currentPath Current property path
|
||||
*/
|
||||
protected function sanitizeFinisherRecursively(
|
||||
array $finisher,
|
||||
array $rteFields,
|
||||
string $defaultBuild = 'default',
|
||||
string $currentPath = ''
|
||||
): array {
|
||||
foreach ($finisher as $key => $value) {
|
||||
$fullPath = $currentPath === '' ? $key : $currentPath . '.' . $key;
|
||||
|
||||
if (is_array($value)) {
|
||||
$finisher[$key] = $this->sanitizeFinisherRecursively($value, $rteFields, $defaultBuild, $fullPath);
|
||||
} elseif (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
|
||||
$stringValue = (string)$value;
|
||||
|
||||
if (isset($rteFields[$fullPath])) {
|
||||
// RTE field: use HtmlSanitizer with the configured preset
|
||||
$presetBuild = $this->resolveSanitizerBuildFromPreset($rteFields[$fullPath]);
|
||||
$finisher[$key] = $this->sanitizeWithBuild($stringValue, $presetBuild ?? $defaultBuild);
|
||||
} else {
|
||||
// Non-RTE field: strip ALL HTML tags for security
|
||||
$finisher[$key] = strip_tags($stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $finisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the sanitizer build name from an RTE preset configuration.
|
||||
*
|
||||
* @param string $presetName The RTE preset name (e.g., 'form-label', 'form-content')
|
||||
* @return string|null The sanitizer build name, or null if not configured
|
||||
*/
|
||||
protected function resolveSanitizerBuildFromPreset(string $presetName): ?string
|
||||
{
|
||||
$processingConfig = $this->richTextConfigurationService->resolveProcessingConfiguration($presetName);
|
||||
return $processingConfig['HTMLparser_db.']['htmlSanitize.']['build'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize HTML content with the specified sanitizer build.
|
||||
*
|
||||
* @param string $content The HTML content to sanitize
|
||||
* @param string $build The sanitizer build name or class name
|
||||
* @return string The sanitized content
|
||||
*/
|
||||
protected function sanitizeWithBuild(string $content, string $build): string
|
||||
{
|
||||
return $this->createSanitizer($build)->sanitize($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a sanitizer instance for the given build configuration.
|
||||
*
|
||||
* Supports both preset names (e.g., 'default') and class names implementing BuilderInterface.
|
||||
*
|
||||
* @param string $build The sanitizer build name or class name
|
||||
* @return Sanitizer The sanitizer instance
|
||||
*/
|
||||
protected function createSanitizer(string $build): Sanitizer
|
||||
{
|
||||
if (class_exists($build) && is_a($build, \TYPO3\HtmlSanitizer\Builder\BuilderInterface::class, true)) {
|
||||
$builder = GeneralUtility::makeInstance($build);
|
||||
} else {
|
||||
$factory = GeneralUtility::makeInstance(SanitizerBuilderFactory::class);
|
||||
$builder = $factory->build($build);
|
||||
}
|
||||
return $builder->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the random token which is used in the hash for the form tokens.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateSessionToken(): string
|
||||
{
|
||||
return GeneralUtility::makeInstance(Random::class)->generateRandomHexString(64);
|
||||
}
|
||||
|
||||
protected function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessing;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ArrayProcessing\ArrayProcessor;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatablePropertyCollectionElementPropertiesValidator;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\FormElementHmacDataValidator;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\PropertyCollectionElementHmacDataValidator;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
readonly class FormDefinitionValidationService
|
||||
{
|
||||
public function __construct(
|
||||
protected HashService $hashService,
|
||||
protected ConfigurationService $configurationService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Validate the form definition properties using the form setup.
|
||||
* Pseudo workflow:
|
||||
* Is the form element type creatable by the form editor?
|
||||
* YES
|
||||
* foreach(form element properties) (without finishers|validators)
|
||||
* is the form element property defined in the form setup (can be manipulated)?
|
||||
* YES
|
||||
* is the form element property configured to only allow a limited set of values (e.g. Inspector-MultiSelectEditor, Inspector-SingleSelectEditor ...)
|
||||
* YES
|
||||
* is the form element property value within the set of allowed values?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* valid!
|
||||
* NO
|
||||
* is the form element property defined in "predefinedDefaults" in the form setup (cannot be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form element property value equals to the value defined in "predefinedDefaults" in the form setup?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* is there a hmac hash available for the form element property value (cannot be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form element property value equals the historical value (and is the historical value valid)?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* foreach(form elements finishers|validators)
|
||||
* is the form elements finisher|validator creatable by the form editor?
|
||||
* YES
|
||||
* foreach(form elements finisher|validator properties)
|
||||
* is the form elements finisher|validator property defined in the form setup (can be manipulated)?
|
||||
* YES
|
||||
* is the form elements finisher|validator property configured to only allow a limited set of values (e.g. Inspector-MultiSelectEditor, Inspector-SingleSelectEditor ...)
|
||||
* YES
|
||||
* is the form elements finisher|validator property value within the set of allowed values?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* valid!
|
||||
* NO
|
||||
* is the form elements finisher|validator property defined in "predefinedDefaults" in the form setup (cannot be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form elements finisher|validator property value equals to the value defined in "predefinedDefaults" in the form setup?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* is there a hmac hash available for the form elements finisher|validator property value (can not be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form elements finisher|validator property value equals the historical value (and is the historical value valid)?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* foreach(form elements finisher|validator properties)
|
||||
* is there a hmac hash available for the form elements finisher|validator property value (can not be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form elements finisher|validator property value equals the historical value (and is the historical value valid)?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* foreach(form element properties) (without finishers|validators)
|
||||
* is there a hmac hash available for the form element property value (cannot be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form element property value equals the historical value (and is the historical value valid)?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* foreach(form elements finisher|validator properties)
|
||||
* is there a hmac hash available for the form elements finisher|validator property value (can not be manipulated but should be written)?
|
||||
* YES
|
||||
* is the form elements finisher|validator property value equals the historical value (and is the historical value valid)?
|
||||
* YES
|
||||
* valid!
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
* NO
|
||||
* invalid! throw exception
|
||||
*
|
||||
* @throws PropertyException
|
||||
*/
|
||||
public function validateFormDefinitionProperties(
|
||||
array $currentFormElement,
|
||||
string $prototypeName,
|
||||
string $sessionToken
|
||||
): void {
|
||||
$renderables = $currentFormElement['renderables'] ?? [];
|
||||
$propertyCollectionElements = $currentFormElement['finishers'] ?? $currentFormElement['validators'] ?? [];
|
||||
$propertyCollectionName = $currentFormElement['type'] === 'Form' ? 'finishers' : 'validators';
|
||||
unset($currentFormElement['renderables'], $currentFormElement['finishers'], $currentFormElement['validators']);
|
||||
|
||||
$validationDto = GeneralUtility::makeInstance(
|
||||
ValidationDto::class,
|
||||
$prototypeName,
|
||||
$currentFormElement['type'],
|
||||
$currentFormElement['identifier'],
|
||||
null,
|
||||
$propertyCollectionName
|
||||
);
|
||||
|
||||
if ($this->configurationService->isFormElementTypeCreatableByFormEditor($validationDto)) {
|
||||
$this->validateAllPropertyValuesFromCreatableFormElement(
|
||||
$currentFormElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
);
|
||||
foreach ($propertyCollectionElements as $propertyCollectionElement) {
|
||||
$validationDto = $validationDto->withPropertyCollectionElementIdentifier(
|
||||
$propertyCollectionElement['identifier']
|
||||
);
|
||||
if ($this->configurationService->isPropertyCollectionElementIdentifierCreatableByFormEditor($validationDto)) {
|
||||
$this->validateAllPropertyValuesFromCreatablePropertyCollectionElement(
|
||||
$propertyCollectionElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
);
|
||||
} else {
|
||||
$this->validateAllPropertyCollectionElementValuesByHmac(
|
||||
$propertyCollectionElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->validateAllFormElementPropertyValuesByHmac($currentFormElement, $sessionToken, $validationDto);
|
||||
foreach ($propertyCollectionElements as $propertyCollectionElement) {
|
||||
$this->validateAllPropertyCollectionElementValuesByHmac(
|
||||
$propertyCollectionElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($renderables as $renderable) {
|
||||
$this->validateFormDefinitionProperties($renderable, $prototypeName, $sessionToken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns TRUE if a property value is equals to the historical value
|
||||
* and FALSE if not.
|
||||
* "Historical values" means values which are available within the form definition
|
||||
* while the form editor is loaded and the values which are available after a
|
||||
* successful validation of the form definition on a save operation.
|
||||
* The value must be equal to the historical value if the property key for the value
|
||||
* is not defined within the form setup.
|
||||
* This means that the property can not be changed by the form editor but we want to keep the value
|
||||
* in its original state.
|
||||
* If this is not the case (return value is FALSE), an exception must be thrown.
|
||||
*
|
||||
* @throws PropertyException
|
||||
*/
|
||||
public function isPropertyValueEqualToHistoricalValue(
|
||||
array $hmacContent,
|
||||
mixed $propertyValue,
|
||||
array $hmacData,
|
||||
string $sessionToken
|
||||
): bool {
|
||||
$this->checkHmacDataIntegrity($hmacData, $hmacContent, $sessionToken);
|
||||
$hmacContent[] = $propertyValue;
|
||||
$expectedHash = $this->hashService->hmac(serialize($hmacContent), $sessionToken);
|
||||
return hash_equals($expectedHash, $hmacData['hmac']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the historical value and the hmac hash to ensure the integrity
|
||||
* of the data.
|
||||
* An exception will be thrown if the value is modified.
|
||||
*
|
||||
* @throws PropertyException
|
||||
*/
|
||||
protected function checkHmacDataIntegrity(array $hmacData, array $hmacContent, string $sessionToken)
|
||||
{
|
||||
$hmac = $hmacData['hmac'] ?? null;
|
||||
if (empty($hmac)) {
|
||||
throw new PropertyException('Hmac must not be empty. #1528538222', 1528538222);
|
||||
}
|
||||
$hmacContent[] = $hmacData['value'] ?? '';
|
||||
$expectedHash = $this->hashService->hmac(serialize($hmacContent), $sessionToken);
|
||||
if (!hash_equals($expectedHash, $hmac)) {
|
||||
throw new PropertyException('Unauthorized modification of historical data. #1528538252', 1528538252);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk through all form element properties and checks
|
||||
* if the values matches to their hmac hashes.
|
||||
*/
|
||||
protected function validateAllFormElementPropertyValuesByHmac(
|
||||
array $currentElement,
|
||||
string $sessionToken,
|
||||
ValidationDto $validationDto
|
||||
): void {
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $currentElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'validateProperties',
|
||||
'^(?!(_orig_.*|.*\._orig_.*)$).*',
|
||||
GeneralUtility::makeInstance(
|
||||
FormElementHmacDataValidator::class,
|
||||
$currentElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk through all property collection properties and checks
|
||||
* if the values matches to their hmac hashes.
|
||||
*/
|
||||
protected function validateAllPropertyCollectionElementValuesByHmac(
|
||||
array $currentElement,
|
||||
string $sessionToken,
|
||||
ValidationDto $validationDto
|
||||
): void {
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $currentElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'validateProperties',
|
||||
'^(?!(_orig_.*|.*\._orig_.*)$).*',
|
||||
GeneralUtility::makeInstance(
|
||||
PropertyCollectionElementHmacDataValidator::class,
|
||||
$currentElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk through all form element properties and checks
|
||||
* if the property is defined within the form editor setup
|
||||
* or if the property is defined within the "predefinedDefaults" in the form editor setup
|
||||
* and the property value matches the predefined value
|
||||
* or if there is a valid hmac hash for the value.
|
||||
*/
|
||||
protected function validateAllPropertyValuesFromCreatableFormElement(
|
||||
array $currentElement,
|
||||
string $sessionToken,
|
||||
ValidationDto $validationDto
|
||||
): void {
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $currentElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'validateProperties',
|
||||
'^(?!(_orig_.*|.*\._orig_.*|type|identifier)$).*',
|
||||
GeneralUtility::makeInstance(
|
||||
CreatableFormElementPropertiesValidator::class,
|
||||
$currentElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk through all property collection properties and checks
|
||||
* if the property is defined within the form editor setup
|
||||
* or if the property is defined within the "predefinedDefaults" in the form editor setup
|
||||
* and the property value matches the predefined value
|
||||
* or if there is a valid hmac hash for the value.
|
||||
*/
|
||||
protected function validateAllPropertyValuesFromCreatablePropertyCollectionElement(
|
||||
array $currentElement,
|
||||
string $sessionToken,
|
||||
ValidationDto $validationDto
|
||||
): void {
|
||||
GeneralUtility::makeInstance(ArrayProcessor::class, $currentElement)->forEach(
|
||||
GeneralUtility::makeInstance(
|
||||
ArrayProcessing::class,
|
||||
'validateProperties',
|
||||
'^(?!(_orig_.*|.*\._orig_.*|identifier)$).*',
|
||||
GeneralUtility::makeInstance(
|
||||
CreatablePropertyCollectionElementPropertiesValidator::class,
|
||||
$currentElement,
|
||||
$sessionToken,
|
||||
$validationDto
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractExtractor implements ExtractorInterface
|
||||
{
|
||||
/**
|
||||
* @var ExtractorDto
|
||||
*/
|
||||
protected $extractorDto;
|
||||
|
||||
public function __construct(ExtractorDto $extractorDto)
|
||||
{
|
||||
$this->extractorDto = $extractorDto;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class AdditionalElementPropertyPathsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType] = $matches;
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['formElements'][$formElementType]['additionalElementPropertyPaths'][] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ExtractorDto
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $prototypeConfiguration;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $result = [];
|
||||
|
||||
public function __construct(array $prototypeConfiguration)
|
||||
{
|
||||
$this->prototypeConfiguration = $prototypeConfiguration;
|
||||
}
|
||||
|
||||
public function getPrototypeConfiguration(): array
|
||||
{
|
||||
return $this->prototypeConfiguration;
|
||||
}
|
||||
|
||||
public function getResult(): array
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function setResult(array $result): ExtractorDto
|
||||
{
|
||||
$this->result = $result;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface ExtractorInterface
|
||||
{
|
||||
public function __construct(ExtractorDto $extractorDto);
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $key, $value, array $matches);
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class IsCreatableFormElementExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType] = $matches;
|
||||
|
||||
$formElementGroup = $value;
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
|
||||
if (!ArrayUtility::isValidPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
'formElementsDefinition.' . $formElementType . '.formEditor.groupSorting',
|
||||
'.'
|
||||
)) {
|
||||
$result['formElements'][$formElementType]['creatable'] = false;
|
||||
$this->extractorDto->setResult($result);
|
||||
return;
|
||||
}
|
||||
|
||||
$formElementGroups = array_keys(
|
||||
ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), 'formEditor.formElementGroups', '.')
|
||||
);
|
||||
|
||||
$result['formElements'][$formElementType]['creatable'] = in_array(
|
||||
$formElementGroup,
|
||||
$formElementGroups,
|
||||
true
|
||||
);
|
||||
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class MultiValuePropertiesExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $formEditorIndex] = $matches;
|
||||
|
||||
if (
|
||||
$value !== 'Inspector-PropertyGridEditor'
|
||||
&& $value !== 'Inspector-MultiSelectEditor'
|
||||
&& $value !== 'Inspector-CountrySelectEditor'
|
||||
&& $value !== 'Inspector-ValidationErrorMessageEditor'
|
||||
&& $value !== 'Inspector-RequiredValidatorEditor'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value === 'Inspector-RequiredValidatorEditor') {
|
||||
$propertyPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'configurationOptions',
|
||||
'validationErrorMessage',
|
||||
'propertyPath',
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$propertyPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'propertyPath',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
|
||||
if (ArrayUtility::isValidPath($this->extractorDto->getPrototypeConfiguration(), $propertyPath, '.')) {
|
||||
$result['formElements'][$formElementType]['multiValueProperties'][] = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
$propertyPath,
|
||||
'.'
|
||||
);
|
||||
}
|
||||
|
||||
if ($value === 'Inspector-PropertyGridEditor') {
|
||||
$result['formElements'][$formElementType]['multiValueProperties'][] = 'defaultValue';
|
||||
}
|
||||
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PredefinedDefaultsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $propertyPath] = $matches;
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['formElements'][$formElementType]['predefinedDefaults'][$propertyPath] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PropertyPathsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
$formElementPropertyPaths = $this->getPropertyPaths($value, $matches);
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result = array_merge_recursive($result, ['formElements' => $formElementPropertyPaths]);
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
|
||||
protected function getPropertyPaths(string $value, array $matches): array
|
||||
{
|
||||
$paths = [];
|
||||
[, $formElementType, $formEditorIndex] = $matches;
|
||||
|
||||
$paths[$formElementType]['propertyPaths'] = [];
|
||||
$templateNamePath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'templateName',
|
||||
]
|
||||
);
|
||||
$templateName = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
$templateNamePath,
|
||||
'.'
|
||||
);
|
||||
|
||||
// Special processing of "Inspector-GridColumnViewPortConfigurationEditor" inspector editors.
|
||||
// Expand the property path which contains a "{@viewPortIdentifier}" placeholder
|
||||
// to X property paths which contain all available placeholder replacements.
|
||||
if ($templateName === 'Inspector-GridColumnViewPortConfigurationEditor') {
|
||||
$viewPortsPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'configurationOptions',
|
||||
'viewPorts',
|
||||
]
|
||||
);
|
||||
$viewPorts = ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), $viewPortsPath, '.');
|
||||
foreach ($viewPorts as $viewPort) {
|
||||
$viewPortIdentifier = $viewPort['viewPortIdentifier'];
|
||||
$propertyPath = str_replace('{@viewPortIdentifier}', $viewPortIdentifier, $value);
|
||||
$paths[$formElementType]['propertyPaths'][] = $propertyPath;
|
||||
}
|
||||
} else {
|
||||
$paths[$formElementType]['propertyPaths'][] = $value;
|
||||
}
|
||||
return $paths;
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SelectOptionsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $formEditorIndex] = $matches;
|
||||
|
||||
$templateName = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'templateName',
|
||||
]
|
||||
),
|
||||
'.'
|
||||
);
|
||||
|
||||
if ($templateName === 'Inspector-FinishersEditor') {
|
||||
$propertyPath = '_finishers';
|
||||
} elseif ($templateName === 'Inspector-ValidatorsEditor') {
|
||||
$propertyPath = '_validators';
|
||||
} else {
|
||||
if ($templateName === 'Inspector-RequiredValidatorEditor') {
|
||||
$propertyPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'configurationOptions',
|
||||
'validationErrorMessage',
|
||||
'propertyPath',
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$propertyPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'propertyPath',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$propertyPath = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
$propertyPath,
|
||||
'.'
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['formElements'][$formElementType]['selectOptions'][$propertyPath][] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class IsCreatablePropertyCollectionElementExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $formEditorIndex] = $matches;
|
||||
|
||||
if (
|
||||
$value !== 'Inspector-FinishersEditor'
|
||||
&& $value !== 'Inspector-ValidatorsEditor'
|
||||
&& $value !== 'Inspector-RequiredValidatorEditor'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$propertyCollectionName = $value === 'Inspector-FinishersEditor' ? 'finishers' : 'validators';
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
|
||||
if (
|
||||
$value === 'Inspector-FinishersEditor'
|
||||
|| $value === 'Inspector-ValidatorsEditor'
|
||||
) {
|
||||
$selectOptionsPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'selectOptions',
|
||||
]
|
||||
);
|
||||
if (!ArrayUtility::isValidPath($this->extractorDto->getPrototypeConfiguration(), $selectOptionsPath, '.')) {
|
||||
return;
|
||||
}
|
||||
$selectOptions = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
$selectOptionsPath,
|
||||
'.'
|
||||
);
|
||||
foreach ($selectOptions as $selectOption) {
|
||||
$validatorIdentifier = $selectOption['value'] ?? '';
|
||||
if (empty($validatorIdentifier)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result['formElements'][$formElementType]['collections'][$propertyCollectionName][$validatorIdentifier]['creatable'] = true;
|
||||
}
|
||||
} else {
|
||||
$validatorIdentifierPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'editors',
|
||||
$formEditorIndex,
|
||||
'validatorIdentifier',
|
||||
]
|
||||
);
|
||||
if (!ArrayUtility::isValidPath($this->extractorDto->getPrototypeConfiguration(), $validatorIdentifierPath, '.')) {
|
||||
return;
|
||||
}
|
||||
$validatorIdentifier = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
$validatorIdentifierPath,
|
||||
'.'
|
||||
);
|
||||
$result['formElements'][$formElementType]['collections'][$propertyCollectionName][$validatorIdentifier]['creatable'] = true;
|
||||
}
|
||||
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class MultiValuePropertiesExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $propertyCollectionName, $propertyCollectionIndex, $propertyCollectionEditorIndex] = $matches;
|
||||
|
||||
if (
|
||||
$value !== 'Inspector-PropertyGridEditor'
|
||||
&& $value !== 'Inspector-MultiSelectEditor'
|
||||
&& $value !== 'Inspector-CountrySelectEditor'
|
||||
&& $value !== 'Inspector-ValidationErrorMessageEditor'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$propertyPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'propertyCollections',
|
||||
$propertyCollectionName,
|
||||
$propertyCollectionIndex,
|
||||
'editors',
|
||||
$propertyCollectionEditorIndex,
|
||||
'propertyPath',
|
||||
]
|
||||
);
|
||||
$propertyValue = ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), $propertyPath, '.');
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
|
||||
if (
|
||||
$value === 'Inspector-PropertyGridEditor'
|
||||
|| $value === 'Inspector-MultiSelectEditor'
|
||||
) {
|
||||
$identifierPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'propertyCollections',
|
||||
$propertyCollectionName,
|
||||
$propertyCollectionIndex,
|
||||
'identifier',
|
||||
]
|
||||
);
|
||||
$identifier = ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), $identifierPath, '.');
|
||||
|
||||
$result['formElements'][$formElementType]['collections'][$propertyCollectionName][$identifier]['multiValueProperties'][] = $propertyValue;
|
||||
if ($value === 'Inspector-PropertyGridEditor') {
|
||||
$result['formElements'][$formElementType]['collections'][$propertyCollectionName][$identifier]['multiValueProperties'][] = 'defaultValue';
|
||||
}
|
||||
} else {
|
||||
$result['formElements'][$formElementType]['multiValueProperties'][] = $propertyValue;
|
||||
}
|
||||
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PredefinedDefaultsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $propertyCollectionName, $propertyCollectionElementIdentifier, $propertyPath] = $matches;
|
||||
$propertyCollectionName = str_replace('Definition', '', $propertyCollectionName);
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['collections'][$propertyCollectionName][$propertyCollectionElementIdentifier]['predefinedDefaults'][$propertyPath] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class PropertyPathsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $propertyCollectionName, $propertyCollectionIndex] = $matches;
|
||||
|
||||
$identifierPath = implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'propertyCollections',
|
||||
$propertyCollectionName,
|
||||
$propertyCollectionIndex,
|
||||
'identifier',
|
||||
]
|
||||
);
|
||||
$identifier = ArrayUtility::getValueByPath($this->extractorDto->getPrototypeConfiguration(), $identifierPath, '.');
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['formElements'][$formElementType]['collections'][$propertyCollectionName][$identifier]['propertyPaths'][] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\AbstractExtractor;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SelectOptionsExtractor extends AbstractExtractor
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __invoke(string $_, $value, array $matches)
|
||||
{
|
||||
[, $formElementType, $propertyCollectionName, $propertyCollectionIndex, $propertyCollectionEditorIndex] = $matches;
|
||||
|
||||
$propertyPath = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'propertyCollections',
|
||||
$propertyCollectionName,
|
||||
$propertyCollectionIndex,
|
||||
'editors',
|
||||
$propertyCollectionEditorIndex,
|
||||
'propertyPath',
|
||||
]
|
||||
),
|
||||
'.'
|
||||
);
|
||||
|
||||
$propertyCollectionElementIdentifier = ArrayUtility::getValueByPath(
|
||||
$this->extractorDto->getPrototypeConfiguration(),
|
||||
implode(
|
||||
'.',
|
||||
[
|
||||
'formElementsDefinition',
|
||||
$formElementType,
|
||||
'formEditor',
|
||||
'propertyCollections',
|
||||
$propertyCollectionName,
|
||||
$propertyCollectionIndex,
|
||||
'identifier',
|
||||
]
|
||||
),
|
||||
'.'
|
||||
);
|
||||
|
||||
$propertyCollectionName = str_replace('Definition', '', $propertyCollectionName);
|
||||
|
||||
$result = $this->extractorDto->getResult();
|
||||
$result['collections'][$propertyCollectionName][$propertyCollectionElementIdentifier]['selectOptions'][$propertyPath][] = $value;
|
||||
$this->extractorDto->setResult($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Configuration;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface as ExtbaseConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Form\Domain\DTO\PersistenceManagerConfiguration;
|
||||
use TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManagerInterface as ExtFormConfigurationManagerInterface;
|
||||
|
||||
/**
|
||||
* Service for accessing form storage configuration (persistenceManager settings)
|
||||
*
|
||||
* This service provides a clean interface to access form storage related settings
|
||||
* from the YAML configuration without coupling every component to the configuration
|
||||
* loading mechanism.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
final readonly class PersistenceConfigurationService
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(lazy: true)]
|
||||
private ExtbaseConfigurationManagerInterface $extbaseConfigurationManager,
|
||||
#[Autowire(lazy: ExtFormConfigurationManagerInterface::class)]
|
||||
private ExtFormConfigurationManagerInterface $extFormConfigurationManager,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get all form settings
|
||||
*/
|
||||
public function getFormSettings(): array
|
||||
{
|
||||
$isFrontend = $this->isFrontendRequest();
|
||||
$request = $this->getCurrentRequest();
|
||||
|
||||
$typoScriptSettings = $this->extbaseConfigurationManager->getConfiguration(
|
||||
ExtbaseConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
|
||||
'form'
|
||||
);
|
||||
|
||||
return $this->extFormConfigurationManager->getYamlConfiguration(
|
||||
$typoScriptSettings,
|
||||
$isFrontend,
|
||||
$isFrontend ? $request : null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get persistence manager settings as a typed DTO
|
||||
*/
|
||||
public function getPersistenceManagerConfiguration(): PersistenceManagerConfiguration
|
||||
{
|
||||
$formSettings = $this->getFormSettings();
|
||||
return PersistenceManagerConfiguration::fromArray($formSettings['persistenceManager'] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowed extension paths from form configuration
|
||||
*
|
||||
* @return string[] Array of allowed extension paths (e.g., ["EXT:my_extension/Configuration/Forms/"])
|
||||
*/
|
||||
public function getAllowedExtensionPaths(): array
|
||||
{
|
||||
return $this->getPersistenceManagerConfiguration()->allowedExtensionPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get allowed pages for form storage.
|
||||
*
|
||||
* Forms are always stored on pid 0 (root level).
|
||||
*
|
||||
* @return array<int, array{uid: int, title: string}> Array of allowed page IDs
|
||||
*/
|
||||
public function getAllowedPages(): array
|
||||
{
|
||||
return [
|
||||
0 => [
|
||||
'uid' => 0,
|
||||
'title' => 'Root',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if saving to extension paths is allowed
|
||||
*/
|
||||
public function isAllowedToSaveToExtensionPaths(): bool
|
||||
{
|
||||
return $this->getPersistenceManagerConfiguration()->allowSaveToExtensionPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if deleting from extension paths is allowed
|
||||
*/
|
||||
public function isAllowedToDeleteFromExtensionPaths(): bool
|
||||
{
|
||||
return $this->getPersistenceManagerConfiguration()->allowDeleteFromExtensionPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sort configuration for form listing
|
||||
*
|
||||
* @return array{sortByKeys: string[], sortAscending: bool}
|
||||
*/
|
||||
public function getSortConfiguration(): array
|
||||
{
|
||||
$configuration = $this->getPersistenceManagerConfiguration();
|
||||
|
||||
return [
|
||||
'sortByKeys' => $configuration->sortByKeys,
|
||||
'sortAscending' => $configuration->sortAscending,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current request is a frontend request
|
||||
*/
|
||||
private function isFrontendRequest(): bool
|
||||
{
|
||||
$request = $this->getCurrentRequest();
|
||||
|
||||
if ($request !== null) {
|
||||
return ApplicationType::fromRequest($request)->isFrontend();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current request from globals
|
||||
*/
|
||||
private function getCurrentRequest(): ?ServerRequestInterface
|
||||
{
|
||||
return $GLOBALS['TYPO3_REQUEST'] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\DTO;
|
||||
|
||||
/**
|
||||
* FormData - Complete form definition DTO
|
||||
* Used for read/write operations with full form structure
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class FormData
|
||||
{
|
||||
public function __construct(
|
||||
public string $identifier,
|
||||
public string $type,
|
||||
public string $name,
|
||||
public string $prototypeName,
|
||||
public array $renderingOptions,
|
||||
public array $finishers,
|
||||
public array $renderables,
|
||||
public array $variants,
|
||||
) {}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
identifier: $data['identifier'] ?? '',
|
||||
type: $data['type'] ?? 'Form',
|
||||
name: $data['label'] ?? $data['identifier'] ?? '',
|
||||
prototypeName: $data['prototypeName'] ?? 'standard',
|
||||
renderingOptions: $data['renderingOptions'] ?? [],
|
||||
finishers: $data['finishers'] ?? [],
|
||||
renderables: $data['renderables'] ?? [],
|
||||
variants: $data['variants'] ?? [],
|
||||
);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'identifier' => $this->identifier,
|
||||
'type' => $this->type,
|
||||
'label' => $this->name,
|
||||
'prototypeName' => $this->prototypeName,
|
||||
'renderingOptions' => $this->renderingOptions,
|
||||
'finishers' => $this->finishers,
|
||||
'renderables' => $this->renderables,
|
||||
'variants' => $this->variants,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\DTO;
|
||||
|
||||
/**
|
||||
* FormMetadata - Lightweight DTO for form listings
|
||||
* Contains only metadata, not the full form definition
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class FormMetadata
|
||||
{
|
||||
public function __construct(
|
||||
public string $identifier,
|
||||
public string $type,
|
||||
public string $name,
|
||||
public string $prototypeName,
|
||||
public ?string $persistenceIdentifier = null,
|
||||
public bool $invalid = false,
|
||||
public bool $readOnly = false,
|
||||
public bool $removable = true,
|
||||
public ?string $storageType = null,
|
||||
public bool $duplicateIdentifier = false,
|
||||
public ?int $fileUid = null,
|
||||
public int $referenceCount = 0,
|
||||
public ?string $editUrl = null,
|
||||
public ?string $storageLocation = null,
|
||||
public array $actions = [],
|
||||
) {}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
identifier: $data['identifier'] ?? '',
|
||||
type: $data['type'] ?? 'Form',
|
||||
name: $data['label'] ?? $data['identifier'] ?? '',
|
||||
prototypeName: $data['prototypeName'] ?? 'standard',
|
||||
persistenceIdentifier: $data['persistenceIdentifier'] ?? null,
|
||||
invalid: $data['invalid'] ?? false,
|
||||
readOnly: $data['readOnly'] ?? false,
|
||||
removable: $data['removable'] ?? true,
|
||||
storageType: $data['storageType'] ?? null,
|
||||
duplicateIdentifier: $data['duplicateIdentifier'] ?? false,
|
||||
fileUid: $data['fileUid'] ?? null,
|
||||
referenceCount: $data['referenceCount'] ?? 0,
|
||||
editUrl: $data['editUrl'] ?? null,
|
||||
storageLocation: $data['storageLocation'] ?? null,
|
||||
actions: $data['actions'] ?? [],
|
||||
);
|
||||
}
|
||||
|
||||
public static function createInvalid(
|
||||
string $persistenceIdentifier,
|
||||
string $errorMessage
|
||||
): self {
|
||||
return new self(
|
||||
identifier: $persistenceIdentifier,
|
||||
type: 'Form',
|
||||
name: $errorMessage,
|
||||
prototypeName: 'standard',
|
||||
persistenceIdentifier: $persistenceIdentifier,
|
||||
invalid: true,
|
||||
);
|
||||
}
|
||||
|
||||
public static function createFromYaml(
|
||||
array $yamlData,
|
||||
string $persistenceIdentifier,
|
||||
?int $fileUid = null
|
||||
): self {
|
||||
return self::fromArray($yamlData)
|
||||
->withPersistenceIdentifier($persistenceIdentifier)
|
||||
->withFileUid($fileUid);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'identifier' => $this->identifier,
|
||||
'type' => $this->type,
|
||||
'label' => $this->name,
|
||||
'name' => $this->name,
|
||||
'prototypeName' => $this->prototypeName,
|
||||
'persistenceIdentifier' => $this->persistenceIdentifier ?? $this->identifier,
|
||||
'invalid' => $this->invalid,
|
||||
'readOnly' => $this->readOnly,
|
||||
'removable' => $this->removable,
|
||||
'storageType' => $this->storageType,
|
||||
'storageLocation' => $this->storageLocation ?? $this->storageType,
|
||||
'duplicateIdentifier' => $this->duplicateIdentifier,
|
||||
'fileUid' => $this->fileUid,
|
||||
'referenceCount' => $this->referenceCount,
|
||||
'editUrl' => $this->editUrl,
|
||||
'actions' => $this->actions,
|
||||
];
|
||||
}
|
||||
|
||||
private function with(array $changes): self
|
||||
{
|
||||
return new self(
|
||||
identifier: $changes['identifier'] ?? $this->identifier,
|
||||
type: $changes['type'] ?? $this->type,
|
||||
name: $changes['name'] ?? $this->name,
|
||||
prototypeName: $changes['prototypeName'] ?? $this->prototypeName,
|
||||
persistenceIdentifier: $changes['persistenceIdentifier'] ?? $this->persistenceIdentifier,
|
||||
invalid: $changes['invalid'] ?? $this->invalid,
|
||||
readOnly: $changes['readOnly'] ?? $this->readOnly,
|
||||
removable: $changes['removable'] ?? $this->removable,
|
||||
storageType: $changes['storageType'] ?? $this->storageType,
|
||||
duplicateIdentifier: $changes['duplicateIdentifier'] ?? $this->duplicateIdentifier,
|
||||
fileUid: $changes['fileUid'] ?? $this->fileUid,
|
||||
referenceCount: $changes['referenceCount'] ?? $this->referenceCount,
|
||||
editUrl: $changes['editUrl'] ?? $this->editUrl,
|
||||
storageLocation: $changes['storageLocation'] ?? $this->storageLocation,
|
||||
actions: $changes['actions'] ?? $this->actions,
|
||||
);
|
||||
}
|
||||
|
||||
public function withPersistenceIdentifier(string $persistenceIdentifier): self
|
||||
{
|
||||
return $this->with(['persistenceIdentifier' => $persistenceIdentifier]);
|
||||
}
|
||||
|
||||
public function withStorageType(string $storageType): self
|
||||
{
|
||||
return $this->with(['storageType' => $storageType]);
|
||||
}
|
||||
|
||||
public function withDuplicateIdentifier(bool $duplicateIdentifier): self
|
||||
{
|
||||
return $this->with(['duplicateIdentifier' => $duplicateIdentifier]);
|
||||
}
|
||||
|
||||
public function withReadOnly(bool $readOnly): self
|
||||
{
|
||||
return $this->with(['readOnly' => $readOnly]);
|
||||
}
|
||||
|
||||
public function withRemovable(bool $removable): self
|
||||
{
|
||||
return $this->with(['removable' => $removable]);
|
||||
}
|
||||
|
||||
public function withFileUid(?int $fileUid): self
|
||||
{
|
||||
return $this->with(['fileUid' => $fileUid]);
|
||||
}
|
||||
|
||||
public function withReferenceCount(int $referenceCount): self
|
||||
{
|
||||
return $this->with(['referenceCount' => $referenceCount]);
|
||||
}
|
||||
|
||||
public function withInvalid(bool $invalid): self
|
||||
{
|
||||
return $this->with(['invalid' => $invalid]);
|
||||
}
|
||||
|
||||
public function withEditUrl(string $editUrl): self
|
||||
{
|
||||
return $this->with(['editUrl' => $editUrl]);
|
||||
}
|
||||
|
||||
public function withStorageLocation(?string $storageLocation): self
|
||||
{
|
||||
return $this->with(['storageLocation' => $storageLocation]);
|
||||
}
|
||||
|
||||
public function withActions(array $actions): self
|
||||
{
|
||||
return $this->with(['actions' => $actions]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a comparable scalar value for the given sort field.
|
||||
*
|
||||
* Field names in SearchCriteria::ORDER_FIELDS are intentionally kept
|
||||
* identical to the property names of this class, so a dynamic lookup
|
||||
* is sufficient. Unknown fields yield null and are skipped by the
|
||||
* caller. Booleans are cast to int for correct numeric ordering.
|
||||
*/
|
||||
public function getSortableValue(string $field): int|string|null
|
||||
{
|
||||
if (!property_exists($this, $field)) {
|
||||
return null;
|
||||
}
|
||||
$value = $this->$field;
|
||||
if (is_bool($value)) {
|
||||
return (int)$value;
|
||||
}
|
||||
return is_int($value) || is_string($value) ? $value : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\DTO;
|
||||
|
||||
/**
|
||||
* Typed representation of the "persistenceManager" section of the form
|
||||
* YAML configuration.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class PersistenceManagerConfiguration
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
public const DEFAULT_SORT_BY_KEYS = ['name', 'fileUid'];
|
||||
|
||||
/**
|
||||
* @param list<string> $sortByKeys Keys the forms are sorted by in the form manager and plugin select
|
||||
* @param list<string> $allowedExtensionPaths EXT: paths that contain forms shipped within extensions
|
||||
* @param list<string> $allowedFileMounts File mounts forms may be stored in
|
||||
*/
|
||||
public function __construct(
|
||||
public bool $allowSaveToExtensionPaths = false,
|
||||
public bool $allowDeleteFromExtensionPaths = false,
|
||||
public array $sortByKeys = self::DEFAULT_SORT_BY_KEYS,
|
||||
public bool $sortAscending = true,
|
||||
public array $allowedExtensionPaths = [],
|
||||
public array $allowedFileMounts = [],
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create the DTO from the raw "persistenceManager" configuration array.
|
||||
*
|
||||
* @param array<string, mixed> $configuration
|
||||
*/
|
||||
public static function fromArray(array $configuration): self
|
||||
{
|
||||
return new self(
|
||||
allowSaveToExtensionPaths: (bool)($configuration['allowSaveToExtensionPaths'] ?? false),
|
||||
allowDeleteFromExtensionPaths: (bool)($configuration['allowDeleteFromExtensionPaths'] ?? false),
|
||||
sortByKeys: self::normalizeStringList($configuration['sortByKeys'] ?? null, self::DEFAULT_SORT_BY_KEYS),
|
||||
sortAscending: (bool)($configuration['sortAscending'] ?? true),
|
||||
allowedExtensionPaths: self::normalizeStringList($configuration['allowedExtensionPaths'] ?? null, []),
|
||||
allowedFileMounts: self::normalizeStringList($configuration['allowedFileMounts'] ?? null, []),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a configuration value into a numerically indexed list of strings.
|
||||
*
|
||||
* The YAML configuration may use associative keys (e.g. `10:`, `20:`) to
|
||||
* define ordering, so values are cast to strings and re-indexed.
|
||||
*
|
||||
* @param list<string> $default
|
||||
* @return list<string>
|
||||
*/
|
||||
private static function normalizeStringList(mixed $value, array $default): array
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return array_values(array_map(strval(...), $value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\DTO;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Search criteria for filtering and sorting form lists
|
||||
*
|
||||
* Follows TYPO3 Demand pattern naming conventions:
|
||||
* - searchTerm: Text to search for in form properties
|
||||
* - orderField: Field name to sort by
|
||||
* - orderDirection: Sort direction ('asc' or 'desc')
|
||||
* - limit: Maximum number of results
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class SearchCriteria
|
||||
{
|
||||
private const string ORDER_ASCENDING = 'asc';
|
||||
private const string ORDER_DESCENDING = 'desc';
|
||||
private const string DEFAULT_ORDER_FIELD = 'name';
|
||||
|
||||
/**
|
||||
* Allowed sort fields. Each entry MUST match a public property name of
|
||||
* FormMetadata exactly, because FormMetadata::getSortableValue() uses
|
||||
* dynamic property access ($this->$field) instead of an explicit mapping.
|
||||
*/
|
||||
private const array ORDER_FIELDS = ['name', 'identifier', 'persistenceIdentifier', 'prototypeName', 'storageLocation', 'duplicateIdentifier', 'referenceCount'];
|
||||
|
||||
public string $orderField;
|
||||
public string $orderDirection;
|
||||
|
||||
public function __construct(
|
||||
public ?string $searchTerm = null,
|
||||
?string $orderField = null,
|
||||
?string $orderDirection = null,
|
||||
public ?int $limit = null,
|
||||
) {
|
||||
// Validate and normalize orderField
|
||||
$this->orderField = in_array($orderField, self::ORDER_FIELDS, true)
|
||||
? $orderField
|
||||
: self::DEFAULT_ORDER_FIELD;
|
||||
|
||||
// Validate and normalize orderDirection
|
||||
$this->orderDirection = in_array($orderDirection, [self::ORDER_ASCENDING, self::ORDER_DESCENDING], true)
|
||||
? $orderDirection
|
||||
: self::ORDER_ASCENDING;
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
searchTerm: $data['searchTerm'] ?? null,
|
||||
orderField: $data['orderField'] ?? null,
|
||||
orderDirection: $data['orderDirection'] ?? null,
|
||||
limit: $data['limit'] ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromRequest(ServerRequestInterface $request): self
|
||||
{
|
||||
$queryParams = $request->getQueryParams();
|
||||
$parsedBody = $request->getParsedBody() ?? [];
|
||||
|
||||
return new self(
|
||||
searchTerm: $queryParams['searchTerm'] ?? $parsedBody['searchTerm'] ?? null,
|
||||
orderField: $queryParams['orderField'] ?? $parsedBody['orderField'] ?? null,
|
||||
orderDirection: $queryParams['orderDirection'] ?? $parsedBody['orderDirection'] ?? null,
|
||||
limit: isset($queryParams['limit']) ? (int)$queryParams['limit'] : (isset($parsedBody['limit']) ? (int)$parsedBody['limit'] : null),
|
||||
);
|
||||
}
|
||||
|
||||
public function getOrderField(): string
|
||||
{
|
||||
return $this->orderField;
|
||||
}
|
||||
|
||||
public function getOrderDirection(): string
|
||||
{
|
||||
return $this->orderDirection;
|
||||
}
|
||||
|
||||
public function getDefaultOrderDirection(): string
|
||||
{
|
||||
return self::ORDER_ASCENDING;
|
||||
}
|
||||
|
||||
public function getReverseOrderDirection(): string
|
||||
{
|
||||
return $this->orderDirection === self::ORDER_ASCENDING
|
||||
? self::ORDER_DESCENDING
|
||||
: self::ORDER_ASCENDING;
|
||||
}
|
||||
|
||||
public function getSearchTerm(): ?string
|
||||
{
|
||||
return $this->searchTerm;
|
||||
}
|
||||
|
||||
public function hasSearchTerm(): bool
|
||||
{
|
||||
return $this->searchTerm !== null && $this->searchTerm !== '';
|
||||
}
|
||||
|
||||
public function getLimit(): ?int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
public function hasLimit(): bool
|
||||
{
|
||||
return $this->limit !== null && $this->limit > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if any filter/search constraints are set
|
||||
*/
|
||||
public function hasConstraints(): bool
|
||||
{
|
||||
return $this->hasSearchTerm() || $this->hasLimit();
|
||||
}
|
||||
|
||||
public function getParameters(): array
|
||||
{
|
||||
$parameters = [];
|
||||
if ($this->hasSearchTerm()) {
|
||||
$parameters['searchTerm'] = $this->searchTerm;
|
||||
}
|
||||
if ($this->hasLimit()) {
|
||||
$parameters['limit'] = $this->limit;
|
||||
}
|
||||
$parameters['orderField'] = $this->orderField;
|
||||
$parameters['orderDirection'] = $this->orderDirection;
|
||||
return $parameters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\DTO;
|
||||
|
||||
/**
|
||||
* Storage context for form persistence operations
|
||||
* Contains additional metadata required for storing forms
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class StorageContext
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $pid = null,
|
||||
) {}
|
||||
|
||||
public static function create(?int $pid = null): self
|
||||
{
|
||||
return new self($pid);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain;
|
||||
|
||||
use TYPO3\CMS\Form\Exception as FormException;
|
||||
|
||||
/**
|
||||
* A generic Form domain Exception
|
||||
*/
|
||||
class Exception extends FormException {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if the "identifier" for a Form, a Page or a Form Element
|
||||
* is invalid (i.e. empty or not a string)
|
||||
*/
|
||||
class IdentifierNotValidException extends Exception {}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a rendering error occurs
|
||||
*/
|
||||
class RenderingException extends Exception {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a Type Definition for a form element was not found,
|
||||
* or if the implementationClassName was not set.
|
||||
*/
|
||||
class TypeDefinitionNotFoundException extends Exception {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a Type Definition for a form element was not valid,
|
||||
* i.e. it has properties which are not supported.
|
||||
*/
|
||||
class TypeDefinitionNotValidException extends Exception {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if the ArrayFormFactory want to create child
|
||||
* elements within a unknown composite renderable.
|
||||
*/
|
||||
class UnknownCompositRenderableException extends Exception {}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Factory;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\FormDefinitionConversionService;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
|
||||
use TYPO3\CMS\Form\Event\AfterFormIsBuiltEvent;
|
||||
|
||||
/**
|
||||
* Base class for custom *Form Factories*. A Form Factory is responsible for building
|
||||
* a {@link TYPO3\CMS\Form\Domain\Model\FormDefinition}.
|
||||
*
|
||||
* Example
|
||||
* =======
|
||||
*
|
||||
* Generally, you should use this class as follows:
|
||||
*
|
||||
* <pre>
|
||||
* class MyFooBarFactory extends AbstractFormFactory {
|
||||
* public function build(array $configuration, $prototypeName) {
|
||||
* $configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
|
||||
* $prototypeConfiguration = $configurationService->getPrototypeConfiguration($prototypeName);
|
||||
* $formDefinition = GeneralUtility::makeInstance(FormDefinition::class, 'nameOfMyForm', $prototypeConfiguration);
|
||||
*
|
||||
* // now, you should call methods on $formDefinition to add pages and form elements
|
||||
*
|
||||
* return $formDefinition;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Scope: frontend / backend
|
||||
* **This class is meant to be sub classed by developers.**
|
||||
*/
|
||||
abstract class AbstractFormFactory implements FormFactoryInterface
|
||||
{
|
||||
protected ?EventDispatcherInterface $eventDispatcher = null;
|
||||
protected ?FormDefinitionConversionService $formDefinitionConversionService = null;
|
||||
|
||||
public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
public function injectFormDefinitionConversionService(FormDefinitionConversionService $formDefinitionConversionService): void
|
||||
{
|
||||
$this->formDefinitionConversionService = $formDefinitionConversionService;
|
||||
}
|
||||
|
||||
protected function triggerFormBuildingFinished(FormDefinition $form): FormDefinition
|
||||
{
|
||||
return $this->eventDispatcher->dispatch(new AfterFormIsBuiltEvent($form))->form;
|
||||
}
|
||||
|
||||
protected function getFormDefinitionConversionService(): FormDefinitionConversionService
|
||||
{
|
||||
return $this->formDefinitionConversionService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Factory;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Configuration\ConfigurationService;
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Exception\RenderingException;
|
||||
use TYPO3\CMS\Form\Domain\Exception\UnknownCompositRenderableException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface;
|
||||
use TYPO3\CMS\Form\Event\BeforeRenderableIsAddedToFormEvent;
|
||||
|
||||
/**
|
||||
* A factory that creates a FormDefinition from an array
|
||||
*
|
||||
* Scope: frontend / backend
|
||||
*/
|
||||
#[Autoconfigure(public: true, shared: false)]
|
||||
class ArrayFormFactory extends AbstractFormFactory
|
||||
{
|
||||
/**
|
||||
* Build a form definition, depending on some configuration.
|
||||
*
|
||||
* @throws RenderingException
|
||||
* @internal
|
||||
*/
|
||||
public function build(
|
||||
array $configuration,
|
||||
?string $prototypeName = null,
|
||||
?ServerRequestInterface $request = null
|
||||
): FormDefinition {
|
||||
if (empty($prototypeName)) {
|
||||
$prototypeName = $configuration['prototypeName'] ?? 'standard';
|
||||
}
|
||||
$persistenceIdentifier = $configuration['persistenceIdentifier'] ?? null;
|
||||
|
||||
// Get prototype configuration once and reuse it
|
||||
$prototypeConfiguration = GeneralUtility::makeInstance(ConfigurationService::class)
|
||||
->getPrototypeConfiguration($prototypeName);
|
||||
|
||||
// Get RTE property paths for proper sanitization
|
||||
$rtePropertyPaths = $this->getFormDefinitionConversionService()->extractRtePropertyPaths($prototypeConfiguration);
|
||||
|
||||
$configuration = $this->getFormDefinitionConversionService()->sanitizeHtml($configuration, $rtePropertyPaths);
|
||||
|
||||
if ($configuration['invalid'] ?? false) {
|
||||
throw new RenderingException($configuration['label'], 1529710560);
|
||||
}
|
||||
|
||||
$form = GeneralUtility::makeInstance(
|
||||
FormDefinition::class,
|
||||
$configuration['identifier'],
|
||||
$prototypeConfiguration,
|
||||
'Form',
|
||||
$persistenceIdentifier
|
||||
);
|
||||
// Set renderingOptions before processing renderables, so that options
|
||||
// like 'previewMode' are available during initializeFormElement().
|
||||
if (isset($configuration['renderingOptions'])) {
|
||||
foreach ($configuration['renderingOptions'] as $key => $value) {
|
||||
$form->setRenderingOption($key, $value);
|
||||
}
|
||||
}
|
||||
if (isset($configuration['renderables'])) {
|
||||
foreach ($configuration['renderables'] as $pageConfiguration) {
|
||||
$this->addNestedRenderable($pageConfiguration, $form, $request);
|
||||
}
|
||||
}
|
||||
|
||||
unset($configuration['persistenceIdentifier']);
|
||||
unset($configuration['prototypeName']);
|
||||
unset($configuration['renderables']);
|
||||
unset($configuration['type']);
|
||||
unset($configuration['identifier']);
|
||||
$form->setOptions($configuration);
|
||||
$form->setRequest($request);
|
||||
|
||||
return $this->triggerFormBuildingFinished($form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add form elements to the $parentRenderable
|
||||
*
|
||||
* @return mixed
|
||||
* @throws IdentifierNotValidException
|
||||
* @throws UnknownCompositRenderableException
|
||||
*/
|
||||
protected function addNestedRenderable(
|
||||
array $nestedRenderableConfiguration,
|
||||
CompositeRenderableInterface $parentRenderable,
|
||||
?ServerRequestInterface $request = null
|
||||
) {
|
||||
if (!isset($nestedRenderableConfiguration['identifier'])) {
|
||||
throw new IdentifierNotValidException('Identifier not set.', 1329289436);
|
||||
}
|
||||
if ($parentRenderable instanceof FormDefinition) {
|
||||
$renderable = $parentRenderable->createPage($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
|
||||
} elseif ($parentRenderable instanceof AbstractSection) {
|
||||
$renderable = $parentRenderable->createElement($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
|
||||
if ($request !== null && method_exists($renderable, 'setRequest')) {
|
||||
$renderable->setRequest($request);
|
||||
}
|
||||
} else {
|
||||
throw new UnknownCompositRenderableException('Unknown composit renderable "' . get_class($parentRenderable) . '"', 1479593622);
|
||||
}
|
||||
|
||||
$childRenderables = is_array($nestedRenderableConfiguration['renderables'] ?? null)
|
||||
? $nestedRenderableConfiguration['renderables']
|
||||
: [];
|
||||
|
||||
unset($nestedRenderableConfiguration['type']);
|
||||
unset($nestedRenderableConfiguration['identifier']);
|
||||
unset($nestedRenderableConfiguration['renderables']);
|
||||
|
||||
$renderable->setOptions($nestedRenderableConfiguration);
|
||||
|
||||
if ($renderable instanceof CompositeRenderableInterface) {
|
||||
foreach ($childRenderables as $elementConfiguration) {
|
||||
$this->addNestedRenderable($elementConfiguration, $renderable, $request);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->eventDispatcher->dispatch(new BeforeRenderableIsAddedToFormEvent($renderable))->renderable;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Factory;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
|
||||
|
||||
/**
|
||||
* A Form Factory is responsible for building a {@link TYPO3\CMS\Form\Domain\Model\FormDefinition}.
|
||||
* **Instead of implementing this interface, subclassing {@link AbstractFormFactory} is more appropriate
|
||||
* in most cases**.
|
||||
*
|
||||
* A Form Factory can be called anytime a FormDefinition should be built; in most cases
|
||||
* it is done through an invocation of a Form Rendering ViewHelper.
|
||||
*
|
||||
* Scope: frontend / backend
|
||||
*/
|
||||
interface FormFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Build a form definition, depending on some configuration.
|
||||
*
|
||||
* The configuration array is factory-specific; for example a YAML or JSON factory
|
||||
* could retrieve the path to the YAML / JSON file via the configuration array.
|
||||
*
|
||||
* @param array $configuration factory-specific configuration array
|
||||
* @param string $prototypeName The name of the "PrototypeName" to use; it is factory-specific to implement this.
|
||||
* @param ServerRequestInterface $request The PSR-7 request object
|
||||
* @return FormDefinition a newly built form definition
|
||||
*/
|
||||
public function build(
|
||||
array $configuration,
|
||||
?string $prototypeName = null,
|
||||
?ServerRequestInterface $request = null
|
||||
): FormDefinition;
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
|
||||
use TYPO3\CMS\Core\View\ViewFactoryData;
|
||||
use TYPO3\CMS\Core\View\ViewFactoryInterface;
|
||||
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\StringableFormElementInterface;
|
||||
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
|
||||
use TYPO3\CMS\Form\Service\TranslationService;
|
||||
|
||||
/**
|
||||
* Finisher base class.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is meant to be sub classed by developers**
|
||||
*/
|
||||
abstract class AbstractFinisher implements FinisherInterface, LoggerAwareInterface
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $finisherIdentifier = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $shortFinisherIdentifier = '';
|
||||
|
||||
/**
|
||||
* The options which have been set from the outside. Instead of directly
|
||||
* accessing them, you should rather use parseOption().
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* These are the default options of the finisher.
|
||||
* Override them in your concrete implementation.
|
||||
* Default options should not be changed from "outside"
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [];
|
||||
|
||||
/**
|
||||
* @var FinisherContext
|
||||
*/
|
||||
protected $finisherContext;
|
||||
|
||||
private ViewFactoryInterface $viewFactory;
|
||||
|
||||
private TranslationService $translationService;
|
||||
|
||||
public function injectViewFactory(ViewFactoryInterface $viewFactory)
|
||||
{
|
||||
$this->viewFactory = $viewFactory;
|
||||
}
|
||||
|
||||
public function injectTranslationService(TranslationService $translationService)
|
||||
{
|
||||
$this->translationService = $translationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $finisherIdentifier The identifier for this finisher
|
||||
*/
|
||||
public function setFinisherIdentifier(string $finisherIdentifier): void
|
||||
{
|
||||
$this->finisherIdentifier = $finisherIdentifier;
|
||||
$this->shortFinisherIdentifier = preg_replace('/Finisher$/', '', $finisherIdentifier) ?? '';
|
||||
}
|
||||
|
||||
public function getFinisherIdentifier(): string
|
||||
{
|
||||
return $this->finisherIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options configuration options in the format ['option1' => 'value1', 'option2' => 'value2', ...]
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a single finisher option (@see setOptions())
|
||||
*
|
||||
* @param string $optionName name of the option to be set
|
||||
* @param mixed $optionValue value of the option
|
||||
*/
|
||||
public function setOption(string $optionName, $optionValue)
|
||||
{
|
||||
$this->options[$optionName] = $optionValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the finisher
|
||||
*
|
||||
* @param FinisherContext $finisherContext The Finisher context that contains the current Form Runtime and Response
|
||||
* @return string|null
|
||||
*/
|
||||
final public function execute(FinisherContext $finisherContext)
|
||||
{
|
||||
$this->finisherContext = $finisherContext;
|
||||
|
||||
if (!$this->isEnabled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->executeInternal();
|
||||
} catch (FinisherException $e) {
|
||||
$this->logger->error('Failed to execute finisher', ['exception' => $e]);
|
||||
$this->finisherContext->cancel();
|
||||
$formRuntime = $this->finisherContext->getFormRuntime();
|
||||
$renderingOptions = $formRuntime->getRenderingOptions();
|
||||
$viewFactoryData = new ViewFactoryData(
|
||||
templateRootPaths: is_array($renderingOptions['templateRootPaths'] ?? null) ? $renderingOptions['templateRootPaths'] : [],
|
||||
partialRootPaths: is_array($renderingOptions['partialRootPaths'] ?? null) ? $renderingOptions['partialRootPaths'] : [],
|
||||
layoutRootPaths: is_array($renderingOptions['layoutRootPaths'] ?? null) ? $renderingOptions['layoutRootPaths'] : [],
|
||||
request: $this->finisherContext->getRequest(),
|
||||
);
|
||||
$view = $this->viewFactory->create($viewFactoryData);
|
||||
$message = $this->parseOption('errorMessage') ?: $this->translationService->translate('form.finisher.error', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
|
||||
$view->assign('message', $message);
|
||||
return $view->render('Finishers/Error');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called in the concrete finisher whenever self::execute() is called.
|
||||
*
|
||||
* Override and fill with your own implementation!
|
||||
*
|
||||
* @throws FinisherException
|
||||
* @return string|void|null
|
||||
*/
|
||||
abstract protected function executeInternal();
|
||||
|
||||
/**
|
||||
* Read the option called $optionName from $this->options, and parse {...}
|
||||
* as object accessors.
|
||||
*
|
||||
* Then translate the value.
|
||||
*
|
||||
* If $optionName was not found, the corresponding default option is returned (from $this->defaultOptions)
|
||||
*
|
||||
* @param string $optionName
|
||||
* @return string|array|int|bool|\Closure|callable|null
|
||||
*/
|
||||
protected function parseOption(string $optionName)
|
||||
{
|
||||
if ($optionName === 'translation') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$optionValue = ArrayUtility::getValueByPath($this->options, $optionName, '.');
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
$optionValue = null;
|
||||
}
|
||||
try {
|
||||
$defaultValue = ArrayUtility::getValueByPath($this->defaultOptions, $optionName, '.');
|
||||
} catch (MissingArrayPathException $exception) {
|
||||
$defaultValue = null;
|
||||
}
|
||||
|
||||
if ($optionValue === null && $defaultValue !== null) {
|
||||
$optionValue = $defaultValue;
|
||||
}
|
||||
|
||||
if ($optionValue === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!is_string($optionValue) && !is_array($optionValue)) {
|
||||
return $optionValue;
|
||||
}
|
||||
|
||||
$formRuntime = $this->finisherContext->getFormRuntime();
|
||||
$optionValue = $this->substituteRuntimeReferences($optionValue, $formRuntime);
|
||||
|
||||
if (is_string($optionValue)) {
|
||||
$translationOptions = is_array($this->options['translation'] ?? null)
|
||||
? $this->options['translation']
|
||||
: [];
|
||||
|
||||
$optionValue = $this->translateFinisherOption(
|
||||
$optionValue,
|
||||
$formRuntime,
|
||||
$optionName,
|
||||
$optionValue,
|
||||
$translationOptions
|
||||
);
|
||||
|
||||
$optionValue = $this->substituteRuntimeReferences($optionValue, $formRuntime);
|
||||
}
|
||||
|
||||
if (empty($optionValue)) {
|
||||
if ($defaultValue !== null) {
|
||||
$optionValue = $defaultValue;
|
||||
}
|
||||
}
|
||||
return $optionValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps TranslationService::translateFinisherOption to recursively
|
||||
* invoke all array items of resolved form state values or nested
|
||||
* finisher option configuration settings.
|
||||
*
|
||||
* @param string|array $subject
|
||||
* @param FormRuntime $formRuntime
|
||||
* @param string|array $optionValue
|
||||
* @return array|string
|
||||
*/
|
||||
protected function translateFinisherOption(
|
||||
$subject,
|
||||
FormRuntime $formRuntime,
|
||||
string $optionName,
|
||||
$optionValue,
|
||||
array $translationOptions
|
||||
) {
|
||||
if (is_array($subject)) {
|
||||
foreach ($subject as $key => $value) {
|
||||
$subject[$key] = $this->translateFinisherOption(
|
||||
$value,
|
||||
$formRuntime,
|
||||
$optionName . '.' . $value,
|
||||
$value,
|
||||
$translationOptions
|
||||
);
|
||||
}
|
||||
return $subject;
|
||||
}
|
||||
|
||||
return $this->translationService->translateFinisherOption(
|
||||
$formRuntime,
|
||||
$this->finisherIdentifier,
|
||||
$optionName,
|
||||
$optionValue,
|
||||
$translationOptions
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* You can encapsulate an option value with {}.
|
||||
* This enables you to access every gettable property from the
|
||||
* TYPO3\CMS\Form\Domain\Runtime\FormRuntime.
|
||||
*
|
||||
* For example: {formState.formValues.<elementIdentifier>}
|
||||
* or {<elementIdentifier>}
|
||||
*
|
||||
* Both examples are equal to "$formRuntime->getFormState()->getFormValues()[<elementIdentifier>]"
|
||||
* There is a special option value '{__currentTimestamp}'.
|
||||
* This will be replaced with the current timestamp.
|
||||
*
|
||||
* @param string|array $needle
|
||||
* @param FormRuntime $formRuntime
|
||||
* @return mixed
|
||||
*/
|
||||
protected function substituteRuntimeReferences($needle, FormRuntime $formRuntime)
|
||||
{
|
||||
// neither array nor string, directly return
|
||||
if (!is_array($needle) && !is_string($needle)) {
|
||||
return $needle;
|
||||
}
|
||||
|
||||
// resolve (recursively) all array items
|
||||
if (is_array($needle)) {
|
||||
$substitutedNeedle = [];
|
||||
foreach ($needle as $key => $item) {
|
||||
$key = $this->substituteRuntimeReferences($key, $formRuntime);
|
||||
$item = $this->substituteRuntimeReferences($item, $formRuntime);
|
||||
$substitutedNeedle[$key] = $item;
|
||||
}
|
||||
return $substitutedNeedle;
|
||||
}
|
||||
|
||||
// substitute one(!) variable in string which either could result
|
||||
// again in a string or an array representing multiple values
|
||||
if (preg_match('/^{([^}]+)}$/', $needle, $matches)) {
|
||||
return $this->resolveRuntimeReference(
|
||||
$matches[1],
|
||||
$formRuntime
|
||||
);
|
||||
}
|
||||
|
||||
// in case string contains more than just one variable or just a static
|
||||
// value that does not need to be substituted at all, candidates are:
|
||||
// * "prefix{variable}suffix
|
||||
// * "{variable-1},{variable-2}"
|
||||
// * "some static value"
|
||||
// * mixed cases of the above
|
||||
return preg_replace_callback(
|
||||
'/{([^}]+)}/',
|
||||
function ($matches) use ($formRuntime) {
|
||||
$value = $this->resolveRuntimeReference(
|
||||
$matches[1],
|
||||
$formRuntime
|
||||
);
|
||||
|
||||
// substitute each match by returning the resolved value
|
||||
if (!is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// now the resolve value is an array that shall substitute
|
||||
// a variable in a string that probably is not the only one
|
||||
// or is wrapped with other static string content (see above)
|
||||
// ... which is just not possible
|
||||
throw new FinisherException(
|
||||
'Cannot convert array to string',
|
||||
1519239265
|
||||
);
|
||||
},
|
||||
$needle
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolving property by name from submitted form data.
|
||||
*
|
||||
* @return int|string|array
|
||||
*/
|
||||
protected function resolveRuntimeReference(string $property, FormRuntime $formRuntime)
|
||||
{
|
||||
if ($property === '__currentTimestamp') {
|
||||
return time();
|
||||
}
|
||||
|
||||
// try to resolve the path '{...}' within the FormRuntime
|
||||
$value = ObjectAccess::getPropertyPath($formRuntime, $property);
|
||||
|
||||
if (is_object($value)) {
|
||||
$element = $formRuntime->getFormDefinition()->getElementByIdentifier($property);
|
||||
|
||||
if (!$element instanceof StringableFormElementInterface) {
|
||||
throw new FinisherException(
|
||||
sprintf('Cannot convert object value of "%s" to string', $property),
|
||||
1574362327
|
||||
);
|
||||
}
|
||||
|
||||
$value = $element->valueToString($value);
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
// try to resolve the path '{...}' within the FinisherVariableProvider
|
||||
$value = ObjectAccess::getPropertyPath(
|
||||
$this->finisherContext->getFinisherVariableProvider(),
|
||||
$property
|
||||
);
|
||||
}
|
||||
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// in case no value could be resolved
|
||||
return '{' . $property . '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this finisher is enabled
|
||||
*/
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return !isset($this->options['renderingOptions']['enabled']) || (bool)$this->parseOption('renderingOptions.enabled') === true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
|
||||
/**
|
||||
* A simple finisher that invokes a closure when executed
|
||||
*
|
||||
* Usage:
|
||||
* //...
|
||||
* $closureFinisher = GeneralUtility::makeInstance(ClosureFinisher::class);
|
||||
* $closureFinisher->setOption('closure', function($finisherContext) {
|
||||
* $formRuntime = $finisherContext->getFormRuntime();
|
||||
* // ...
|
||||
* });
|
||||
* $formDefinition->addFinisher($closureFinisher);
|
||||
* // ...
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class ClosureFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'closure' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function executeInternal()
|
||||
{
|
||||
$closure = $this->parseOption('closure');
|
||||
if ($closure === null) {
|
||||
return;
|
||||
}
|
||||
if (!$closure instanceof \Closure) {
|
||||
throw new FinisherException(sprintf('The option "closure" must be of type Closure, "%s" given.', gettype($closure)), 1332155239);
|
||||
}
|
||||
$closure($this->finisherContext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\View\ViewFactoryData;
|
||||
use TYPO3\CMS\Core\View\ViewFactoryInterface;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface as ExtbaseConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Fluid\View\FluidViewAdapter;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
|
||||
/**
|
||||
* A finisher that outputs a given text
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - message: A hard-coded message to be rendered
|
||||
* - contentElementUid: A content element uid to be rendered
|
||||
*
|
||||
* Usage:
|
||||
* //...
|
||||
* $confirmationFinisher = GeneralUtility::makeInstance(ConfirmationFinisher::class);
|
||||
* $confirmationFinisher->setOptions(
|
||||
* [
|
||||
* 'message' => 'foo',
|
||||
* ]
|
||||
* );
|
||||
* $formDefinition->addFinisher($confirmationFinisher);
|
||||
* // ...
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class ConfirmationFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'message' => 'The form has been submitted.',
|
||||
'contentElementUid' => 0,
|
||||
'typoscriptObjectPath' => 'lib.tx_form.contentElementRendering',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly ExtbaseConfigurationManagerInterface $extbaseConfigurationManager,
|
||||
private readonly ViewFactoryInterface $viewFactory,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function executeInternal(): string
|
||||
{
|
||||
$options = $this->options;
|
||||
if (!isset($options['templateName']) || !is_string($options['templateName'])) {
|
||||
throw new FinisherException(
|
||||
'The option "templateName" must be set for the ConfirmationFinisher.',
|
||||
1521573955
|
||||
);
|
||||
}
|
||||
|
||||
$contentElementUid = $this->parseOption('contentElementUid');
|
||||
$typoscriptObjectPath = $this->parseOption('typoscriptObjectPath');
|
||||
$typoscriptObjectPath = is_string($typoscriptObjectPath) ? $typoscriptObjectPath : '';
|
||||
if (!empty($contentElementUid)) {
|
||||
$pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
|
||||
$lastSegment = array_pop($pathSegments);
|
||||
$setup = $this->extbaseConfigurationManager->getConfiguration(ExtbaseConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
|
||||
foreach ($pathSegments as $segment) {
|
||||
if (!array_key_exists($segment . '.', $setup)) {
|
||||
throw new FinisherException(
|
||||
sprintf('TypoScript object path "%s" does not exist', $typoscriptObjectPath),
|
||||
1489238980
|
||||
);
|
||||
}
|
||||
$setup = $setup[$segment . '.'];
|
||||
}
|
||||
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
||||
$contentObjectRenderer->setRequest($this->finisherContext->getRequest()->withoutAttribute('extbase'));
|
||||
$contentObjectRenderer->start([$contentElementUid]);
|
||||
$contentObjectRenderer->setCurrentVal((string)$contentElementUid);
|
||||
$message = $contentObjectRenderer->cObjGetSingle($setup[$lastSegment], $setup[$lastSegment . '.'], $lastSegment);
|
||||
} else {
|
||||
$message = $this->parseOption('message');
|
||||
}
|
||||
|
||||
$formRuntime = $this->finisherContext->getFormRuntime();
|
||||
$viewFactoryData = new ViewFactoryData(
|
||||
templateRootPaths: is_array($options['templateRootPaths'] ?? null) ? $options['templateRootPaths'] : [],
|
||||
partialRootPaths: is_array($options['partialRootPaths'] ?? null) ? $options['partialRootPaths'] : [],
|
||||
layoutRootPaths: is_array($options['layoutRootPaths'] ?? null) ? $options['layoutRootPaths'] : [],
|
||||
request: $this->finisherContext->getRequest(),
|
||||
);
|
||||
$view = $this->viewFactory->create($viewFactoryData);
|
||||
if ($view instanceof FluidViewAdapter) {
|
||||
$view->getRenderingContext()->getViewHelperVariableContainer()
|
||||
->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);
|
||||
}
|
||||
if (is_array($this->options['variables'] ?? null)) {
|
||||
$view->assignMultiple($this->options['variables']);
|
||||
}
|
||||
$view->assignMultiple([
|
||||
'form' => $formRuntime,
|
||||
'finisherVariableProvider' => $this->finisherContext->getFinisherVariableProvider(),
|
||||
'message' => $message,
|
||||
'isPreparedMessage' => !empty($contentElementUid),
|
||||
]);
|
||||
return $view->render($options['templateName']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Core\Resource\FileReference;
|
||||
use TYPO3\CMS\Core\Resource\Folder;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference as ExtbaseFileReference;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
|
||||
|
||||
/**
|
||||
* This finisher remove the submitted files.
|
||||
* Use this e.g after the email finisher if you don't want
|
||||
* to keep the files online.
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class DeleteUploadsFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*/
|
||||
protected function executeInternal()
|
||||
{
|
||||
$formRuntime = $this->finisherContext->getFormRuntime();
|
||||
|
||||
$uploadFolders = [];
|
||||
$elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();
|
||||
foreach ($elements as $element) {
|
||||
if (!$element instanceof FileUpload) {
|
||||
continue;
|
||||
}
|
||||
$file = $formRuntime[$element->getIdentifier()];
|
||||
if (!$file) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($file instanceof ExtbaseFileReference) {
|
||||
$file = $file->getOriginalResource();
|
||||
}
|
||||
if ($file instanceof FileReference) {
|
||||
$this->deleteFileAndCollectFolder($file, $uploadFolders);
|
||||
} elseif ($file instanceof ObjectStorage) {
|
||||
foreach ($file as $singleFile) {
|
||||
if ($singleFile instanceof ExtbaseFileReference) {
|
||||
$singleFile = $singleFile->getOriginalResource();
|
||||
}
|
||||
if ($singleFile instanceof FileReference) {
|
||||
$this->deleteFileAndCollectFolder($singleFile, $uploadFolders);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->deleteEmptyUploadFolders($uploadFolders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the file and collects its parent folder for later cleanup.
|
||||
*
|
||||
* @param array<string, Folder> $uploadFolders
|
||||
*/
|
||||
private function deleteFileAndCollectFolder(FileReference $file, array &$uploadFolders): void
|
||||
{
|
||||
$folder = $file->getParentFolder();
|
||||
if ($folder instanceof Folder) {
|
||||
$uploadFolders[$folder->getCombinedIdentifier()] = $folder;
|
||||
}
|
||||
$file->getStorage()->deleteFile($file->getOriginalFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* note:
|
||||
* TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter::importUploadedResource()
|
||||
* creates a sub-folder for file uploads (e.g. .../form_<40-chars-hash>/actual.file)
|
||||
* @param Folder[] $folders
|
||||
*/
|
||||
protected function deleteEmptyUploadFolders(array $folders): void
|
||||
{
|
||||
foreach ($folders as $folder) {
|
||||
if ($this->isEmptyFolder($folder)) {
|
||||
$folder->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function isEmptyFolder(Folder $folder): bool
|
||||
{
|
||||
return $folder->getFileCount() === 0
|
||||
&& $folder->getStorage()->countFoldersInFolder($folder) === 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use TYPO3\CMS\Core\Mail\FluidEmail;
|
||||
use TYPO3\CMS\Core\Mail\MailerInterface;
|
||||
use TYPO3\CMS\Core\Mail\TemplatedEmailFactory;
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\MathUtility;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
|
||||
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
|
||||
use TYPO3\CMS\Form\Event\BeforeEmailFinisherInitializedEvent;
|
||||
use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
|
||||
|
||||
/**
|
||||
* This finisher sends an email to one recipient
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - templateName (mandatory): Template name for the mail body
|
||||
* - templateRootPaths: root paths for the templates
|
||||
* - layoutRootPaths: root paths for the layouts
|
||||
* - partialRootPaths: root paths for the partials
|
||||
* - variables: associative array of variables which are available inside the Fluid template
|
||||
*
|
||||
* The following options control the mail sending. In all of them, placeholders in the form
|
||||
* of {...} are replaced with the corresponding form value; i.e. {email} as senderAddress
|
||||
* makes the recipient address configurable.
|
||||
*
|
||||
* - subject (mandatory): Subject of the email
|
||||
* - recipients (mandatory): Email addresses and human-readable names of the recipients
|
||||
* - senderAddress (mandatory): Email address of the sender
|
||||
* - senderName: Human-readable name of the sender
|
||||
* - replyToRecipients: Email addresses and human-readable names of the reply-to recipients
|
||||
* - carbonCopyRecipients: Email addresses and human-readable names of the copy recipients
|
||||
* - blindCarbonCopyRecipients: Email addresses and human-readable names of the blind copy recipients
|
||||
* - title: The title of the email - If not set "subject" is used by default
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class EmailFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'recipientName' => '',
|
||||
'senderName' => '',
|
||||
'addHtmlPart' => true,
|
||||
'attachUploads' => true,
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
protected readonly EventDispatcherInterface $eventDispatcher,
|
||||
protected readonly TemplatedEmailFactory $templatedEmailFactory,
|
||||
protected readonly MailerInterface $mailer,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function executeInternal(): void
|
||||
{
|
||||
$this->options = $this->eventDispatcher
|
||||
->dispatch(new BeforeEmailFinisherInitializedEvent($this->finisherContext, $this->options))
|
||||
->getOptions();
|
||||
// Flexform overrides write strings instead of integers so
|
||||
// we need to cast the string '0' to false.
|
||||
if (
|
||||
isset($this->options['addHtmlPart'])
|
||||
&& $this->options['addHtmlPart'] === '0'
|
||||
) {
|
||||
$this->options['addHtmlPart'] = false;
|
||||
}
|
||||
|
||||
$subject = (string)$this->parseOption('subject');
|
||||
$recipients = $this->getRecipients('recipients');
|
||||
$senderAddress = $this->parseOption('senderAddress');
|
||||
$senderAddress = is_string($senderAddress) ? $senderAddress : '';
|
||||
$senderName = $this->parseOption('senderName');
|
||||
$senderName = is_string($senderName) ? $senderName : '';
|
||||
$replyToRecipients = $this->getRecipients('replyToRecipients');
|
||||
$carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients');
|
||||
$blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients');
|
||||
$addHtmlPart = (bool)$this->parseOption('addHtmlPart');
|
||||
$attachUploads = $this->parseOption('attachUploads');
|
||||
$title = (string)$this->parseOption('title') ?: $subject;
|
||||
|
||||
if ($subject === '') {
|
||||
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
|
||||
}
|
||||
if (empty($recipients)) {
|
||||
throw new FinisherException('The option "recipients" must be set for the EmailFinisher.', 1327060200);
|
||||
}
|
||||
if (empty($senderAddress)) {
|
||||
throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
|
||||
}
|
||||
|
||||
$formRuntime = $this->finisherContext->getFormRuntime();
|
||||
|
||||
$mail = $this
|
||||
->initializeFluidEmail($formRuntime)
|
||||
->from(new Address($senderAddress, $senderName))
|
||||
->to(...$recipients)
|
||||
->subject($subject)
|
||||
->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN)
|
||||
->assign('title', $title);
|
||||
|
||||
if (!empty($replyToRecipients)) {
|
||||
$mail->replyTo(...$replyToRecipients);
|
||||
}
|
||||
|
||||
if (!empty($carbonCopyRecipients)) {
|
||||
$mail->cc(...$carbonCopyRecipients);
|
||||
}
|
||||
|
||||
if (!empty($blindCarbonCopyRecipients)) {
|
||||
$mail->bcc(...$blindCarbonCopyRecipients);
|
||||
}
|
||||
|
||||
if (is_string($this->options['translation']['language'] ?? null) && $this->options['translation']['language'] !== '') {
|
||||
$mail->assign('languageKey', $this->options['translation']['language']);
|
||||
}
|
||||
|
||||
$message = $this->parseOption('message');
|
||||
if (is_string($message) && $message !== '') {
|
||||
// Remove whitespace between HTML tags to prevent lib.parseFunc_RTE
|
||||
// from converting newlines into additional blank lines in the email output
|
||||
$message = preg_replace('/>\s+</', '><', $message);
|
||||
$placeholderPos = strpos($message, '{formValues}');
|
||||
if ($placeholderPos !== false) {
|
||||
$mail->assign('messageBefore', substr($message, 0, $placeholderPos));
|
||||
$mail->assign('messageAfter', substr($message, $placeholderPos + strlen('{formValues}')));
|
||||
} else {
|
||||
// No placeholder - show message only, no form values
|
||||
$mail->assign('messageBefore', $message);
|
||||
$mail->assign('messageAfter', '');
|
||||
$mail->assign('hideFormValues', true);
|
||||
}
|
||||
}
|
||||
|
||||
if ($attachUploads) {
|
||||
foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) {
|
||||
if (!$element instanceof FileUpload) {
|
||||
continue;
|
||||
}
|
||||
$file = $formRuntime[$element->getIdentifier()];
|
||||
if ($file instanceof FileReference) {
|
||||
$file = $file->getOriginalResource();
|
||||
}
|
||||
if ($file instanceof FileInterface) {
|
||||
$mail->attach($file->getContents(), $file->getName(), $file->getMimeType());
|
||||
} elseif ($file instanceof ObjectStorage) {
|
||||
foreach ($file as $singleFile) {
|
||||
if ($singleFile instanceof FileReference) {
|
||||
$singleFile = $singleFile->getOriginalResource();
|
||||
}
|
||||
if ($singleFile instanceof FileInterface) {
|
||||
$mail->attach($singleFile->getContents(), $singleFile->getName(), $singleFile->getMimeType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$this->mailer->send($mail);
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
throw new FinisherException(
|
||||
'Failed to send the email: ' . $e->getMessage(),
|
||||
1754047320,
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
|
||||
{
|
||||
$mailMessage = $this->templatedEmailFactory->createWithOverrides(
|
||||
$this->options['templateRootPaths'] ?? [],
|
||||
$this->options['layoutRootPaths'] ?? [],
|
||||
$this->options['partialRootPaths'] ?? [],
|
||||
$this->finisherContext->getRequest(),
|
||||
);
|
||||
|
||||
if (!isset($this->options['templateName']) || $this->options['templateName'] === '') {
|
||||
throw new FinisherException('The option "templateName" must be set to use FluidEmail.', 1599834020);
|
||||
}
|
||||
|
||||
// Migrate old template name to default FluidEmail name
|
||||
if ($this->options['templateName'] === '{@format}.html') {
|
||||
$this->options['templateName'] = 'Default';
|
||||
}
|
||||
|
||||
$mailMessage
|
||||
->setTemplate($this->options['templateName'])
|
||||
->assignMultiple([
|
||||
'finisherVariableProvider' => $this->finisherContext->getFinisherVariableProvider(),
|
||||
'form' => $formRuntime,
|
||||
]);
|
||||
|
||||
if (is_array($this->options['variables'] ?? null)) {
|
||||
$mailMessage->assignMultiple($this->options['variables']);
|
||||
}
|
||||
|
||||
$mailMessage
|
||||
->getViewHelperVariableContainer()
|
||||
->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime);
|
||||
|
||||
return $mailMessage;
|
||||
}
|
||||
|
||||
protected function getRecipients(string $listOption): array
|
||||
{
|
||||
$recipients = $this->parseOption($listOption) ?? [];
|
||||
if (!is_array($recipients) || $recipients === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$addresses = [];
|
||||
foreach ($recipients as $address => $name) {
|
||||
// The if is needed to set address and name with TypoScript
|
||||
if (MathUtility::canBeInterpretedAsInteger($address)) {
|
||||
if (is_array($name)) {
|
||||
$address = $name[0] ?? '';
|
||||
$name = $name[1] ?? '';
|
||||
} else {
|
||||
$address = $name;
|
||||
$name = '';
|
||||
}
|
||||
}
|
||||
|
||||
$address = trim((string)$address);
|
||||
|
||||
if (!GeneralUtility::validEmail($address)) {
|
||||
// Drop entries without a valid address
|
||||
continue;
|
||||
}
|
||||
$addresses[] = new Address($address, $name);
|
||||
}
|
||||
return $addresses;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown in Form Finishers
|
||||
*/
|
||||
class FinisherException extends Exception {}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Extbase\Mvc\Request;
|
||||
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
|
||||
|
||||
/**
|
||||
* The context that is passed to each finisher when executed.
|
||||
* It acts like an EventObject that is able to stop propagation.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* @internal
|
||||
*/
|
||||
class FinisherContext
|
||||
{
|
||||
/**
|
||||
* If TRUE further finishers won't be invoked
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $cancelled = false;
|
||||
|
||||
/**
|
||||
* A reference to the Form Runtime the finisher belongs to
|
||||
*/
|
||||
protected FormRuntime $formRuntime;
|
||||
|
||||
/**
|
||||
* The assigned controller context which might be needed by the finisher.
|
||||
*/
|
||||
protected FinisherVariableProvider $finisherVariableProvider;
|
||||
|
||||
private Request $request;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __construct(FormRuntime $formRuntime, Request $request)
|
||||
{
|
||||
$this->formRuntime = $formRuntime;
|
||||
$this->request = $request;
|
||||
$this->finisherVariableProvider = new FinisherVariableProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the finisher invocation after the current finisher
|
||||
*/
|
||||
public function cancel()
|
||||
{
|
||||
$this->cancelled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* TRUE if no further finishers should be invoked. Defaults to FALSE
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function isCancelled(): bool
|
||||
{
|
||||
return $this->cancelled;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Form Runtime that is associated with the current finisher
|
||||
*/
|
||||
public function getFormRuntime(): FormRuntime
|
||||
{
|
||||
return $this->formRuntime;
|
||||
}
|
||||
|
||||
/**
|
||||
* The values of the submitted form (after validation and property mapping)
|
||||
*/
|
||||
public function getFormValues(): array
|
||||
{
|
||||
return $this->formRuntime->getFormState()->getFormValues();
|
||||
}
|
||||
|
||||
public function getFinisherVariableProvider(): FinisherVariableProvider
|
||||
{
|
||||
return $this->finisherVariableProvider;
|
||||
}
|
||||
|
||||
public function getRequest(): Request
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
/**
|
||||
* Finisher that can be attached to a form in order to be invoked
|
||||
* as soon as the complete form is submitted
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
interface FinisherInterface
|
||||
{
|
||||
/**
|
||||
* Executes the finisher
|
||||
*
|
||||
* @param FinisherContext $finisherContext The Finisher context that contains the current Form Runtime and Response
|
||||
* @return string|null
|
||||
*/
|
||||
public function execute(FinisherContext $finisherContext);
|
||||
|
||||
public function setFinisherIdentifier(string $finisherIdentifier): void;
|
||||
|
||||
/**
|
||||
* @param array $options configuration options in the format ['option1' => 'value1', 'option2' => 'value2', ...]
|
||||
*/
|
||||
public function setOptions(array $options);
|
||||
|
||||
/**
|
||||
* Sets a single finisher option (@see setOptions())
|
||||
*
|
||||
* @param string $optionName name of the option to be set
|
||||
* @param mixed $optionValue value of the option
|
||||
*/
|
||||
public function setOption(string $optionName, $optionValue);
|
||||
|
||||
/**
|
||||
* Returns whether this finisher is enabled
|
||||
*/
|
||||
public function isEnabled(): bool;
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
|
||||
|
||||
/**
|
||||
* Store data for usage between the finishers.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* @internal
|
||||
*/
|
||||
final class FinisherVariableProvider implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* Two-dimensional object array storing the values. The first dimension is the finisher identifier,
|
||||
* and the second dimension is the identifier for the data the finisher wants to store.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $objects = [];
|
||||
|
||||
/**
|
||||
* Add a variable to the finisher container.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function add(string $finisherIdentifier, string $key, $value)
|
||||
{
|
||||
$this->addOrUpdate($finisherIdentifier, $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a variable to the Variable Container.
|
||||
* In case the value is already inside, it is silently overridden.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function addOrUpdate(string $finisherIdentifier, string $key, $value)
|
||||
{
|
||||
if (!array_key_exists($finisherIdentifier, $this->objects)) {
|
||||
$this->objects[$finisherIdentifier] = [];
|
||||
}
|
||||
$this->objects[$finisherIdentifier] = ArrayUtility::setValueByPath(
|
||||
$this->objects[$finisherIdentifier],
|
||||
$key,
|
||||
$value,
|
||||
'.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a variable which is stored
|
||||
*
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $finisherIdentifier, string $key, $default = null)
|
||||
{
|
||||
if ($this->exists($finisherIdentifier, $key)) {
|
||||
return ArrayUtility::getValueByPath($this->objects[$finisherIdentifier], $key, '.');
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether there is a variable stored for the given key
|
||||
*
|
||||
* @param string $finisherIdentifier
|
||||
* @param string $key
|
||||
*/
|
||||
public function exists($finisherIdentifier, $key): bool
|
||||
{
|
||||
try {
|
||||
ArrayUtility::getValueByPath($this->objects[$finisherIdentifier] ?? [], $key, '.');
|
||||
} catch (MissingArrayPathException $e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a value from the variable container
|
||||
*/
|
||||
public function remove(string $finisherIdentifier, string $key)
|
||||
{
|
||||
if ($this->exists($finisherIdentifier, $key)) {
|
||||
$this->objects[$finisherIdentifier] = ArrayUtility::removeByPath(
|
||||
$this->objects[$finisherIdentifier],
|
||||
$key,
|
||||
'.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up for serializing.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
return ['objects'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an offset exists
|
||||
*
|
||||
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
|
||||
* @param mixed $offset An offset to check for.
|
||||
* @return bool TRUE on success or FALSE on failure.
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return isset($this->objects[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to retrieve
|
||||
*
|
||||
* @link https://php.net/manual/en/arrayaccess.offsetget.php
|
||||
* @param mixed $offset The offset to retrieve.
|
||||
* @return mixed Can return all value types.
|
||||
*/
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
return $this->objects[$offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to set
|
||||
*
|
||||
* @link https://php.net/manual/en/arrayaccess.offsetset.php
|
||||
* @param mixed $offset The offset to assign the value to.
|
||||
* @param mixed $value The value to set.
|
||||
*/
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->objects[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset to unset
|
||||
*
|
||||
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
|
||||
* @param mixed $offset The offset to unset.
|
||||
*/
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
unset($this->objects[$offset]);
|
||||
}
|
||||
|
||||
public function getIterator(): \Traversable
|
||||
{
|
||||
foreach ($this->objects as $offset => $value) {
|
||||
yield $offset => $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count elements of an object
|
||||
*
|
||||
* @link https://php.net/manual/en/countable.count.php
|
||||
* @return int The custom count as an integer.
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->objects);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessageService;
|
||||
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\MathUtility;
|
||||
use TYPO3\CMS\Extbase\Error\Error;
|
||||
use TYPO3\CMS\Extbase\Error\Message;
|
||||
use TYPO3\CMS\Extbase\Error\Notice;
|
||||
use TYPO3\CMS\Extbase\Error\Warning;
|
||||
use TYPO3\CMS\Extbase\Service\ExtensionService;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
|
||||
/**
|
||||
* A simple finisher that adds a message to the FlashMessageContainer
|
||||
*
|
||||
* Usage:
|
||||
* //...
|
||||
* $flashMessageFinisher = GeneralUtility::makeInstance(FlashMessageFinisher::class);
|
||||
* $flashMessageFinisher->setOptions(
|
||||
* [
|
||||
* 'messageBody' => 'Some message body',
|
||||
* 'messageTitle' => 'Some message title',
|
||||
* 'messageArguments' => ['foo' => 'bar'],
|
||||
* 'severity' => \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR
|
||||
* ]
|
||||
* );
|
||||
* $formDefinition->addFinisher($flashMessageFinisher);
|
||||
* // ...
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class FlashMessageFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'messageBody' => null,
|
||||
'messageTitle' => '',
|
||||
'messageArguments' => [],
|
||||
'messageCode' => null,
|
||||
'severity' => ContextualFeedbackSeverity::OK,
|
||||
];
|
||||
|
||||
private ExtensionService $extensionService;
|
||||
private FlashMessageService $flashMessageService;
|
||||
|
||||
public function injectFlashMessageService(FlashMessageService $flashMessageService): void
|
||||
{
|
||||
$this->flashMessageService = $flashMessageService;
|
||||
}
|
||||
|
||||
public function injectExtensionService(ExtensionService $extensionService): void
|
||||
{
|
||||
$this->extensionService = $extensionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function executeInternal()
|
||||
{
|
||||
$messageBody = $this->parseOption('messageBody');
|
||||
if (!is_string($messageBody)) {
|
||||
throw new FinisherException(sprintf('The message body must be of type string, "%s" given.', gettype($messageBody)), 1335980069);
|
||||
}
|
||||
$messageTitle = $this->parseOption('messageTitle');
|
||||
$messageArguments = $this->parseOption('messageArguments');
|
||||
$messageCode = $this->parseOption('messageCode');
|
||||
$severity = $this->parseOption('severity');
|
||||
|
||||
if (MathUtility::canBeInterpretedAsInteger($severity)) {
|
||||
$severity = ContextualFeedbackSeverity::tryFrom((int)$severity);
|
||||
}
|
||||
if (!$severity instanceof ContextualFeedbackSeverity) {
|
||||
$severity = $this->defaultOptions['severity'];
|
||||
}
|
||||
|
||||
$messageClass = match ($severity) {
|
||||
ContextualFeedbackSeverity::NOTICE => Notice::class,
|
||||
ContextualFeedbackSeverity::WARNING => Warning::class,
|
||||
ContextualFeedbackSeverity::ERROR => Error::class,
|
||||
default => Message::class,
|
||||
};
|
||||
/** @var Message|Notice|Warning|Error $message */
|
||||
$message = GeneralUtility::makeInstance($messageClass, $messageBody, $messageCode, $messageArguments, $messageTitle);
|
||||
$flashMessage = new FlashMessage(
|
||||
$message->render(),
|
||||
$message->getTitle(),
|
||||
$severity,
|
||||
true
|
||||
);
|
||||
|
||||
// todo: this value has to be taken from the request directly in the future
|
||||
$pluginNamespace = $this->extensionService->getPluginNamespace(
|
||||
$this->finisherContext->getRequest()->getControllerExtensionName(),
|
||||
$this->finisherContext->getRequest()->getPluginName()
|
||||
);
|
||||
|
||||
$this->flashMessageService->getMessageQueueByIdentifier('extbase.flashmessages.' . $pluginNamespace)->addMessage($flashMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use TYPO3\CMS\Core\Http\PropagateResponseException;
|
||||
use TYPO3\CMS\Core\Http\RedirectResponse;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* This finisher redirects to another Controller.
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class RedirectFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'pageUid' => 1,
|
||||
'additionalParameters' => '',
|
||||
'statusCode' => 303,
|
||||
'fragment' => '',
|
||||
];
|
||||
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*/
|
||||
protected function executeInternal(): void
|
||||
{
|
||||
$pageUid = $this->parseOption('pageUid');
|
||||
$pageUid = (int)str_replace('pages_', '', (string)$pageUid);
|
||||
$additionalParameters = $this->parseOption('additionalParameters');
|
||||
$additionalParameters = is_string($additionalParameters) ? $additionalParameters : '';
|
||||
$additionalParameters = '&' . ltrim($additionalParameters, '&');
|
||||
$statusCode = (int)$this->parseOption('statusCode');
|
||||
$fragment = (string)$this->parseOption('fragment');
|
||||
|
||||
$this->finisherContext->cancel();
|
||||
$this->redirect($pageUid, $additionalParameters, $fragment, $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects the request to another page.
|
||||
*
|
||||
* Redirect will be sent to the client which then performs another request to the new URI.
|
||||
*
|
||||
* NOTE: This method only supports web requests and will thrown an exception
|
||||
* if used with other request types.
|
||||
*
|
||||
* @param int $pageUid Target page uid. If NULL, the current page uid is used
|
||||
* @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
|
||||
* @see forward()
|
||||
*/
|
||||
protected function redirect(int $pageUid, string $additionalParameters, string $fragment, int $statusCode): never
|
||||
{
|
||||
$redirectUri = $this->finisherContext->getRequest()->getAttribute('currentContentObject')->createUrl([
|
||||
'parameter' => $pageUid,
|
||||
'additionalParams' => $additionalParameters,
|
||||
'section' => $fragment,
|
||||
]);
|
||||
$this->redirectToUri($redirectUri, $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects the web request to another uri.
|
||||
*
|
||||
* NOTE: This method only supports web requests and will throw an exception if used with other request types.
|
||||
*
|
||||
* @param string $uri A string representation of a URI
|
||||
* @param int $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other
|
||||
* @throws PropagateResponseException
|
||||
*/
|
||||
protected function redirectToUri(string $uri, int $statusCode = 303): never
|
||||
{
|
||||
$uri = $this->addBaseUriIfNecessary($uri);
|
||||
$response = new RedirectResponse($uri, $statusCode);
|
||||
// End processing and dispatching by throwing a PropagateResponseException with our response.
|
||||
// @todo: Should be changed to *return* a response instead, but this requires the ContentObjectRender
|
||||
// @todo: to deal with responses instead of strings, if the form is used in a fluid template rendered by the
|
||||
// @todo: FluidTemplateContentObject and the extbase bootstrap isn't used.
|
||||
throw new PropagateResponseException($response, 1477070964);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the base uri if not already in place.
|
||||
*
|
||||
* @param string $uri The URI
|
||||
*/
|
||||
protected function addBaseUriIfNecessary(string $uri): string
|
||||
{
|
||||
return GeneralUtility::locationHeaderUrl($uri, $this->finisherContext->getRequest());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Finishers;
|
||||
|
||||
use Doctrine\DBAL\Exception;
|
||||
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
|
||||
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
|
||||
|
||||
/**
|
||||
* This finisher saves the data from a submitted form into
|
||||
* a database table.
|
||||
*
|
||||
* Configuration
|
||||
* =============
|
||||
*
|
||||
* options.table (mandatory)
|
||||
* -------------
|
||||
* Save or update values into this table
|
||||
*
|
||||
* options.mode (default: insert)
|
||||
* ------------
|
||||
* Possible values are 'insert' or 'update'.
|
||||
*
|
||||
* insert: will create a new database row with the values from the
|
||||
* submitted form and/or some predefined values.
|
||||
* See options.elements and options.databaseFieldMappings
|
||||
* update: will update a given database row with the values from the
|
||||
* submitted form and/or some predefined values.
|
||||
* 'options.whereClause' is then required.
|
||||
*
|
||||
* options.whereClause
|
||||
* -------------------
|
||||
* This where clause will be used for a database update action
|
||||
*
|
||||
* options.elements
|
||||
* ----------------
|
||||
* Use this to map form element values to existing database columns.
|
||||
* Each key within options.elements has to match with a
|
||||
* form element identifier within your form definition.
|
||||
* The value for each key within options.elements is an array with
|
||||
* additional information.
|
||||
*
|
||||
* options.elements.<elementIdentifier>.mapOnDatabaseColumn (mandatory)
|
||||
* --------------------------------------------------------
|
||||
* The value from the submitted form element with the identifier
|
||||
* '<elementIdentifier>' will be written into this database column
|
||||
*
|
||||
* options.elements.<elementIdentifier>.skipIfValueIsEmpty (default: false)
|
||||
* ------------------------------------------------------
|
||||
* Set this to true if the database column should not be written
|
||||
* if the value from the submitted form element with the identifier
|
||||
* '<elementIdentifier>' is empty (think about password fields etc.)
|
||||
*
|
||||
* options.elements.<elementIdentifier>.hashed (default: false)
|
||||
* ------------------------------------------------------
|
||||
* Set this to true if the value from the submitted form element
|
||||
* should be hashed before writing into the database.
|
||||
*
|
||||
* options.elements.<elementIdentifier>.saveFileIdentifierInsteadOfUid (default: false)
|
||||
* -------------------------------------------------------------------
|
||||
* This setting only rules for form elements which creates a FAL object
|
||||
* like FileUpload or ImageUpload.
|
||||
* By default, the uid of the FAL object will be written into
|
||||
* the database column. Set this to true if you want to store the
|
||||
* FAL identifier (1:/user_uploads/some_uploaded_pic.jpg) instead.
|
||||
*
|
||||
* options.databaseColumnMappings
|
||||
* ------------------------------
|
||||
* Use this to map database columns to static values (which can be
|
||||
* made dynamic through typoscript overrides of course).
|
||||
* Each key within options.databaseColumnMappings has to match with a
|
||||
* existing database column.
|
||||
* The value for each key within options.databaseColumnMappings is an
|
||||
* array with additional information.
|
||||
*
|
||||
* This mapping is done *before* the options.elements mapping.
|
||||
* This means if you map a database column to a value through
|
||||
* options.databaseColumnMappings and map a submitted form element
|
||||
* value to the same database column, the submitted form element value
|
||||
* will override the value you set within options.databaseColumnMappings.
|
||||
*
|
||||
* options.databaseColumnMappings.<databaseColumnName>.value
|
||||
* ---------------------------------------------------------
|
||||
* The value which will be written to the database column.
|
||||
* You can use the FormRuntime accessor feature to access every
|
||||
* getable property from the TYPO3\CMS\Form\Domain\Runtime\FormRuntime
|
||||
* Read the description within
|
||||
* TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher::parseOption
|
||||
* In short: use something like {<elementIdentifier>} to get the value
|
||||
* from the submitted form element with the identifier
|
||||
* <elementIdentifier>
|
||||
*
|
||||
* Don't be confused. If you use the FormRuntime accessor feature within
|
||||
* options.databaseColumnMappings, the functionality is nearly equal
|
||||
* to the options.elements configuration.
|
||||
*
|
||||
* options.databaseColumnMappings.<databaseColumnName>.skipIfValueIsEmpty (default: false)
|
||||
* ---------------------------------------------------------------------
|
||||
* Set this to true if the database column should not be written
|
||||
* if the value from
|
||||
* options.databaseColumnMappings.<databaseColumnName>.value is empty.
|
||||
*
|
||||
* Example
|
||||
* =======
|
||||
*
|
||||
* finishers:
|
||||
* -
|
||||
* identifier: SaveToDatabase
|
||||
* options:
|
||||
* table: 'fe_users'
|
||||
* mode: update
|
||||
* whereClause:
|
||||
* uid: 1
|
||||
* databaseColumnMappings:
|
||||
* pid:
|
||||
* value: 1
|
||||
* elements:
|
||||
* text-1:
|
||||
* mapOnDatabaseColumn: 'first_name'
|
||||
* text-2:
|
||||
* mapOnDatabaseColumn: 'last_name'
|
||||
* text-3:
|
||||
* mapOnDatabaseColumn: 'username'
|
||||
* advancedpassword-1:
|
||||
* mapOnDatabaseColumn: 'password'
|
||||
* skipIfValueIsEmpty: true
|
||||
* hashed: true
|
||||
*
|
||||
* Multiple database operations
|
||||
* ============================
|
||||
*
|
||||
* You can write options as an array to perform multiple database operations.
|
||||
*
|
||||
* finishers:
|
||||
* -
|
||||
* identifier: SaveToDatabase
|
||||
* options:
|
||||
* 1:
|
||||
* table: 'my_table'
|
||||
* mode: insert
|
||||
* databaseColumnMappings:
|
||||
* some_column:
|
||||
* value: 'cool'
|
||||
* 2:
|
||||
* table: 'my_other_table'
|
||||
* mode: update
|
||||
* whereClause:
|
||||
* pid: 1
|
||||
* databaseColumnMappings:
|
||||
* some_other_column:
|
||||
* value: '{SaveToDatabase.insertedUids.1}'
|
||||
*
|
||||
* This would perform 2 database operations.
|
||||
* One insert and one update.
|
||||
* You can access the inserted uids with '{SaveToDatabase.insertedUids.<theArrayKeyNumberWithinOptions>}'
|
||||
* If you perform an insert operation, the value of the inserted database row will be stored
|
||||
* within the FinisherVariableProvider.
|
||||
* <theArrayKeyNumberWithinOptions> references to the numeric key within options
|
||||
* within which the insert operation is executed.
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class SaveToDatabaseFinisher extends AbstractFinisher
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultOptions = [
|
||||
'table' => null,
|
||||
'mode' => 'insert',
|
||||
'whereClause' => [],
|
||||
'elements' => [],
|
||||
'databaseColumnMappings' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \TYPO3\CMS\Core\Database\Connection
|
||||
*/
|
||||
protected $databaseConnection;
|
||||
|
||||
/**
|
||||
* Executes this finisher
|
||||
* @see AbstractFinisher::execute()
|
||||
*
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function executeInternal(): void
|
||||
{
|
||||
$options = [];
|
||||
if (isset($this->options['table'])) {
|
||||
$options[] = $this->options;
|
||||
} else {
|
||||
$options = $this->options;
|
||||
}
|
||||
|
||||
foreach ($options as $optionKey => $option) {
|
||||
$this->options = $option;
|
||||
$this->process($optionKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare data for saving to database
|
||||
*/
|
||||
protected function prepareData(array $elementsConfiguration, array $databaseData): array
|
||||
{
|
||||
foreach ($this->getFormValues() as $elementIdentifier => $elementValue) {
|
||||
if (
|
||||
($elementValue === null || $elementValue === '')
|
||||
&& isset($elementsConfiguration[$elementIdentifier])
|
||||
&& isset($elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'])
|
||||
&& $elementsConfiguration[$elementIdentifier]['skipIfValueIsEmpty'] === true
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$element = $this->getElementByIdentifier($elementIdentifier);
|
||||
if (
|
||||
!$element
|
||||
|| !isset($elementsConfiguration[$elementIdentifier])
|
||||
|| !isset($elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn'])
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'])) {
|
||||
$saveFileIdentifierInsteadOfUid = (bool)$elementsConfiguration[$elementIdentifier]['saveFileIdentifierInsteadOfUid'];
|
||||
} else {
|
||||
$saveFileIdentifierInsteadOfUid = false;
|
||||
}
|
||||
|
||||
if ($elementValue instanceof FileReference) {
|
||||
$elementValue = $this->prepareFileForDatabase($elementValue, $saveFileIdentifierInsteadOfUid);
|
||||
} elseif ($elementValue instanceof ObjectStorage) {
|
||||
$fileIdentifiers = [];
|
||||
foreach ($elementValue as $singleElement) {
|
||||
if ($singleElement instanceof FileReference) {
|
||||
$fileIdentifiers[] = $this->prepareFileForDatabase($singleElement, $saveFileIdentifierInsteadOfUid);
|
||||
}
|
||||
}
|
||||
$elementValue = implode(',', $fileIdentifiers);
|
||||
} elseif (is_array($elementValue)) {
|
||||
$elementValue = implode(',', $elementValue);
|
||||
} elseif ($elementValue instanceof \DateTimeInterface) {
|
||||
$format = $elementsConfiguration[$elementIdentifier]['dateFormat'] ?? 'U';
|
||||
$elementValue = $elementValue->format($format);
|
||||
} elseif ($elementValue && ($elementsConfiguration[$elementIdentifier]['hashed'] ?? false) === true) {
|
||||
$hashInstance = GeneralUtility::makeInstance(PasswordHashFactory::class)->getDefaultHashInstance('FE');
|
||||
$elementValue = $hashInstance->getHashedPassword($elementValue);
|
||||
}
|
||||
|
||||
$databaseData[$elementsConfiguration[$elementIdentifier]['mapOnDatabaseColumn']] = $elementValue;
|
||||
}
|
||||
return $databaseData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the current database operation
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function process(int $iterationCount): void
|
||||
{
|
||||
$this->throwExceptionOnInconsistentConfiguration();
|
||||
|
||||
$table = $this->parseOption('table');
|
||||
$table = is_string($table) ? $table : '';
|
||||
$elementsConfiguration = $this->parseOption('elements');
|
||||
$elementsConfiguration = is_array($elementsConfiguration) ? $elementsConfiguration : [];
|
||||
$databaseColumnMappingsConfiguration = $this->parseOption('databaseColumnMappings');
|
||||
|
||||
$this->databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
|
||||
|
||||
$databaseData = [];
|
||||
foreach ($databaseColumnMappingsConfiguration as $databaseColumnName => $databaseColumnConfiguration) {
|
||||
$value = $this->parseOption('databaseColumnMappings.' . $databaseColumnName . '.value');
|
||||
if (
|
||||
empty($value)
|
||||
&& ($databaseColumnConfiguration['skipIfValueIsEmpty'] ?? false) === true
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$databaseData[$databaseColumnName] = $value;
|
||||
}
|
||||
|
||||
$databaseData = $this->prepareData($elementsConfiguration, $databaseData);
|
||||
|
||||
try {
|
||||
$this->saveToDatabase($databaseData, $table, $iterationCount);
|
||||
} catch (Exception $e) {
|
||||
throw new FinisherException(
|
||||
'Failed to save data to database table: ' . $table . '. Error message:' . $e->getMessage(),
|
||||
1754050114,
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save or insert the values from
|
||||
* $databaseData into the table $table
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function saveToDatabase(array $databaseData, string $table, int $iterationCount): void
|
||||
{
|
||||
if (!empty($databaseData)) {
|
||||
if ($this->parseOption('mode') === 'update') {
|
||||
$whereClause = $this->parseOption('whereClause');
|
||||
foreach ($whereClause as $columnName => $columnValue) {
|
||||
$whereClause[$columnName] = $this->parseOption('whereClause.' . $columnName);
|
||||
}
|
||||
$this->databaseConnection->update(
|
||||
$table,
|
||||
$databaseData,
|
||||
$whereClause
|
||||
);
|
||||
} else {
|
||||
$this->databaseConnection->insert($table, $databaseData);
|
||||
try {
|
||||
$insertedUid = (int)$this->databaseConnection->lastInsertId();
|
||||
} catch (Exception) {
|
||||
// Some database tables like sys_category_record_mm may not
|
||||
// have an "identity" (uid column). In this case DBAL may
|
||||
// throw an exception, which we gracefully handle here.
|
||||
$insertedUid = 0;
|
||||
}
|
||||
$this->finisherContext->getFinisherVariableProvider()->add(
|
||||
$this->shortFinisherIdentifier,
|
||||
'insertedUids.' . $iterationCount,
|
||||
$insertedUid
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if some inconsistent configuration
|
||||
* are detected.
|
||||
*
|
||||
* @throws FinisherException
|
||||
*/
|
||||
protected function throwExceptionOnInconsistentConfiguration(): void
|
||||
{
|
||||
if (
|
||||
$this->parseOption('mode') === 'update'
|
||||
&& empty($this->parseOption('whereClause'))
|
||||
) {
|
||||
throw new FinisherException(
|
||||
'An empty option "whereClause" is not allowed in update mode.',
|
||||
1480469086
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values of the submitted form
|
||||
*/
|
||||
protected function getFormValues(): array
|
||||
{
|
||||
return $this->finisherContext->getFormValues();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a form element object for a given identifier.
|
||||
*
|
||||
* @return FormElementInterface|null
|
||||
*/
|
||||
protected function getElementByIdentifier(string $elementIdentifier): ?FormElementInterface
|
||||
{
|
||||
return $this
|
||||
->finisherContext
|
||||
->getFormRuntime()
|
||||
->getFormDefinition()
|
||||
->getElementByIdentifier($elementIdentifier);
|
||||
}
|
||||
|
||||
protected function prepareFileForDatabase(FileReference $fileReference, bool $saveFileIdentifierInsteadOfUid = false): int|string
|
||||
{
|
||||
if ($saveFileIdentifierInsteadOfUid) {
|
||||
$elementValue = $fileReference->getOriginalResource()->getCombinedIdentifier();
|
||||
} else {
|
||||
$elementValue = $fileReference->getOriginalResource()->getProperty('uid_local');
|
||||
}
|
||||
|
||||
return $elementValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception as DomainException;
|
||||
|
||||
/**
|
||||
* A generic Form model Exception
|
||||
*/
|
||||
class Exception extends DomainException {}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if two Form Elements with the same Identifier are added
|
||||
* to a form.
|
||||
*/
|
||||
class DuplicateFormElementException extends Exception {}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a Finisher Preset was not found,
|
||||
* or if the implementationClassName was not set.
|
||||
*/
|
||||
class FinisherPresetNotFoundException extends Exception {}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if the form definition would get an inconsistent state, like
|
||||
* adding a page to two different forms
|
||||
*/
|
||||
class FormDefinitionConsistencyException extends Exception {}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception;
|
||||
|
||||
/**
|
||||
* This exception is thrown if a Validator Preset was not found,
|
||||
* or if the implementationClassName was not set.
|
||||
*/
|
||||
class ValidatorPresetNotFoundException extends Exception {}
|
||||
@@ -0,0 +1,698 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
|
||||
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException;
|
||||
use TYPO3\CMS\Form\Domain\Finishers\FinisherInterface;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\DuplicateFormElementException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\FinisherPresetNotFoundException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\VariableRenderableInterface;
|
||||
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
|
||||
use TYPO3\CMS\Form\Exception as FormException;
|
||||
use TYPO3\CMS\Form\Mvc\ProcessingRule;
|
||||
|
||||
/**
|
||||
* This class encapsulates a complete *Form Definition*, with all of its pages,
|
||||
* form elements, validation rules which apply and finishers which should be
|
||||
* executed when the form is completely filled in.
|
||||
*
|
||||
* It is *not modified* when the form executes.
|
||||
*
|
||||
* The Anatomy Of A Form
|
||||
* =====================
|
||||
*
|
||||
* A FormDefinition consists of multiple *Page* ({@link Page}) objects. When a
|
||||
* form is displayed to the user, only one *Page* is visible at any given time,
|
||||
* and there is a navigation to go back and forth between the pages.
|
||||
*
|
||||
* A *Page* consists of multiple *FormElements* ({@link FormElementInterface}, {@link AbstractFormElement}),
|
||||
* which represent the input fields, textareas, checkboxes shown inside the page.
|
||||
*
|
||||
* *FormDefinition*, *Page* and *FormElement* have *identifier* properties, which
|
||||
* must be unique for each given type (i.e. it is allowed that the FormDefinition and
|
||||
* a FormElement have the *same* identifier, but two FormElements are not allowed to
|
||||
* have the same identifier.
|
||||
*
|
||||
* Simple Example
|
||||
* --------------
|
||||
*
|
||||
* Generally, you can create a FormDefinition manually by just calling the API
|
||||
* methods on it, or you use a *Form Definition Factory* to build the form from
|
||||
* another representation format such as YAML.
|
||||
*
|
||||
* /---code php
|
||||
* $formDefinition = GeneralUtility::makeInstance(FormDefinition::class, 'myForm');
|
||||
*
|
||||
* $page1 = GeneralUtility::makeInstance(Page::class, 'page1');
|
||||
* $formDefinition->addPage($page);
|
||||
*
|
||||
* $element1 = GeneralUtility::makeInstance(GenericFormElement::class, 'title', 'Textfield'); # the second argument is the type of the form element
|
||||
* $page1->addElement($element1);
|
||||
* \---
|
||||
*
|
||||
* Creating a Form, Using Abstract Form Element Types
|
||||
* =====================================================
|
||||
*
|
||||
* While you can use the {@link FormDefinition::addPage} or {@link Page::addElement}
|
||||
* methods and create the Page and FormElement objects manually, it is often better
|
||||
* to use the corresponding create* methods ({@link FormDefinition::createPage}
|
||||
* and {@link Page::createElement}), as you pass them an abstract *Form Element Type*
|
||||
* such as *Text* or *Page*, and the system **automatically
|
||||
* resolves the implementation class name and sets default values**.
|
||||
*
|
||||
* So the simple example from above should be rewritten as follows:
|
||||
*
|
||||
* /---code php
|
||||
* $prototypeConfiguration = []; // We'll talk about this later
|
||||
*
|
||||
* $formDefinition = GeneralUtility::makeInstance(FormDefinition::class, 'myForm', $prototypeConfiguration);
|
||||
* $page1 = $formDefinition->createPage('page1');
|
||||
* $element1 = $page1->addElement('title', 'Textfield');
|
||||
* \---
|
||||
*
|
||||
* Now, you might wonder how the system knows that the element *Textfield*
|
||||
* is implemented using a GenericFormElement: **This is configured in the $prototypeConfiguration**.
|
||||
*
|
||||
* To make the example from above actually work, we need to add some sensible
|
||||
* values to *$prototypeConfiguration*:
|
||||
*
|
||||
* <pre>
|
||||
* $prototypeConfiguration = [
|
||||
* 'formElementsDefinition' => [
|
||||
* 'Page' => [
|
||||
* 'implementationClassName' => 'TYPO3\CMS\Form\Domain\Model\FormElements\Page'
|
||||
* ],
|
||||
* 'Textfield' => [
|
||||
* 'implementationClassName' => 'TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement'
|
||||
* ]
|
||||
* ]
|
||||
* ]
|
||||
* </pre>
|
||||
*
|
||||
* For each abstract *Form Element Type* we add some configuration; in the above
|
||||
* case only the *implementation class name*. Still, it is possible to set defaults
|
||||
* for *all* configuration options of such an element, as the following example
|
||||
* shows:
|
||||
*
|
||||
* <pre>
|
||||
* $prototypeConfiguration = [
|
||||
* 'formElementsDefinition' => [
|
||||
* 'Page' => [
|
||||
* 'implementationClassName' => 'TYPO3\CMS\Form\Domain\Model\FormElements\Page',
|
||||
* 'label' => 'this is the label of the page if nothing is specified'
|
||||
* ],
|
||||
* 'Textfield' => [
|
||||
* 'implementationClassName' => 'TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement',
|
||||
* 'label' = >'Default Label',
|
||||
* 'defaultValue' => 'Default form element value',
|
||||
* 'properties' => [
|
||||
* 'placeholder' => 'Text which is shown if element is empty'
|
||||
* ]
|
||||
* ]
|
||||
* ]
|
||||
* ]
|
||||
* </pre>
|
||||
*
|
||||
* Using Preconfigured $prototypeConfiguration
|
||||
* ---------------------------------
|
||||
*
|
||||
* Often, it is not really useful to manually create the $prototypeConfiguration array.
|
||||
*
|
||||
* Most of it comes pre-configured inside the YAML settings of the extensions,
|
||||
* and the {@link \TYPO3\CMS\Form\Domain\Configuration\ConfigurationService} contains helper methods
|
||||
* which return the ready-to-use *$prototypeConfiguration*.
|
||||
*
|
||||
* Property Mapping and Validation Rules
|
||||
* =====================================
|
||||
*
|
||||
* Besides Pages and FormElements, the FormDefinition can contain information
|
||||
* about the *format of the data* which is inputted into the form. This generally means:
|
||||
*
|
||||
* - expected Data Types
|
||||
* - Property Mapping Configuration to be used
|
||||
* - Validation Rules which should apply
|
||||
*
|
||||
* Background Info
|
||||
* ---------------
|
||||
* You might wonder why Data Types and Validation Rules are *not attached
|
||||
* to each FormElement itself*.
|
||||
*
|
||||
* If the form should create a *hierarchical output structure* such as a multi-
|
||||
* dimensional array or a PHP object, your expected data structure might look as follows:
|
||||
* <pre>
|
||||
* - person
|
||||
* -- firstName
|
||||
* -- lastName
|
||||
* -- address
|
||||
* --- street
|
||||
* --- city
|
||||
* </pre>
|
||||
*
|
||||
* Now, let's imagine you want to edit *person.address.street* and *person.address.city*,
|
||||
* but want to validate that the *combination* of *street* and *city* is valid
|
||||
* according to some address database.
|
||||
*
|
||||
* In this case, the form elements would be configured to fill *street* and *city*,
|
||||
* but the *validator* needs to be attached to the *compound object* *address*,
|
||||
* as both parts need to be validated together.
|
||||
*
|
||||
* Connecting FormElements to the output data structure
|
||||
* ====================================================
|
||||
*
|
||||
* The *identifier* of the *FormElement* is most important, as it determines
|
||||
* where in the output structure the value which is entered by the user is placed,
|
||||
* and thus also determines which validation rules need to apply.
|
||||
*
|
||||
* Using the above example, if you want to create a FormElement for the *street*,
|
||||
* you should use the identifier *person.address.street*.
|
||||
*
|
||||
* Rendering a FormDefinition
|
||||
* ==========================
|
||||
*
|
||||
* In order to trigger *rendering* on a FormDefinition,
|
||||
* the current {@link \TYPO3\CMS\Extbase\Mvc\Request} needs to be bound to the FormDefinition,
|
||||
* resulting in a {@link \TYPO3\CMS\Form\Domain\Runtime\FormRuntime} object which contains the *Runtime State* of the form
|
||||
* (such as the currently inserted values).
|
||||
*
|
||||
* /---code php
|
||||
* # $currentRequest and $currentResponse need to be available, f.e. inside a controller you would
|
||||
* # use $this->request. Inside a ViewHelper you would use $this->renderingContext->getRequest()
|
||||
* $form = $formDefinition->bind($currentRequest);
|
||||
*
|
||||
* # now, you can use the $form object to get information about the currently
|
||||
* # entered values into the form, etc.
|
||||
* \---
|
||||
*
|
||||
* Refer to the {@link \TYPO3\CMS\Form\Domain\Runtime\FormRuntime} API doc for further information.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*
|
||||
* @internal May change any time, use FormFactoryInterface to select a different FormDefinition if needed
|
||||
* @todo: Declare final in v12
|
||||
*/
|
||||
class FormDefinition extends AbstractCompositeRenderable implements VariableRenderableInterface
|
||||
{
|
||||
/**
|
||||
* The Form's pages
|
||||
*
|
||||
* @var array<int, Page>
|
||||
*/
|
||||
protected $renderables = [];
|
||||
|
||||
/**
|
||||
* The finishers for this form
|
||||
*
|
||||
* @var list<FinisherInterface>
|
||||
*/
|
||||
protected array $finishers = [];
|
||||
|
||||
/**
|
||||
* Property Mapping Rules, indexed by element identifier
|
||||
*
|
||||
* @var array<string, ProcessingRule>
|
||||
*/
|
||||
protected array $processingRules = [];
|
||||
|
||||
/**
|
||||
* Contains all elements of the form, indexed by identifier.
|
||||
* Is used as internal cache as we need this really often.
|
||||
*
|
||||
* @var array<string, FormElementInterface>
|
||||
*/
|
||||
protected array $elementsByIdentifier = [];
|
||||
|
||||
/**
|
||||
* Form element default values in the format ['elementIdentifier' => 'default value']
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $elementDefaultValues = [];
|
||||
|
||||
/**
|
||||
* Renderer class name to be used.
|
||||
*/
|
||||
protected string $rendererClassName = '';
|
||||
|
||||
/**
|
||||
* @var array<string, array<string, mixed>>
|
||||
*/
|
||||
protected array $typeDefinitions = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array<string, mixed>>
|
||||
*/
|
||||
protected array $validatorsDefinition = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array<string, mixed>>
|
||||
*/
|
||||
protected array $finishersDefinition = [];
|
||||
|
||||
/**
|
||||
* The persistence identifier of the form
|
||||
*/
|
||||
protected string $persistenceIdentifier = '';
|
||||
|
||||
/**
|
||||
* Constructor. Creates a new FormDefinition with the given identifier.
|
||||
*
|
||||
* @param string $identifier The Form Definition's identifier, must be a non-empty string.
|
||||
* @param array $prototypeConfiguration overrides form defaults of this definition
|
||||
* @param string $type element type of this form
|
||||
* @param string|null $persistenceIdentifier the persistence identifier of the form
|
||||
* @throws IdentifierNotValidException if the identifier was not valid
|
||||
*/
|
||||
public function __construct(
|
||||
string $identifier,
|
||||
array $prototypeConfiguration = [],
|
||||
string $type = 'Form',
|
||||
?string $persistenceIdentifier = null
|
||||
) {
|
||||
$this->typeDefinitions = $prototypeConfiguration['formElementsDefinition'] ?? [];
|
||||
$this->validatorsDefinition = $prototypeConfiguration['validatorsDefinition'] ?? [];
|
||||
$this->finishersDefinition = $prototypeConfiguration['finishersDefinition'] ?? [];
|
||||
|
||||
if ($identifier === '') {
|
||||
throw new IdentifierNotValidException('The given identifier was empty.', 1477082503);
|
||||
}
|
||||
|
||||
$this->identifier = $identifier;
|
||||
$this->type = $type;
|
||||
$this->persistenceIdentifier = (string)$persistenceIdentifier;
|
||||
|
||||
if ($prototypeConfiguration !== []) {
|
||||
$this->initializeFromFormDefaults();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the form defaults of the current type
|
||||
*
|
||||
* @throws TypeDefinitionNotFoundException
|
||||
* @internal
|
||||
*/
|
||||
protected function initializeFromFormDefaults()
|
||||
{
|
||||
if (!isset($this->typeDefinitions[$this->type])) {
|
||||
throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $this->type), 1474905835);
|
||||
}
|
||||
$typeDefinition = $this->typeDefinitions[$this->type];
|
||||
$this->setOptions($typeDefinition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple properties of this object at once.
|
||||
* Every property which has a corresponding set* method can be set using
|
||||
* the passed $options array.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function setOptions(array $options, bool $resetFinishers = false)
|
||||
{
|
||||
if (isset($options['rendererClassName'])) {
|
||||
$this->setRendererClassName($options['rendererClassName']);
|
||||
}
|
||||
if (isset($options['label'])) {
|
||||
$this->setLabel($options['label']);
|
||||
}
|
||||
if (isset($options['renderingOptions'])) {
|
||||
foreach ($options['renderingOptions'] as $key => $value) {
|
||||
$this->setRenderingOption($key, $value);
|
||||
}
|
||||
}
|
||||
if (isset($options['finishers'])) {
|
||||
if ($resetFinishers) {
|
||||
$this->finishers = [];
|
||||
}
|
||||
foreach ($options['finishers'] as $finisherConfiguration) {
|
||||
$this->createFinisher($finisherConfiguration['identifier'], $finisherConfiguration['options'] ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['variants'])) {
|
||||
foreach ($options['variants'] as $variantConfiguration) {
|
||||
$this->createVariant($variantConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayUtility::assertAllArrayKeysAreValid(
|
||||
$options,
|
||||
['rendererClassName', 'renderingOptions', 'finishers', 'formEditor', 'label', 'variants']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a page with the given $identifier and attach this page to the form.
|
||||
*
|
||||
* - Create Page object based on the given $typeName
|
||||
* - set defaults inside the Page object
|
||||
* - attach Page object to this form
|
||||
* - return the newly created Page object
|
||||
*
|
||||
* @param string $identifier Identifier of the new page
|
||||
* @param string $typeName Type of the new page
|
||||
* @return Page the newly created page
|
||||
* @throws TypeDefinitionNotFoundException
|
||||
*/
|
||||
public function createPage(string $identifier, string $typeName = 'Page'): Page
|
||||
{
|
||||
if (!isset($this->typeDefinitions[$typeName])) {
|
||||
throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1474905953);
|
||||
}
|
||||
|
||||
$typeDefinition = $this->typeDefinitions[$typeName];
|
||||
|
||||
if (!isset($typeDefinition['implementationClassName'])) {
|
||||
throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1477083126);
|
||||
}
|
||||
$implementationClassName = $typeDefinition['implementationClassName'];
|
||||
|
||||
/** @var Page $page */
|
||||
$page = GeneralUtility::makeInstance($implementationClassName, $identifier, $typeName);
|
||||
|
||||
if (isset($typeDefinition['label'])) {
|
||||
$page->setLabel($typeDefinition['label']);
|
||||
}
|
||||
|
||||
if (isset($typeDefinition['renderingOptions'])) {
|
||||
foreach ($typeDefinition['renderingOptions'] as $key => $value) {
|
||||
$page->setRenderingOption($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($typeDefinition['variants'])) {
|
||||
foreach ($typeDefinition['variants'] as $variantConfiguration) {
|
||||
$page->createVariant($variantConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayUtility::assertAllArrayKeysAreValid(
|
||||
$typeDefinition,
|
||||
['implementationClassName', 'label', 'renderingOptions', 'formEditor', 'variants']
|
||||
);
|
||||
|
||||
$this->addPage($page);
|
||||
return $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new page at the end of the form.
|
||||
*
|
||||
* Instead of this method, you should often use {@link createPage} instead.
|
||||
*
|
||||
* @param Page $page
|
||||
* @throws FormDefinitionConsistencyException if Page is already added to a FormDefinition
|
||||
* @see createPage
|
||||
*/
|
||||
public function addPage(Page $page)
|
||||
{
|
||||
$this->addRenderable($page);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Form's pages
|
||||
*
|
||||
* @return array<int, Page> The Form's pages in the correct order
|
||||
*/
|
||||
public function getPages(): array
|
||||
{
|
||||
return $this->renderables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a page with the given $index exists
|
||||
*
|
||||
* @return bool TRUE if a page with the given $index exists, otherwise FALSE
|
||||
*/
|
||||
public function hasPageWithIndex(int $index): bool
|
||||
{
|
||||
return isset($this->renderables[$index]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the page with the passed index. The first page has index zero.
|
||||
*
|
||||
* If page at $index does not exist, an exception is thrown. @see hasPageWithIndex()
|
||||
*
|
||||
* @param int $index
|
||||
* @return Page the page
|
||||
* @throws FormException if the specified index does not exist
|
||||
*/
|
||||
public function getPageByIndex(int $index)
|
||||
{
|
||||
if (!$this->hasPageWithIndex($index)) {
|
||||
throw new FormException(sprintf('There is no page with an index of %d', $index), 1329233627);
|
||||
}
|
||||
return $this->renderables[$index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified finisher to this form
|
||||
*/
|
||||
public function addFinisher(FinisherInterface $finisher)
|
||||
{
|
||||
$this->finishers[] = $finisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $finisherIdentifier identifier of the finisher as registered in the current form (for example: "Redirect")
|
||||
* @param array $options options for this finisher in the format ['option1' => 'value1', 'option2' => 'value2', ...]
|
||||
* @throws FinisherPresetNotFoundException
|
||||
*/
|
||||
public function createFinisher(string $finisherIdentifier, array $options = []): FinisherInterface
|
||||
{
|
||||
if (isset($this->finishersDefinition[$finisherIdentifier]['implementationClassName'])) {
|
||||
$implementationClassName = $this->finishersDefinition[$finisherIdentifier]['implementationClassName'];
|
||||
$defaultOptions = $this->finishersDefinition[$finisherIdentifier]['options'] ?? [];
|
||||
ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
|
||||
/** @var FinisherInterface $finisher */
|
||||
$finisher = GeneralUtility::makeInstance($implementationClassName);
|
||||
$finisher->setFinisherIdentifier($finisherIdentifier);
|
||||
$finisher->setOptions($defaultOptions);
|
||||
$this->addFinisher($finisher);
|
||||
return $finisher;
|
||||
}
|
||||
throw new FinisherPresetNotFoundException('The finisher preset identified by "' . $finisherIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328709784);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all finishers of this form
|
||||
*
|
||||
* @return list<FinisherInterface>
|
||||
*/
|
||||
public function getFinishers(): array
|
||||
{
|
||||
return $this->finishers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an element to the ElementsByIdentifier Cache.
|
||||
*
|
||||
* @throws DuplicateFormElementException
|
||||
* @internal
|
||||
*/
|
||||
public function registerRenderable(RenderableInterface $renderable)
|
||||
{
|
||||
if ($renderable instanceof FormElementInterface) {
|
||||
if (isset($this->elementsByIdentifier[$renderable->getIdentifier()])) {
|
||||
throw new DuplicateFormElementException(sprintf('A form element with identifier "%s" is already part of the form.', $renderable->getIdentifier()), 1325663761);
|
||||
}
|
||||
$this->elementsByIdentifier[$renderable->getIdentifier()] = $renderable;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an element from the ElementsByIdentifier cache
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function unregisterRenderable(RenderableInterface $renderable)
|
||||
{
|
||||
if ($renderable instanceof FormElementInterface) {
|
||||
unset($this->elementsByIdentifier[$renderable->getIdentifier()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all form elements with their identifiers as keys
|
||||
*
|
||||
* @return array<string, FormElementInterface>
|
||||
*/
|
||||
public function getElements(): array
|
||||
{
|
||||
return $this->elementsByIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Form Element by its identifier
|
||||
*
|
||||
* If identifier does not exist, returns NULL.
|
||||
*
|
||||
* @param string $elementIdentifier
|
||||
* @return FormElementInterface|null The element with the given $elementIdentifier or NULL if none found
|
||||
*/
|
||||
public function getElementByIdentifier(string $elementIdentifier)
|
||||
{
|
||||
return $this->elementsByIdentifier[$elementIdentifier] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default value of a form element
|
||||
*
|
||||
* @param string $elementIdentifier identifier of the form element. This supports property paths!
|
||||
* @param mixed $defaultValue
|
||||
* @internal
|
||||
*/
|
||||
public function addElementDefaultValue(string $elementIdentifier, $defaultValue)
|
||||
{
|
||||
$this->elementDefaultValues = ArrayUtility::setValueByPath(
|
||||
$this->elementDefaultValues,
|
||||
$elementIdentifier,
|
||||
$defaultValue,
|
||||
'.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the default value of the specified form element
|
||||
* or NULL if no default value was set
|
||||
*
|
||||
* @param string $elementIdentifier identifier of the form element. This supports property paths!
|
||||
* @return mixed The elements default value
|
||||
* @internal
|
||||
*/
|
||||
public function getElementDefaultValueByIdentifier(string $elementIdentifier)
|
||||
{
|
||||
return ObjectAccess::getPropertyPath($this->elementDefaultValues, $elementIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move $pageToMove before $referencePage
|
||||
*/
|
||||
public function movePageBefore(Page $pageToMove, Page $referencePage)
|
||||
{
|
||||
$this->moveRenderableBefore($pageToMove, $referencePage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move $pageToMove after $referencePage
|
||||
*/
|
||||
public function movePageAfter(Page $pageToMove, Page $referencePage)
|
||||
{
|
||||
$this->moveRenderableAfter($pageToMove, $referencePage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove $pageToRemove from form
|
||||
*/
|
||||
public function removePage(Page $pageToRemove)
|
||||
{
|
||||
$this->removeRenderable($pageToRemove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind the current request & response to this form instance, effectively creating
|
||||
* a new "instance" of the Form.
|
||||
*/
|
||||
public function bind(RequestInterface $request): FormRuntime
|
||||
{
|
||||
$formRuntime = GeneralUtility::makeInstance(FormRuntime::class);
|
||||
$formRuntime->setFormDefinition($this);
|
||||
$formRuntime->setRequest($request);
|
||||
$formRuntime->initialize();
|
||||
return $formRuntime;
|
||||
}
|
||||
|
||||
public function getProcessingRule(string $propertyPath): ProcessingRule
|
||||
{
|
||||
if (!isset($this->processingRules[$propertyPath])) {
|
||||
$this->processingRules[$propertyPath] = GeneralUtility::makeInstance(ProcessingRule::class);
|
||||
}
|
||||
return $this->processingRules[$propertyPath];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all mapping rules
|
||||
*
|
||||
* @return array<string, ProcessingRule>
|
||||
* @internal
|
||||
*/
|
||||
public function getProcessingRules(): array
|
||||
{
|
||||
return $this->processingRules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, mixed>>
|
||||
* @internal
|
||||
*/
|
||||
public function getTypeDefinitions(): array
|
||||
{
|
||||
return $this->typeDefinitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, mixed>>
|
||||
* @internal
|
||||
*/
|
||||
public function getValidatorsDefinition(): array
|
||||
{
|
||||
return $this->validatorsDefinition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the persistence identifier of the form
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getPersistenceIdentifier(): string
|
||||
{
|
||||
return $this->persistenceIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the renderer class name
|
||||
*/
|
||||
public function setRendererClassName(string $rendererClassName)
|
||||
{
|
||||
$this->rendererClassName = $rendererClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the classname of the renderer
|
||||
*/
|
||||
public function getRendererClassName(): string
|
||||
{
|
||||
return $this->rendererClassName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable;
|
||||
|
||||
/**
|
||||
* A base form element, which is the starting point for creating custom (PHP-based)
|
||||
* Form Elements.
|
||||
*
|
||||
* A *FormElement* is a part of a *Page*, which in turn is part of a FormDefinition.
|
||||
* See {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* Subclassing this class is a good starting-point for implementing custom PHP-based
|
||||
* Form Elements.
|
||||
*
|
||||
* Most of the functionality and API is implemented in {@link \TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable}, so
|
||||
* make sure to check out this class as well.
|
||||
*
|
||||
* Still, it is quite rare that you need to subclass this class; often
|
||||
* you can just use the {@link \TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement} and replace some templates.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is meant to be sub classed by developers.**
|
||||
*/
|
||||
abstract class AbstractFormElement extends AbstractRenderable implements FormElementInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $properties = [];
|
||||
|
||||
/**
|
||||
* Constructor. Needs this FormElement's identifier and the FormElement type
|
||||
*
|
||||
* @param string $identifier The FormElement's identifier
|
||||
* @param string $type The Form Element Type
|
||||
* @throws IdentifierNotValidException
|
||||
*/
|
||||
public function __construct(string $identifier, string $type)
|
||||
{
|
||||
if (strlen($identifier) === 0) {
|
||||
throw new IdentifierNotValidException('The given identifier string is empty.', 1477082502);
|
||||
}
|
||||
$this->identifier = $identifier;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method in your custom FormElements if needed
|
||||
*/
|
||||
public function initializeFormElement() {}
|
||||
|
||||
/**
|
||||
* Get the global unique identifier of the element
|
||||
*/
|
||||
public function getUniqueIdentifier(): string
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
$uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
|
||||
$uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
|
||||
return lcfirst($uniqueIdentifier);
|
||||
}
|
||||
|
||||
public function setOptions(array $options, bool $resetValidators = false)
|
||||
{
|
||||
if (isset($options['defaultValue'])) {
|
||||
$this->setDefaultValue($options['defaultValue']);
|
||||
}
|
||||
|
||||
if (isset($options['properties'])) {
|
||||
foreach ($options['properties'] as $key => $value) {
|
||||
$this->setProperty($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options, $resetValidators);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default value of the element
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default value of the element
|
||||
*
|
||||
* @param mixed $defaultValue
|
||||
*/
|
||||
public function setDefaultValue($defaultValue)
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
$currentDefaultValue = $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
|
||||
if (is_array($currentDefaultValue) && is_array($defaultValue)) {
|
||||
ArrayUtility::mergeRecursiveWithOverrule($currentDefaultValue, $defaultValue);
|
||||
$defaultValue = ArrayUtility::removeNullValuesRecursive($currentDefaultValue);
|
||||
}
|
||||
$formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the element is required
|
||||
*/
|
||||
public function isRequired(): bool
|
||||
{
|
||||
foreach ($this->getValidators() as $validator) {
|
||||
if ($validator instanceof NotEmptyValidator) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a property of the element
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setProperty(string $key, $value)
|
||||
{
|
||||
if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
|
||||
ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
|
||||
$this->properties[$key] = ArrayUtility::removeNullValuesRecursive($this->properties[$key]);
|
||||
} elseif ($value === null) {
|
||||
unset($this->properties[$key]);
|
||||
} else {
|
||||
$this->properties[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all properties
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException;
|
||||
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable;
|
||||
|
||||
/**
|
||||
* A base class for "section-like" form parts like "Page" or "Section" (which
|
||||
* is rendered as "Fieldset")
|
||||
*
|
||||
* This class contains multiple FormElements ({@link FormElementInterface}).
|
||||
*
|
||||
* Please see {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* Scope: frontend
|
||||
*/
|
||||
abstract class AbstractSection extends AbstractCompositeRenderable
|
||||
{
|
||||
/**
|
||||
* @var FormElementInterface[]
|
||||
*/
|
||||
protected $renderables = [];
|
||||
|
||||
/**
|
||||
* Constructor. Needs the identifier and type of this element
|
||||
*
|
||||
* @param string $identifier The Section identifier
|
||||
* @param string $type The Section type
|
||||
* @throws IdentifierNotValidException if the identifier was no non-empty string
|
||||
*/
|
||||
public function __construct(string $identifier, string $type)
|
||||
{
|
||||
if ($identifier === '') {
|
||||
throw new IdentifierNotValidException('The given identifier was empty.', 1477082501);
|
||||
}
|
||||
|
||||
$this->identifier = $identifier;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the child Form Elements
|
||||
*
|
||||
* @return FormElementInterface[] The Page's elements
|
||||
*/
|
||||
public function getElements(): array
|
||||
{
|
||||
return $this->renderables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the child Form Elements
|
||||
*
|
||||
* @return FormElementInterface[] The Page's elements
|
||||
*/
|
||||
public function getElementsRecursively(): array
|
||||
{
|
||||
return $this->getRenderablesRecursively();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new form element at the end of the section
|
||||
*
|
||||
* @param FormElementInterface $formElement The form element to add
|
||||
* @throws FormDefinitionConsistencyException if FormElement is already added to a section
|
||||
*/
|
||||
public function addElement(FormElementInterface $formElement)
|
||||
{
|
||||
$this->addRenderable($formElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a form element with the given $identifier and attach it to this section/page.
|
||||
*
|
||||
* - Create Form Element object based on the given $typeName
|
||||
* - set defaults inside the Form Element (based on the parent form's field defaults)
|
||||
* - attach Form Element to this Section/Page
|
||||
* - return the newly created Form Element object
|
||||
*
|
||||
*
|
||||
* @param string $identifier Identifier of the new form element
|
||||
* @param string $typeName type of the new form element
|
||||
* @return FormElementInterface the newly created form element
|
||||
* @throws TypeDefinitionNotFoundException
|
||||
* @throws TypeDefinitionNotValidException
|
||||
*/
|
||||
public function createElement(string $identifier, string $typeName): FormElementInterface
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
|
||||
$typeDefinitions = $formDefinition->getTypeDefinitions();
|
||||
if (isset($typeDefinitions[$typeName])) {
|
||||
$typeDefinition = $typeDefinitions[$typeName];
|
||||
} else {
|
||||
$renderingOptions = $formDefinition->getRenderingOptions();
|
||||
$skipUnknownElements = isset($renderingOptions['skipUnknownElements']) && $renderingOptions['skipUnknownElements'] === true;
|
||||
if (!$skipUnknownElements) {
|
||||
throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1382364019);
|
||||
}
|
||||
|
||||
$element = GeneralUtility::makeInstance(UnknownFormElement::class, $identifier, $typeName);
|
||||
$this->addElement($element);
|
||||
return $element;
|
||||
}
|
||||
|
||||
if (!isset($typeDefinition['implementationClassName'])) {
|
||||
throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1325689855);
|
||||
}
|
||||
|
||||
$implementationClassName = $typeDefinition['implementationClassName'];
|
||||
$element = GeneralUtility::makeInstance($implementationClassName, $identifier, $typeName);
|
||||
if (!$element instanceof FormElementInterface) {
|
||||
throw new TypeDefinitionNotValidException(sprintf('The "implementationClassName" for element "%s" ("%s") does not implement the FormElementInterface.', $identifier, $implementationClassName), 1327318156);
|
||||
}
|
||||
unset($typeDefinition['implementationClassName']);
|
||||
|
||||
$this->addElement($element);
|
||||
$element->setOptions($typeDefinition);
|
||||
|
||||
$element->initializeFormElement();
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move FormElement $element before $referenceElement.
|
||||
*
|
||||
* Both $element and $referenceElement must be direct descendants of this Section/Page.
|
||||
*
|
||||
* @param FormElementInterface $elementToMove
|
||||
* @param FormElementInterface $referenceElement
|
||||
*/
|
||||
public function moveElementBefore(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
|
||||
{
|
||||
$this->moveRenderableBefore($elementToMove, $referenceElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move FormElement $element after $referenceElement
|
||||
*
|
||||
* Both $element and $referenceElement must be direct descendants of this Section/Page.
|
||||
*
|
||||
* @param FormElementInterface $elementToMove
|
||||
* @param FormElementInterface $referenceElement
|
||||
*/
|
||||
public function moveElementAfter(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
|
||||
{
|
||||
$this->moveRenderableAfter($elementToMove, $referenceElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove $elementToRemove from this Section/Page
|
||||
*/
|
||||
public function removeElement(FormElementInterface $elementToRemove)
|
||||
{
|
||||
$this->removeRenderable($elementToRemove);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
|
||||
|
||||
/**
|
||||
* A date form element
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class Date extends AbstractFormElement implements StringableFormElementInterface
|
||||
{
|
||||
/**
|
||||
* Initializes the Form Element by setting the data type to "DateTime"
|
||||
* @internal
|
||||
*/
|
||||
public function initializeFormElement()
|
||||
{
|
||||
$this->setDataType(\DateTime::class);
|
||||
/** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
|
||||
$propertyMappingConfiguration = $this->getRootForm()->getProcessingRule($this->getIdentifier())->getPropertyMappingConfiguration();
|
||||
// @see https://www.w3.org/TR/2011/WD-html-markup-20110405/input.date.html#input.date.attrs.value
|
||||
// 'Y-m-d' = https://tools.ietf.org/html/rfc3339#section-5.6 -> full-date
|
||||
$propertyMappingConfiguration->setTypeConverterOption(DateTimeConverter::class, DateTimeConverter::CONFIGURATION_DATE_FORMAT, 'Y-m-d');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $value
|
||||
*/
|
||||
public function valueToString($value): string
|
||||
{
|
||||
$dateFormat = $this->properties['displayFormat'] ?? 'Y-m-d';
|
||||
|
||||
return $value->format($dateFormat);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException;
|
||||
use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
|
||||
use TYPO3\CMS\Core\Resource\ResourceFactory;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
|
||||
use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
|
||||
|
||||
/**
|
||||
* A generic file upload form element
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class FileUpload extends AbstractFormElement
|
||||
{
|
||||
/**
|
||||
* Initializes the Form Element by setting the data type to an Extbase File Reference
|
||||
* @internal
|
||||
*/
|
||||
public function initializeFormElement()
|
||||
{
|
||||
$this->setDataType(FileReference::class);
|
||||
|
||||
// Set the property mapping configuration for the file upload element.
|
||||
// * Add the UploadedFileReferenceConverter to convert an uploaded file to a
|
||||
// FileReference (single upload) or ObjectStorage (multiple uploads).
|
||||
// * Setup the storage:
|
||||
// If the property "saveToFileMount" exist for this element it will be used.
|
||||
// If this file mount or the property "saveToFileMount" does not exist
|
||||
// the default storage "1:/user_uploads/" will be used. Uploads are placed
|
||||
// in a dedicated sub-folder (e.g. ".../form_<40-chars-hash>/actual.file").
|
||||
$typeConverter = GeneralUtility::makeInstance(UploadedFileReferenceConverter::class);
|
||||
/** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
|
||||
$propertyMappingConfiguration = $this->getRootForm()
|
||||
->getProcessingRule($this->getIdentifier())
|
||||
->getPropertyMappingConfiguration()
|
||||
->setTypeConverter($typeConverter);
|
||||
|
||||
$uploadConfiguration = [
|
||||
UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
|
||||
];
|
||||
|
||||
// In preview mode (Form Editor backend module), skip upload folder resolution
|
||||
// entirely. File uploads are non-functional during preview and resolving the
|
||||
// target folder may throw access permission exceptions for backend users who
|
||||
// do not have access to the configured upload storage.
|
||||
if (!($this->getRootForm()->getRenderingOptions()['previewMode'] ?? false)) {
|
||||
$saveToFileMountIdentifier = $this->getProperties()['saveToFileMount'] ?? '';
|
||||
if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
|
||||
$uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
|
||||
} else {
|
||||
// @todo Why should uploaded files be stored to the same directory as the *.form.yaml definitions?
|
||||
$persistenceIdentifier = $this->getRootForm()->getPersistenceIdentifier();
|
||||
if (!empty($persistenceIdentifier)) {
|
||||
$pathinfo = PathUtility::pathinfo($persistenceIdentifier);
|
||||
$saveToFileMountIdentifier = $pathinfo['dirname'];
|
||||
if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
|
||||
$uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
|
||||
{
|
||||
if (empty($saveToFileMountIdentifier)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (PathUtility::isExtensionPath($saveToFileMountIdentifier)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
|
||||
|
||||
try {
|
||||
$resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
|
||||
return true;
|
||||
} catch (\InvalidArgumentException|InsufficientFolderAccessPermissionsException|FolderDoesNotExistException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
|
||||
|
||||
/**
|
||||
* A base form element interface, which can be the starting point for creating
|
||||
* custom (PHP-based) Form Elements.
|
||||
*
|
||||
* A *FormElement* is a part of a *Page*, which in turn is part of a FormDefinition.
|
||||
* See {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* **Often, you should rather subclass {@link AbstractFormElement} instead of
|
||||
* implementing this interface.**
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
interface FormElementInterface extends RenderableInterface
|
||||
{
|
||||
/**
|
||||
* Will be called as soon as the element is (tried to be) added to a form
|
||||
* @see registerInFormIfPossible()
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function initializeFormElement();
|
||||
|
||||
/**
|
||||
* Returns a unique identifier of this element.
|
||||
* While element identifiers are only unique within one form,
|
||||
* this includes the identifier of the form itself, making it "globally" unique
|
||||
*
|
||||
* @return string the "globally" unique identifier of this element
|
||||
*/
|
||||
public function getUniqueIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Get the default value with which the Form Element should be initialized
|
||||
* during display.
|
||||
*
|
||||
* @return mixed the default value for this Form Element
|
||||
*/
|
||||
public function getDefaultValue();
|
||||
|
||||
/**
|
||||
* Set the default value with which the Form Element should be initialized
|
||||
* during display.
|
||||
*
|
||||
* @param mixed $defaultValue the default value for this Form Element
|
||||
*/
|
||||
public function setDefaultValue($defaultValue);
|
||||
|
||||
/**
|
||||
* Set an element-specific configuration property.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setProperty(string $key, $value);
|
||||
|
||||
/**
|
||||
* Get all element-specific configuration properties
|
||||
*/
|
||||
public function getProperties(): array;
|
||||
|
||||
/**
|
||||
* Set a rendering option
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setRenderingOption(string $key, $value);
|
||||
|
||||
/**
|
||||
* Returns the child validators of the ConjunctionValidator that is registered for this element
|
||||
*
|
||||
* @return \SplObjectStorage<ValidatorInterface>
|
||||
* @internal
|
||||
*/
|
||||
public function getValidators(): \SplObjectStorage;
|
||||
|
||||
/**
|
||||
* Registers a validator for this element
|
||||
*/
|
||||
public function addValidator(ValidatorInterface $validator);
|
||||
|
||||
/**
|
||||
* Set the target data type for this element
|
||||
*
|
||||
* @param string $dataType the target data type
|
||||
*/
|
||||
public function setDataType(string $dataType);
|
||||
|
||||
/**
|
||||
* Whether or not this element is required
|
||||
*/
|
||||
public function isRequired(): bool;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* A generic form element
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class GenericFormElement extends AbstractFormElement {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* A grid column, being part of a grid container
|
||||
*
|
||||
* This class contains multiple FormElements ({@link FormElementInterface}).
|
||||
*
|
||||
* Please see {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
class GridColumn extends Section implements GridColumnInterface {}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* Scope: frontend
|
||||
*/
|
||||
interface GridColumnInterface extends FormElementInterface {}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* A grid row, being part of a grid container
|
||||
*
|
||||
* This class contains multiple FormElements ({@link FormElementInterface}).
|
||||
*
|
||||
* Please see {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
class GridRow extends Section implements GridRowInterface {}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* Scope: frontend
|
||||
*/
|
||||
interface GridRowInterface extends FormElementInterface {}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface;
|
||||
use TYPO3\CMS\Form\Exception as FormException;
|
||||
|
||||
/**
|
||||
* A Page, being part of a bigger FormDefinition. It contains numerous FormElements
|
||||
* as children.
|
||||
*
|
||||
* A FormDefinition consists of multiple Pages, where only one page is visible
|
||||
* at any given time.
|
||||
*
|
||||
* Most of the API of this object is implemented in {@link AbstractSection},
|
||||
* so make sure to review this class as well.
|
||||
*
|
||||
* Please see {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
class Page extends AbstractSection
|
||||
{
|
||||
/**
|
||||
* Constructor. Needs this Page's identifier
|
||||
*
|
||||
* @param string $identifier The Page's identifier
|
||||
* @param string $type The Page's type
|
||||
* @throws \TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException if the identifier was no non-empty string
|
||||
*/
|
||||
public function __construct(string $identifier, string $type = 'Page')
|
||||
{
|
||||
parent::__construct($identifier, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parent renderable
|
||||
*
|
||||
* @throws FormException
|
||||
*/
|
||||
public function setParentRenderable(CompositeRenderableInterface $parentRenderable)
|
||||
{
|
||||
if (!($parentRenderable instanceof FormDefinition)) {
|
||||
throw new FormException(sprintf('The specified parentRenderable must be a FormDefinition, got "%s"', get_debug_type($parentRenderable)), 1329233747);
|
||||
}
|
||||
parent::setParentRenderable($parentRenderable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
|
||||
|
||||
/**
|
||||
* A Section, being part of a bigger Page
|
||||
*
|
||||
* This class contains multiple FormElements ({@link FormElementInterface}).
|
||||
*
|
||||
* Please see {@link FormDefinition} for an in-depth explanation.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
class Section extends AbstractSection implements FormElementInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $properties = [];
|
||||
|
||||
/**
|
||||
* Will be called as soon as the element is (tried to be) added to a form
|
||||
* @see registerInFormIfPossible()
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function initializeFormElement() {}
|
||||
|
||||
public function setOptions(array $options, bool $resetValidators = false)
|
||||
{
|
||||
if (isset($options['properties'])) {
|
||||
foreach ($options['properties'] as $key => $value) {
|
||||
$this->setProperty($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options, $resetValidators);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a unique identifier of this element.
|
||||
* While element identifiers are only unique within one form,
|
||||
* this includes the identifier of the form itself, making it "globally" unique
|
||||
*
|
||||
* @return string the "globally" unique identifier of this element
|
||||
*/
|
||||
public function getUniqueIdentifier(): string
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
return sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default value with which the Form Element should be initialized
|
||||
* during display.
|
||||
* Note: This is currently not used for section elements
|
||||
*
|
||||
* @return mixed the default value for this Form Element
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default value with which the Form Element should be initialized
|
||||
* during display.
|
||||
* Note: This is currently ignored for section elements
|
||||
*
|
||||
* @param mixed $defaultValue the default value for this Form Element
|
||||
*/
|
||||
public function setDefaultValue($defaultValue) {}
|
||||
|
||||
/**
|
||||
* Get all element-specific configuration properties
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an element-specific configuration property.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setProperty(string $key, $value)
|
||||
{
|
||||
if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
|
||||
ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
|
||||
$this->properties[$key] = ArrayUtility::removeNullValuesRecursive($this->properties[$key]);
|
||||
} elseif ($value === null) {
|
||||
unset($this->properties[$key]);
|
||||
} else {
|
||||
$this->properties[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not this element is required
|
||||
*/
|
||||
public function isRequired(): bool
|
||||
{
|
||||
foreach ($this->getValidators() as $validator) {
|
||||
if ($validator instanceof NotEmptyValidator) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
/**
|
||||
* Interface for form elements capable of converting their complex values to string
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
interface StringableFormElementInterface
|
||||
{
|
||||
public function valueToString($value): string;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable;
|
||||
|
||||
/**
|
||||
* A Form Element that has no definition.
|
||||
*
|
||||
* Scope: frontend
|
||||
*/
|
||||
class UnknownFormElement extends AbstractRenderable implements FormElementInterface
|
||||
{
|
||||
/**
|
||||
* Constructor. Needs this FormElement's identifier and the FormElement type
|
||||
*
|
||||
* @param string $identifier The FormElement's identifier
|
||||
* @param string $type The Form Element Type
|
||||
* @throws IdentifierNotValidException
|
||||
*/
|
||||
public function __construct(string $identifier, string $type)
|
||||
{
|
||||
if ($identifier === '') {
|
||||
throw new IdentifierNotValidException('The given identifier was empty.', 1382364370);
|
||||
}
|
||||
$this->identifier = $identifier;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the form element
|
||||
*/
|
||||
public function initializeFormElement() {}
|
||||
|
||||
/**
|
||||
* Returns a unique identifier of this element.
|
||||
* While element identifiers are only unique within one form,
|
||||
* this includes the identifier of the form itself, making it "globally" unique
|
||||
*
|
||||
* @return string the "globally" unique identifier of this element
|
||||
*/
|
||||
public function getUniqueIdentifier(): string
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
$uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
|
||||
$uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9-_]/', '_', $uniqueIdentifier);
|
||||
return lcfirst($uniqueIdentifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template name of the renderable
|
||||
*/
|
||||
public function getTemplateName(): string
|
||||
{
|
||||
return 'UnknownElement';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed the default value for this Form Element
|
||||
* @internal
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not used in this implementation
|
||||
*
|
||||
* @param mixed $defaultValue the default value for this Form Element
|
||||
* @internal
|
||||
*/
|
||||
public function setDefaultValue($defaultValue) {}
|
||||
|
||||
/**
|
||||
* Not used in this implementation
|
||||
*
|
||||
* @param mixed $value
|
||||
* @internal
|
||||
*/
|
||||
public function setProperty(string $key, $value) {}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function isRequired(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException;
|
||||
|
||||
/**
|
||||
* Convenience base class which implements common functionality for most
|
||||
* classes which implement CompositeRenderableInterface, i.e. have **child renderable elements**.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
abstract class AbstractCompositeRenderable extends AbstractRenderable implements CompositeRenderableInterface
|
||||
{
|
||||
/**
|
||||
* array of child renderables
|
||||
*
|
||||
* @var \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface[]
|
||||
*/
|
||||
protected $renderables = [];
|
||||
|
||||
/**
|
||||
* Add a renderable to the list of child renderables.
|
||||
*
|
||||
* This function will be wrapped by the subclasses, f.e. with an "addPage"
|
||||
* or "addElement" method with the correct type hint.
|
||||
*
|
||||
* @param RenderableInterface $renderable
|
||||
* @throws FormDefinitionConsistencyException
|
||||
* @internal
|
||||
*/
|
||||
protected function addRenderable(RenderableInterface $renderable)
|
||||
{
|
||||
if ($renderable->getParentRenderable() !== null) {
|
||||
throw new FormDefinitionConsistencyException(sprintf('The renderable with identifier "%s" is already added to another element (element identifier: "%s").', $renderable->getIdentifier(), $renderable->getParentRenderable()->getIdentifier()), 1325665144);
|
||||
}
|
||||
$renderable->setIndex(count($this->renderables));
|
||||
$renderable->setParentRenderable($this);
|
||||
$this->renderables[] = $renderable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move $renderableToMove before $referenceRenderable
|
||||
*
|
||||
* This function will be wrapped by the subclasses, f.e. with an "movePageBefore"
|
||||
* or "moveElementBefore" method with the correct type hint.
|
||||
*
|
||||
* @param RenderableInterface $renderableToMove
|
||||
* @param RenderableInterface $referenceRenderable
|
||||
* @throws FormDefinitionConsistencyException
|
||||
* @internal
|
||||
*/
|
||||
protected function moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
|
||||
{
|
||||
if ($renderableToMove->getParentRenderable() !== $referenceRenderable->getParentRenderable() || $renderableToMove->getParentRenderable() !== $this) {
|
||||
throw new FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1326089744);
|
||||
}
|
||||
|
||||
$reorderedRenderables = [];
|
||||
$i = 0;
|
||||
foreach ($this->renderables as $renderable) {
|
||||
if ($renderable === $renderableToMove) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($renderable === $referenceRenderable) {
|
||||
$reorderedRenderables[] = $renderableToMove;
|
||||
$renderableToMove->setIndex($i);
|
||||
$i++;
|
||||
}
|
||||
$reorderedRenderables[] = $renderable;
|
||||
$renderable->setIndex($i);
|
||||
$i++;
|
||||
}
|
||||
$this->renderables = $reorderedRenderables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move $renderableToMove after $referenceRenderable
|
||||
*
|
||||
* This function will be wrapped by the subclasses, f.e. with an "movePageAfter"
|
||||
* or "moveElementAfter" method with the correct type hint.
|
||||
*
|
||||
* @param RenderableInterface $renderableToMove
|
||||
* @param RenderableInterface $referenceRenderable
|
||||
* @throws FormDefinitionConsistencyException
|
||||
* @internal
|
||||
*/
|
||||
protected function moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
|
||||
{
|
||||
if ($renderableToMove->getParentRenderable() !== $referenceRenderable->getParentRenderable() || $renderableToMove->getParentRenderable() !== $this) {
|
||||
throw new FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1477083145);
|
||||
}
|
||||
|
||||
$reorderedRenderables = [];
|
||||
$i = 0;
|
||||
foreach ($this->renderables as $renderable) {
|
||||
if ($renderable === $renderableToMove) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$reorderedRenderables[] = $renderable;
|
||||
$renderable->setIndex($i);
|
||||
$i++;
|
||||
|
||||
if ($renderable === $referenceRenderable) {
|
||||
$reorderedRenderables[] = $renderableToMove;
|
||||
$renderableToMove->setIndex($i);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
$this->renderables = $reorderedRenderables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all RenderableInterface instances of this composite renderable recursively
|
||||
*
|
||||
* @return RenderableInterface[]
|
||||
* @internal
|
||||
*/
|
||||
public function getRenderablesRecursively(): array
|
||||
{
|
||||
$renderables = [];
|
||||
foreach ($this->renderables as $renderable) {
|
||||
$renderables[] = $renderable;
|
||||
if ($renderable instanceof CompositeRenderableInterface) {
|
||||
$renderables = array_merge($renderables, $renderable->getRenderablesRecursively());
|
||||
}
|
||||
}
|
||||
return $renderables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a renderable from this renderable.
|
||||
*
|
||||
* This function will be wrapped by the subclasses, f.e. with an "removePage"
|
||||
* or "removeElement" method with the correct type hint.
|
||||
*
|
||||
* @param RenderableInterface $renderableToRemove
|
||||
* @throws FormDefinitionConsistencyException
|
||||
* @internal
|
||||
*/
|
||||
protected function removeRenderable(RenderableInterface $renderableToRemove)
|
||||
{
|
||||
if ($renderableToRemove->getParentRenderable() !== $this) {
|
||||
throw new FormDefinitionConsistencyException('The renderable to be removed must be part of the calling parent renderable.', 1326090127);
|
||||
}
|
||||
|
||||
$updatedRenderables = [];
|
||||
foreach ($this->renderables as $renderable) {
|
||||
if ($renderable === $renderableToRemove) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$updatedRenderables[] = $renderable;
|
||||
}
|
||||
$this->renderables = $updatedRenderables;
|
||||
|
||||
$renderableToRemove->onRemoveFromParentRenderable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this element at the parent form, if there is a connection to the parent form.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function registerInFormIfPossible()
|
||||
{
|
||||
parent::registerInFormIfPossible();
|
||||
foreach ($this->renderables as $renderable) {
|
||||
$renderable->registerInFormIfPossible();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called after a renderable has been removed from its parent
|
||||
* renderable.
|
||||
* This just passes the event down to all child renderables of this composite renderable.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function onRemoveFromParentRenderable()
|
||||
{
|
||||
foreach ($this->renderables as $renderable) {
|
||||
$renderable->onRemoveFromParentRenderable();
|
||||
}
|
||||
parent::onRemoveFromParentRenderable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,448 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Cache\CacheManager;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
|
||||
use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException;
|
||||
use TYPO3\CMS\Form\Domain\Model\Exception\ValidatorPresetNotFoundException;
|
||||
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
|
||||
use TYPO3\CMS\Form\Event\BeforeRenderableIsRemovedFromFormEvent;
|
||||
|
||||
/**
|
||||
* Convenience base class which implements common functionality for most
|
||||
* classes which implement RenderableInterface.
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractRenderable implements RenderableInterface, VariableRenderableInterface
|
||||
{
|
||||
/**
|
||||
* Abstract "type" of this Renderable. Is used during the rendering process
|
||||
* to determine the template file or the View PHP class being used to render
|
||||
* the particular element.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* The identifier of this renderable
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* The parent renderable
|
||||
*/
|
||||
protected ?CompositeRenderableInterface $parentRenderable = null;
|
||||
|
||||
/**
|
||||
* The label of this renderable
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label = '';
|
||||
|
||||
/**
|
||||
* associative array of rendering options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $renderingOptions = [];
|
||||
|
||||
/**
|
||||
* The position of this renderable inside the parent renderable.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $index = 0;
|
||||
|
||||
/**
|
||||
* The name of the template file of the renderable.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $templateName = '';
|
||||
|
||||
/**
|
||||
* associative array of rendering variants
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $variants = [];
|
||||
|
||||
protected ?ValidatorResolver $validatorResolver = null;
|
||||
|
||||
protected ?ServerRequestInterface $request = null;
|
||||
|
||||
/**
|
||||
* Get the type of the renderable
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the identifier of the element
|
||||
*/
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the identifier of the element
|
||||
*/
|
||||
public function setIdentifier(string $identifier)
|
||||
{
|
||||
$this->identifier = $identifier;
|
||||
}
|
||||
|
||||
public function getRequest(): ?ServerRequestInterface
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
public function setRequest(?ServerRequestInterface $request): void
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple properties of this object at once.
|
||||
* Every property which has a corresponding set* method can be set using
|
||||
* the passed $options array.
|
||||
*/
|
||||
public function setOptions(array $options, bool $resetValidators = false)
|
||||
{
|
||||
if (isset($options['label'])) {
|
||||
$this->setLabel($options['label']);
|
||||
}
|
||||
|
||||
if (isset($options['renderingOptions'])) {
|
||||
foreach ($options['renderingOptions'] as $key => $value) {
|
||||
$this->setRenderingOption($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['validators'])) {
|
||||
$runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
|
||||
$configurationHashes = $runtimeCache->get('formAbstractRenderableConfigurationHashes') ?: [];
|
||||
|
||||
if ($resetValidators) {
|
||||
$this->getRootForm()->getProcessingRule($this->getIdentifier())->removeAllValidators();
|
||||
$configurationHashes = [];
|
||||
}
|
||||
|
||||
foreach ($options['validators'] as $validatorConfiguration) {
|
||||
$configurationHash = md5(
|
||||
spl_object_hash($this)
|
||||
. json_encode($validatorConfiguration)
|
||||
);
|
||||
if (in_array($configurationHash, $configurationHashes)) {
|
||||
continue;
|
||||
}
|
||||
$this->createValidator($validatorConfiguration['identifier'], $validatorConfiguration['options'] ?? []);
|
||||
$configurationHashes[] = $configurationHash;
|
||||
$runtimeCache->set('formAbstractRenderableConfigurationHashes', $configurationHashes);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['variants'])) {
|
||||
foreach ($options['variants'] as $variantConfiguration) {
|
||||
$this->createVariant($variantConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayUtility::assertAllArrayKeysAreValid(
|
||||
$options,
|
||||
['label', 'defaultValue', 'properties', 'renderingOptions', 'validators', 'formEditor', 'variants']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a validator for the element.
|
||||
*
|
||||
* @throws ValidatorPresetNotFoundException
|
||||
*/
|
||||
public function createValidator(string $validatorIdentifier, array $options = []): ?ValidatorInterface
|
||||
{
|
||||
$validatorsDefinition = $this->getRootForm()->getValidatorsDefinition();
|
||||
if (isset($validatorsDefinition[$validatorIdentifier]) && is_array($validatorsDefinition[$validatorIdentifier]) && isset($validatorsDefinition[$validatorIdentifier]['implementationClassName'])) {
|
||||
$implementationClassName = $validatorsDefinition[$validatorIdentifier]['implementationClassName'];
|
||||
$defaultOptions = $validatorsDefinition[$validatorIdentifier]['options'] ?? [];
|
||||
ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
|
||||
// @todo: It would be great if Renderable's and FormElements could use DI, but especially
|
||||
// FormElements which extend AbstractRenderable pollute __construct() with manual
|
||||
// arguments. To retrieve the ValidatorResolver, we have to fall back to getContainer()
|
||||
// for now, until this has been resolved.
|
||||
if ($this->validatorResolver === null) {
|
||||
$container = GeneralUtility::getContainer();
|
||||
$this->validatorResolver = $container->get(ValidatorResolver::class);
|
||||
}
|
||||
$validator = $this->validatorResolver->createValidator($implementationClassName, $defaultOptions, $this->request);
|
||||
if ($validator !== null) {
|
||||
$this->addValidator($validator);
|
||||
}
|
||||
return $validator;
|
||||
}
|
||||
throw new ValidatorPresetNotFoundException('The validator preset identified by "' . $validatorIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328710202);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a validator to the element.
|
||||
*/
|
||||
public function addValidator(ValidatorInterface $validator)
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
$formDefinition->getProcessingRule($this->getIdentifier())->addValidator($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all validators on the element
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getValidators(): \SplObjectStorage
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
return $formDefinition->getProcessingRule($this->getIdentifier())->getValidators();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datatype
|
||||
*/
|
||||
public function setDataType(string $dataType)
|
||||
{
|
||||
$formDefinition = $this->getRootForm();
|
||||
$formDefinition->getProcessingRule($this->getIdentifier())->setDataType($dataType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the classname of the renderer
|
||||
*/
|
||||
public function getRendererClassName(): string
|
||||
{
|
||||
return $this->getRootForm()->getRendererClassName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all rendering options
|
||||
*/
|
||||
public function getRenderingOptions(): array
|
||||
{
|
||||
return $this->renderingOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the rendering option $key to $value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function setRenderingOption(string $key, $value)
|
||||
{
|
||||
if (is_array($value) && isset($this->renderingOptions[$key]) && is_array($this->renderingOptions[$key])) {
|
||||
ArrayUtility::mergeRecursiveWithOverrule($this->renderingOptions[$key], $value);
|
||||
$this->renderingOptions[$key] = ArrayUtility::removeNullValuesRecursive($this->renderingOptions[$key]);
|
||||
} elseif ($value === null) {
|
||||
unset($this->renderingOptions[$key]);
|
||||
} else {
|
||||
$this->renderingOptions[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parent renderable
|
||||
*
|
||||
* @return CompositeRenderableInterface|null
|
||||
*/
|
||||
public function getParentRenderable()
|
||||
{
|
||||
return $this->parentRenderable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parent renderable
|
||||
*/
|
||||
public function setParentRenderable(CompositeRenderableInterface $parentRenderable)
|
||||
{
|
||||
$this->parentRenderable = $parentRenderable;
|
||||
$this->registerInFormIfPossible();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the root form this element belongs to
|
||||
*
|
||||
* @throws FormDefinitionConsistencyException
|
||||
*/
|
||||
public function getRootForm(): FormDefinition
|
||||
{
|
||||
$rootRenderable = $this->parentRenderable;
|
||||
while ($rootRenderable !== null && !($rootRenderable instanceof FormDefinition)) {
|
||||
$rootRenderable = $rootRenderable->getParentRenderable();
|
||||
}
|
||||
if ($rootRenderable === null) {
|
||||
throw new FormDefinitionConsistencyException(sprintf('The form element "%s" is not attached to a parent form.', $this->identifier), 1326803398);
|
||||
}
|
||||
|
||||
return $rootRenderable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this element at the parent form, if there is a connection to the parent form.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function registerInFormIfPossible()
|
||||
{
|
||||
try {
|
||||
$rootForm = $this->getRootForm();
|
||||
$rootForm->registerRenderable($this);
|
||||
} catch (FormDefinitionConsistencyException $exception) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered when the renderable is removed from it's parent
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function onRemoveFromParentRenderable()
|
||||
{
|
||||
$event = GeneralUtility::makeInstance(EventDispatcherInterface::class)->dispatch(
|
||||
new BeforeRenderableIsRemovedFromFormEvent($this)
|
||||
);
|
||||
if ($event->isPropagationStopped()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$rootForm = $this->getRootForm();
|
||||
$rootForm->unregisterRenderable($this);
|
||||
} catch (FormDefinitionConsistencyException $exception) {
|
||||
}
|
||||
$this->parentRenderable = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the index of the renderable
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function getIndex(): int
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the index of the renderable
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function setIndex(int $index)
|
||||
{
|
||||
$this->index = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the label of the renderable
|
||||
*/
|
||||
public function getLabel(): string
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the label which shall be displayed next to the form element
|
||||
*/
|
||||
public function setLabel(string $label)
|
||||
{
|
||||
$this->label = $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the templateName name of the renderable
|
||||
*/
|
||||
public function getTemplateName(): string
|
||||
{
|
||||
return empty($this->renderingOptions['templateName'])
|
||||
? $this->type
|
||||
: $this->renderingOptions['templateName'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this renderable is enabled
|
||||
*/
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return !isset($this->renderingOptions['enabled']) || (bool)$this->renderingOptions['enabled'] === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all rendering variants
|
||||
*
|
||||
* @return RenderableVariantInterface[]
|
||||
*/
|
||||
public function getVariants(): array
|
||||
{
|
||||
return $this->variants;
|
||||
}
|
||||
|
||||
public function createVariant(array $options): RenderableVariantInterface
|
||||
{
|
||||
$identifier = $options['identifier'] ?? '';
|
||||
unset($options['identifier']);
|
||||
|
||||
$variant = GeneralUtility::makeInstance(RenderableVariant::class, $identifier, $options, $this);
|
||||
|
||||
$this->addVariant($variant);
|
||||
return $variant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified variant to this form element
|
||||
*/
|
||||
public function addVariant(RenderableVariantInterface $variant)
|
||||
{
|
||||
$this->variants[$variant->getIdentifier()] = $variant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the specified variant to this form element
|
||||
* regardless of their conditions
|
||||
*/
|
||||
public function applyVariant(RenderableVariantInterface $variant)
|
||||
{
|
||||
$variant->apply();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
/**
|
||||
* Interface which all Form Parts must adhere to **when they have sub elements**.
|
||||
* This includes especially "FormDefinition" and "Page".
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
interface CompositeRenderableInterface extends RenderableInterface
|
||||
{
|
||||
/**
|
||||
* Returns all RenderableInterface instances of this composite renderable recursively
|
||||
*
|
||||
* @return \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface[]
|
||||
* @internal
|
||||
*/
|
||||
public function getRenderablesRecursively(): array;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
/*
|
||||
* Inspired by and partially taken from the Neos.Form package (www.neos.io)
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
/**
|
||||
* Base interface which all Form Parts except the FormDefinition must adhere
|
||||
* to (i.e. all elements which are NOT the root of a Form).
|
||||
*
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
*/
|
||||
interface RenderableInterface extends RootRenderableInterface
|
||||
{
|
||||
/**
|
||||
* Return the parent renderable
|
||||
*
|
||||
* @return CompositeRenderableInterface|null the parent renderable
|
||||
* @internal
|
||||
*/
|
||||
public function getParentRenderable();
|
||||
|
||||
/**
|
||||
* Set the new parent renderable. You should not call this directly;
|
||||
* it is automatically called by addRenderable.
|
||||
*
|
||||
* This method should also register itself at the parent form, if possible.
|
||||
*
|
||||
* @param CompositeRenderableInterface $renderable
|
||||
* @internal
|
||||
*/
|
||||
public function setParentRenderable(CompositeRenderableInterface $renderable);
|
||||
|
||||
/**
|
||||
* Set the index of this renderable inside the parent renderable
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function setIndex(int $index);
|
||||
|
||||
/**
|
||||
* Get the index inside the parent renderable
|
||||
*/
|
||||
public function getIndex(): int;
|
||||
|
||||
/**
|
||||
* This function is called after a renderable has been removed from its parent
|
||||
* renderable. The function should make sure to clean up the internal state,
|
||||
* like resetting $this->parentRenderable or deregistering the renderable
|
||||
* of the form.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function onRemoveFromParentRenderable();
|
||||
|
||||
/**
|
||||
* Register this element at the parent form, if there is a connection to the parent form.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function registerInFormIfPossible();
|
||||
|
||||
/**
|
||||
* Get the template name of the renderable
|
||||
*/
|
||||
public function getTemplateName(): string;
|
||||
|
||||
/**
|
||||
* Returns whether this renderable is enabled
|
||||
*/
|
||||
public function isEnabled(): bool;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
|
||||
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
|
||||
|
||||
/**
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* @internal
|
||||
*/
|
||||
class RenderableVariant implements RenderableVariantInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* @var VariableRenderableInterface
|
||||
*/
|
||||
protected $renderable;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $condition = '';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $applied = false;
|
||||
|
||||
/**
|
||||
* @throws IdentifierNotValidException
|
||||
*/
|
||||
public function __construct(
|
||||
string $identifier,
|
||||
array $options,
|
||||
VariableRenderableInterface $renderable
|
||||
) {
|
||||
if ($identifier === '') {
|
||||
throw new IdentifierNotValidException('The given variant identifier was empty.', 1519998923);
|
||||
}
|
||||
$this->identifier = $identifier;
|
||||
$this->renderable = $renderable;
|
||||
|
||||
if (isset($options['condition']) && is_string($options['condition'])) {
|
||||
$this->condition = $options['condition'];
|
||||
}
|
||||
|
||||
unset($options['condition'], $options['identifier'], $options['variants']);
|
||||
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the specified variant to this form element
|
||||
* regardless of their conditions
|
||||
*/
|
||||
public function apply(): void
|
||||
{
|
||||
$this->renderable->setOptions($this->options, true);
|
||||
$this->applied = true;
|
||||
}
|
||||
|
||||
public function conditionMatches(Resolver $conditionResolver): bool
|
||||
{
|
||||
if (empty($this->condition)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$conditionResolver->evaluate($this->condition, ['renderable' => $this->renderable]);
|
||||
}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
public function isApplied(): bool
|
||||
{
|
||||
return $this->applied;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Form\Domain\Model\Renderable;
|
||||
|
||||
use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
|
||||
|
||||
/**
|
||||
* Scope: frontend
|
||||
* **This class is NOT meant to be sub classed by developers.**
|
||||
* @internal
|
||||
*/
|
||||
interface RenderableVariantInterface
|
||||
{
|
||||
public function getIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Apply the specified variant to this form element
|
||||
* regardless of their conditions
|
||||
*/
|
||||
public function apply(): void;
|
||||
|
||||
public function isApplied(): bool;
|
||||
|
||||
public function conditionMatches(Resolver $conditionResolver): bool;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user