Skip to content

Commit

Permalink
Merge pull request #42 from tritum/version/4.x.x
Browse files Browse the repository at this point in the history
Version/4.x.x TYPO3 v12 compatibility
  • Loading branch information
tritum authored Nov 22, 2024
2 parents f2d6bbd + 44c2b3a commit c29348b
Show file tree
Hide file tree
Showing 14 changed files with 447 additions and 173 deletions.
14 changes: 12 additions & 2 deletions Classes/Configuration/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* This file is part of the TYPO3 CMS extension "repeatable_form_elements".
*
* Copyright (C) 2018 Ralf Zimmermann TRITUM GmbH <ralf.zimmermann@tritum.de>
* Copyright (C) 2018 Ralf Zimmermann dreistrom.land AG <r.zimmermann@dreistrom.land>
* Copyright (C) 2021 Elias Häußler <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -27,14 +27,16 @@
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
*
* @author Ralf Zimmermann TRITUM GmbH <ralf.zimmermann@tritum.de>
* @author Ralf Zimmermann | dreistrom.land AG <r.zimmermann@dreistrom.land>
* @author Elias Häußler <[email protected]>
* @author Christian Seyfferth | dreistrom.land AG <[email protected]>
* @license GPL-2.0-or-later
*/
final class Extension
Expand All @@ -43,6 +45,7 @@ final class Extension

public static function addTypoScriptSetup(): void
{
// @todo: maybe move this to 'EXT:repeatable_form_elements/ext_typoscript_setup.typoscript'
ExtensionManagementUtility::addTypoScriptSetup(trim('
module.tx_form {
settings {
Expand All @@ -57,6 +60,8 @@ 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',
Expand All @@ -70,4 +75,9 @@ 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();
}
}
76 changes: 76 additions & 0 deletions Classes/Event/CopyVariantEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace TRITUM\RepeatableFormElements\Event;

/**
* This file is part of the "repeatable_form_elements" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;

/**
* Use this event to manipulate variantOptions on copying form elements
*/
class CopyVariantEvent
{
private array $options;
private FormElementInterface $originalFormElement;
private FormElementInterface $newFormElement;
private string $newIdentifier;
private bool $variantEnabled = true;

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

public function getOptions(): array
{
return $this->options;
}

public function setOptions(array $options): void
{
$this->options = $options;
}

public function getOriginalFormElement(): FormElementInterface
{
return $this->originalFormElement;
}

public function getNewFormElement(): FormElementInterface
{
return $this->newFormElement;
}

public function getNewIdentifier(): string
{
return $this->newIdentifier;
}

public function isVariantEnabled(): bool
{
return $this->variantEnabled;
}

public function setVariantEnabled(bool $variantEnabled): void
{
$this->variantEnabled = $variantEnabled;
}


}
50 changes: 50 additions & 0 deletions Classes/EventListener/AdaptVariantConditionEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace TRITUM\RepeatableFormElements\EventListener;

/**
* This file is part of the "repeatable_form_elements" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

use TRITUM\RepeatableFormElements\Event\CopyVariantEvent;


/**
* Replace original identifiers inside variant condition with identifiers of new element
*
* @param CopyVariantEvent $event
* @return void
*/
final class AdaptVariantConditionEventListener
{
public function __invoke(CopyVariantEvent $event): void
{
$options = $event->getOptions();
$originalIdentifier = $event->getOriginalFormElement()->getIdentifier();

// 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());

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

$event->setOptions($options);
}
}
2 changes: 2 additions & 0 deletions Classes/FormElements/RepeatableContainer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace TRITUM\RepeatableFormElements\FormElements;

/**
Expand Down
18 changes: 6 additions & 12 deletions Classes/Hooks/FormHooks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

namespace TRITUM\RepeatableFormElements\Hooks;

/**
Expand All @@ -11,7 +13,6 @@
use TRITUM\RepeatableFormElements\FormElements\RepeatableContainerInterface;
use TRITUM\RepeatableFormElements\Service\CopyService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement;
use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
Expand Down Expand Up @@ -44,10 +45,11 @@ public function afterInitializeCurrentPage(
return $currentPage;
}

$copyService = GeneralUtility::makeInstance(CopyService::class, $formRuntime);
if ($this->userWentBackToPreviousStep($formRuntime, $currentPage, $lastPage)) {
$this->getObjectManager()->get(CopyService::class, $formRuntime)->createCopiesFromFormState();
$copyService->createCopiesFromFormState();
} else {
$this->getObjectManager()->get(CopyService::class, $formRuntime)->createCopiesFromCurrentRequest();
$copyService->createCopiesFromCurrentRequest();
}

return $currentPage;
Expand Down Expand Up @@ -110,7 +112,7 @@ protected function setRootRepeatableContainerIdentifiers(
$renderable->setIdentifier($newIdentifier);
$formRuntime->getFormDefinition()->registerRenderable($renderable);

$copyService = $this->getObjectManager()->get(CopyService::class, $formRuntime);
$copyService = GeneralUtility::makeInstance(CopyService::class, $formRuntime);
[$originalProcessingRule] = $copyService->copyProcessingRule($originalIdentifier, $newIdentifier);

/** @var ValidatorInterface $validator */
Expand Down Expand Up @@ -150,12 +152,4 @@ protected function userWentBackToPreviousStep(
&& $lastPage !== null
&& $currentPage->getIndex() < $lastPage->getIndex();
}

/**
* @return ObjectManager
*/
protected function getObjectManager(): ObjectManager
{
return GeneralUtility::makeInstance(ObjectManager::class);
}
}
Loading

0 comments on commit c29348b

Please sign in to comment.