Skip to content

Commit

Permalink
Merge pull request #48 from dreistromland-tj/task/typo3_v13_compatibi…
Browse files Browse the repository at this point in the history
…lity

Ad TYPO3 v13 compatibility, drop v11
  • Loading branch information
tritum authored Dec 3, 2024
2 parents 698e177 + 4dc4859 commit d0340df
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 316 deletions.
21 changes: 0 additions & 21 deletions Classes/Configuration/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
namespace TRITUM\RepeatableFormElements\Configuration;

use TRITUM\RepeatableFormElements\Hooks\FormHooks;
use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Extension
Expand Down Expand Up @@ -58,26 +54,9 @@ public static function addTypoScriptSetup(): void
'));
}

public static function registerIcons(): void
{
if ('12.4' <= self::getTypo3Version()) return;

$iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
$iconRegistry->registerIcon(
't3-form-icon-repeatable-container',
SvgIconProvider::class,
['source' => 'EXT:repeatable_form_elements/Resources/Public/Icons/t3-form-icon-repeatable-container.svg']
);
}

public static function registerHooks(): void
{
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'][1511196413] = FormHooks::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'][1511196413] = FormHooks::class;
}

private static function getTypo3Version() : string
{
return (new Typo3Version())->getVersion();
}
}
15 changes: 6 additions & 9 deletions Classes/Event/CopyVariantEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ class CopyVariantEvent
private bool $variantEnabled = true;

public function __construct(
array $options,
array $options,
FormElementInterface $originalFormElement,
FormElementInterface $newFormElement,
string $newIdentifier
)
{
$this->options = $options;
string $newIdentifier,
) {
$this->options = $options;
$this->originalFormElement = $originalFormElement;
$this->newFormElement = $newFormElement;
$this->newIdentifier = $newIdentifier;
$this->newFormElement = $newFormElement;
$this->newIdentifier = $newIdentifier;
}

public function getOptions(): array
Expand Down Expand Up @@ -71,6 +70,4 @@ public function setVariantEnabled(bool $variantEnabled): void
{
$this->variantEnabled = $variantEnabled;
}


}
10 changes: 4 additions & 6 deletions Classes/EventListener/AdaptVariantConditionEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@

use TRITUM\RepeatableFormElements\Event\CopyVariantEvent;


/**
* Replace original identifiers inside variant condition with identifiers of new element
*
* @param CopyVariantEvent $event
* @return void
*/
final class AdaptVariantConditionEventListener
{
Expand All @@ -30,19 +28,19 @@ public function __invoke(CopyVariantEvent $event): void
// get path strings for identifiers for replacement in condition
// e.g. for `traverse(formValues, 'repeatablecontainer-1.0.checkbox-1')`
$originalIdentifierAsPath = str_replace('.', '/', $originalIdentifier);
$newIdentifierAsPath = str_replace('.', '/', $event->getNewIdentifier());
$newIdentifierAsPath = str_replace('.', '/', $event->getNewIdentifier());

// adapt original condition to match identifier of the copied form element
$options['condition'] = str_replace(
[
$originalIdentifier,
$originalIdentifierAsPath
$originalIdentifierAsPath,
],
[
$event->getNewIdentifier(),
$newIdentifierAsPath
$newIdentifierAsPath,
],
$options['condition']
$options['condition'],
);

$event->setOptions($options);
Expand Down
41 changes: 27 additions & 14 deletions Classes/Finisher/SaveToDatabaseFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;

/**
* This finisher is an extension to the default SaveToDatabaseFinisher.
* This finisher is an extension to the default SaveToDatabaseFinisher.
* It is intentionally registered as a new identifier to keep compatibility with existing forms.
*/
class SaveToDatabaseFinisher extends \TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher
{
protected function process(int $iterationCount)
protected function process(int $iterationCount): void
{
$this->throwExceptionOnInconsistentConfiguration();

Expand Down Expand Up @@ -68,10 +68,14 @@ protected function process(int $iterationCount)
* @param array $databaseData prepared data
* @param string $table Tablename to save data to
* @param int $iterationCount finisher iteration
* @return void
*/
protected function processContainer(string $containerPath, array $elementsConfiguration, array $databaseData, string $table, int $iterationCount)
{
protected function processContainer(
string $containerPath,
array $elementsConfiguration,
array $databaseData,
string $table,
int $iterationCount,
): void {
$containerValues = ArrayUtility::getValueByPath($this->getFormValues(), $containerPath, '.');
foreach ($containerValues as $copyId => $containerItem) {
$prefix = $containerPath . '.' . $copyId . '.';
Expand All @@ -90,8 +94,12 @@ protected function processContainer(string $containerPath, array $elementsConfig
* @param string $prefix prefix to get the form element object by a full identifier
* @return array the filled database data
*/
protected function prepareData(array $elementsConfiguration, array $databaseData, array $values = [], string $prefix = ''): array
{
protected function prepareData(
array $elementsConfiguration,
array $databaseData,
array $values = [],
string $prefix = '',
): array {
if (empty($values)) {
$values = $this->getFormValues();
}
Expand Down Expand Up @@ -131,8 +139,12 @@ protected function prepareData(array $elementsConfiguration, array $databaseData
* $databaseData into the table $table
* and provide some finisher variables
*/
protected function saveToDatabase(array $databaseData, string $table, int $iterationCount, ?int $containerItemKey = null)
{
protected function saveToDatabase(
array $databaseData,
string $table,
int $iterationCount,
?int $containerItemKey = null,
): void {
if (!empty($databaseData)) {
if ($this->parseOption('mode') === 'update') {
$whereClause = $this->parseOption('whereClause');
Expand All @@ -142,26 +154,27 @@ protected function saveToDatabase(array $databaseData, string $table, int $itera
$this->databaseConnection->update(
$table,
$databaseData,
$whereClause
$whereClause,
);
} else {
$this->databaseConnection->insert($table, $databaseData);
$insertedUid = (int)$this->databaseConnection->lastInsertId($table);
$this->finisherContext->getFinisherVariableProvider()->add(
$this->shortFinisherIdentifier,
'insertedUids.' . $iterationCount . (is_int($containerItemKey) ? '.' . $containerItemKey : ''),
$insertedUid
$insertedUid,
);

$currentCount = $this->finisherContext->getFinisherVariableProvider()->get(
$this->shortFinisherIdentifier,
'countInserts.', $iterationCount,
0
'countInserts.',
$iterationCount,
0,
);
$this->finisherContext->getFinisherVariableProvider()->addOrUpdate(
$this->shortFinisherIdentifier,
'countInserts.' . $iterationCount,
$currentCount++
$currentCount++,
);
}
}
Expand Down
6 changes: 2 additions & 4 deletions Classes/FormElements/RepeatableContainer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace TRITUM\RepeatableFormElements\FormElements;

Expand All @@ -12,6 +12,4 @@
*/
use TYPO3\CMS\Form\Domain\Model\FormElements\Section;

class RepeatableContainer extends Section implements RepeatableContainerInterface
{
}
class RepeatableContainer extends Section implements RepeatableContainerInterface {}
8 changes: 4 additions & 4 deletions Classes/FormElements/RepeatableContainerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace TRITUM\RepeatableFormElements\FormElements;

/**
Expand All @@ -10,6 +12,4 @@
*/
use TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface;

interface RepeatableContainerInterface extends CompositeRenderableInterface
{
}
interface RepeatableContainerInterface extends CompositeRenderableInterface {}
24 changes: 15 additions & 9 deletions Classes/Hooks/FormHooks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace TRITUM\RepeatableFormElements\Hooks;

Expand All @@ -14,6 +14,7 @@
use TRITUM\RepeatableFormElements\Service\CopyService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
use TYPO3\CMS\Form\Domain\Model\Exception\DuplicateFormElementException;
use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement;
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
use TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface;
Expand All @@ -28,13 +29,15 @@ class FormHooks
* @param CompositeRenderableInterface|null $currentPage
* @param CompositeRenderableInterface|null $lastPage
* @param array $rawRequestArguments
*
* @return CompositeRenderableInterface|null
* @throws DuplicateFormElementException
*/
public function afterInitializeCurrentPage(
FormRuntime $formRuntime,
CompositeRenderableInterface $currentPage = null,
CompositeRenderableInterface $lastPage = null,
array $rawRequestArguments = []
array $rawRequestArguments = [],
): ?CompositeRenderableInterface {
foreach ($formRuntime->getPages() as $page) {
$this->setRootRepeatableContainerIdentifiers($page, $formRuntime);
Expand Down Expand Up @@ -76,16 +79,18 @@ public function beforeRendering(FormRuntime $formRuntime, RootRenderableInterfac
}

/**
* @param RenderableInterface $formElement
* @param RenderableInterface $renderable
* @param FormRuntime $formRuntime
* @param array $repeatableContainerIdentifiers
*
* @throws DuplicateFormElementException
*/
protected function setRootRepeatableContainerIdentifiers(
RenderableInterface $renderable,
FormRuntime $formRuntime,
array $repeatableContainerIdentifiers = []
array $repeatableContainerIdentifiers = [],
): void {
$isRepeatableContainer = $renderable instanceof RepeatableContainerInterface ? true : false;
$isRepeatableContainer = $renderable instanceof RepeatableContainerInterface;

$hasOriginalIdentifier = isset($renderable->getRenderingOptions()['_originalIdentifier']);
if ($isRepeatableContainer) {
Expand All @@ -104,7 +109,7 @@ protected function setRootRepeatableContainerIdentifiers(
$originalIdentifier = $renderable->getIdentifier();
$renderable->setRenderingOption('_originalIdentifier', $originalIdentifier);

if($renderable instanceof AbstractFormElement && $renderable->getDefaultValue()) {
if ($renderable instanceof AbstractFormElement && $renderable->getDefaultValue()) {
$formRuntime->getFormDefinition()->addElementDefaultValue($newIdentifier, $renderable->getDefaultValue());
}

Expand Down Expand Up @@ -139,14 +144,15 @@ protected function setRootRepeatableContainerIdentifiers(
* returns TRUE if the user went back to any previous step in the form.
*
* @param FormRuntime $formRuntime
* @param CompositeRenderableInterface $currentPage
* @param CompositeRenderableInterface $lastPage
* @param CompositeRenderableInterface|null $currentPage
* @param CompositeRenderableInterface|null $lastPage
*
* @return bool
*/
protected function userWentBackToPreviousStep(
FormRuntime $formRuntime,
CompositeRenderableInterface $currentPage = null,
CompositeRenderableInterface $lastPage = null
CompositeRenderableInterface $lastPage = null,
): bool {
return $currentPage !== null
&& $lastPage !== null
Expand Down
Loading

0 comments on commit d0340df

Please sign in to comment.