Skip to content

Commit

Permalink
Fix always save at input mask
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Jan 25, 2025
1 parent 4df6f01 commit 439c07a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require": {
"php": "^8.1",
"ext-dom": "*",
"contao-community-alliance/dc-general": "^2.3.27",
"contao-community-alliance/dc-general": "^2.3.29",
"contao-community-alliance/events-contao-bindings": "^4.13.1",
"contao-community-alliance/meta-palettes": "^2.0.10",
"contao-community-alliance/translator": "^2.4.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* This file is part of MetaModels/core.
*
* (c) 2012-2025 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/core
* @author Sven Baumann <[email protected]>
* @copyright 2012-2025 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace MetaModels\CoreBundle\EventListener\DcGeneral\Table\DcaSetting;

use Contao\Message;
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\BuildWidgetEvent;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface;
use Doctrine\DBAL\Connection;
use MetaModels\IFactory;
use MetaModels\IMetaModel;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* This handles disabling of the alwaysSave field
*/
class DisableAlwaysSaveListener extends AbstractListener
{
/**
* The translator.
*
* @var TranslatorInterface
*/
private TranslatorInterface $translator;

/**
* Create a new instance.
*
* @param RequestScopeDeterminator $scopeDeterminator The scope determinator.
* @param IFactory $factory The MetaModel factory.
* @param Connection $connection The database connection.
* @param TranslatorInterface $translator The translator.
*/
public function __construct(
RequestScopeDeterminator $scopeDeterminator,
IFactory $factory,
Connection $connection,
TranslatorInterface $translator
) {
parent::__construct($scopeDeterminator, $factory, $connection);
$this->translator = $translator;
}

/**
* Disable the readonly checkbox field if the selected attribute has force_alias.
*
* @param BuildWidgetEvent $event The event.
*
* @return void
*/
public function handle(BuildWidgetEvent $event)
{
$environment = $event->getEnvironment();
$dataDefinition = $environment->getDataDefinition();
assert($dataDefinition instanceof ContainerInterface);

if (
($dataDefinition->getName() !== 'tl_metamodel_dcasetting')
|| ($event->getProperty()->getName() !== 'alwaysSave')
|| (null === $event->getModel()->getId())
) {
return;
}

$model = $event->getModel();
assert($model instanceof ModelInterface);
$metaModel = $this->getMetaModelFromModel($model);
assert($metaModel instanceof IMetaModel);
$attribute = $metaModel->getAttributeById((int) $model->getProperty('attr_id'));
if (null === $attribute) {
return;
}

if ($attribute->get('force_alias')) {
Message::addInfo(
$this->translator->trans('readonly_for_always_save', [], 'tl_metamodel_dcasetting')
);

$extra = $event->getProperty()->getExtra();

$extra['disabled'] = true;

$event->getProperty()->setExtra($extra);

$model->setProperty('alwaysSave', true);
}
}
}
12 changes: 12 additions & 0 deletions src/CoreBundle/Resources/config/dc-general/table/tl_dcasetting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ services:
event: dc-general.view.contao2backend.build-widget
method: handle

metamodels.listener.table.tl_metamodel_dcasetting.disable_always_save:
class: MetaModels\CoreBundle\EventListener\DcGeneral\Table\DcaSetting\DisableAlwaysSaveListener
arguments:
- "@cca.dc-general.scope-matcher"
- "@metamodels.factory"
- "@database_connection"
- "@translator"
tags:
- name: kernel.event_listener
event: dc-general.view.contao2backend.build-widget
method: handle

metamodels.listener.table.tl_metamodel_dcasetting.legend_title:
class: MetaModels\CoreBundle\EventListener\DcGeneral\Table\DcaSetting\LegendTitleListener
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,13 @@
<source>Set backend class.</source>
</trans-unit>
<trans-unit id="mandatory_for_unique_attr" resname="mandatory_for_unique_attr">
<source>Unique attribues are automatically mandatory (this is not changable).</source>
<source>Unique attributes are automatically mandatory (this is not changable).</source>
</trans-unit>
<trans-unit id="readonly_for_force_alias" resname="readonly_for_force_alias">
<source>Attribues with force alias are automatically readonly (this is not changable).</source>
<source>Attributes with force alias are automatically readonly (this is not changable).</source>
</trans-unit>
<trans-unit id="readonly_for_always_save" resname="readonly_for_always_save">
<source>Attributes with always save are automatically readonly (this is not changable).</source>
</trans-unit>
<trans-unit id="showRecord" resname="showRecord">
<source>Show the details of record %id%</source>
Expand Down

0 comments on commit 439c07a

Please sign in to comment.