From 74e3c9afd9d2ef6d654a3d48159ad7be79896a95 Mon Sep 17 00:00:00 2001 From: e-spin Date: Sat, 10 Jun 2023 15:36:42 +0200 Subject: [PATCH] Reactivate widget types and sorting --- .check-author.yml | 1 + .../DcaSetting/FileWidgetModeOptions.php | 92 +++++++++++++++++++ src/Resources/config/event_listeners.yml | 10 ++ ...lsAttributeTranslatedFileExtensionTest.php | 21 ++++- 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/EventListener/DcGeneral/Table/DcaSetting/FileWidgetModeOptions.php diff --git a/.check-author.yml b/.check-author.yml index ca7f60c..fcca555 100644 --- a/.check-author.yml +++ b/.check-author.yml @@ -7,3 +7,4 @@ exclude: mapping: "Ingolf Steinhardt ": - "Ingolf Steinhardt " + - "e-spin " diff --git a/src/EventListener/DcGeneral/Table/DcaSetting/FileWidgetModeOptions.php b/src/EventListener/DcGeneral/Table/DcaSetting/FileWidgetModeOptions.php new file mode 100644 index 0000000..b29413f --- /dev/null +++ b/src/EventListener/DcGeneral/Table/DcaSetting/FileWidgetModeOptions.php @@ -0,0 +1,92 @@ + + * @copyright 2012-2023 The MetaModels team. + * @license https://github.com/MetaModels/attribute_translatedfile/blob/master/LICENSE LGPL-3.0-or-later + * @filesource + */ + +declare(strict_types=1); + +namespace MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\DcaSetting; + +use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator; +use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPropertyOptionsEvent; +use Doctrine\DBAL\Connection; +use MetaModels\CoreBundle\EventListener\DcGeneral\Table\DcaSetting\AbstractListener; +use MetaModels\IFactory; + +/** + * Add the options for the file widget mode. + */ +class FileWidgetModeOptions extends AbstractListener +{ + /** + * Invoke the event. + * + * @param GetPropertyOptionsEvent $event The event. + * + * @return void + */ + public function __invoke(GetPropertyOptionsEvent $event): void + { + if (('file_widgetMode' !== $event->getPropertyName()) + || (false === $this->wantToHandle($event)) + || (false === $this->isAttributeTranslatedFile($event)) + ) { + return; + } + + $this->addOptions($event); + } + + /** + * Add the options. + * + * @param GetPropertyOptionsEvent $event The event. + * + * @return void + */ + private function addOptions(GetPropertyOptionsEvent $event): void + { + $addOptions = ['downloads', 'gallery']; + + $event->setOptions(\array_values(\array_unique(\array_merge($event->getOptions(), $addOptions)))); + } + + /** + * If used attribute type of file. + * + * @param GetPropertyOptionsEvent $event The event. + * + * @return bool + */ + private function isAttributeTranslatedFile(GetPropertyOptionsEvent $event): bool + { + $builder = $this->connection->createQueryBuilder(); + $builder + ->select('t.type') + ->from('tl_metamodel_attribute', 't') + ->where($builder->expr()->eq('t.id', ':id')) + ->setParameter('id', $event->getModel()->getProperty('attr_id')); + + $statement = $builder->execute(); + if (0 === $statement->columnCount()) { + return false; + } + + $result = $statement->fetch(\PDO::FETCH_OBJ); + return 'translatedfile' === $result->type; + } +} diff --git a/src/Resources/config/event_listeners.yml b/src/Resources/config/event_listeners.yml index 83a7b3f..823b1bc 100644 --- a/src/Resources/config/event_listeners.yml +++ b/src/Resources/config/event_listeners.yml @@ -36,3 +36,13 @@ services: event: dc-general.view.contao2backend.get-property-options method: removeOption priority: -1 + + MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\DcaSetting\FileWidgetModeOptions: + public: false + arguments: + $scopeDeterminator: '@cca.dc-general.scope-matcher' + $factory: '@MetaModels\IFactory' + $connection: '@database_connection' + tags: + - name: kernel.event_listener + event: dc-general.view.contao2backend.get-property-options diff --git a/tests/DependencyInjection/MetaModelsAttributeTranslatedFileExtensionTest.php b/tests/DependencyInjection/MetaModelsAttributeTranslatedFileExtensionTest.php index 9629cf0..28b3e0c 100644 --- a/tests/DependencyInjection/MetaModelsAttributeTranslatedFileExtensionTest.php +++ b/tests/DependencyInjection/MetaModelsAttributeTranslatedFileExtensionTest.php @@ -3,7 +3,7 @@ /** * This file is part of MetaModels/attribute_translatedfile. * - * (c) 2012-2021 The MetaModels team. + * (c) 2012-2023 The MetaModels team. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -14,7 +14,8 @@ * @author David Molineus * @author Sven Baumann * @author Christian Schiffler - * @copyright 2012-2021 The MetaModels team. + * @author Ingolf Steinhardt + * @copyright 2012-2023 The MetaModels team. * @license https://github.com/MetaModels/attribute_translatedfile/blob/master/LICENSE LGPL-3.0-or-later * @filesource */ @@ -27,6 +28,7 @@ use MetaModels\AttributeTranslatedFileBundle\EventListener\BuildAttributeListener; use MetaModels\AttributeTranslatedFileBundle\EventListener\BuildDataDefinitionListener; use MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\Attribute\RemoveTypeOptions; +use MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\DcaSetting\FileWidgetModeOptions; use MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\FilterSetting\RemoveAttIdOptions; use MetaModels\AttributeTranslatedFileBundle\EventListener\Factory\AddAttributeInformation; use MetaModels\AttributeTranslatedFileBundle\EventListener\ImageSizeOptions; @@ -65,7 +67,7 @@ public function testFactoryIsRegistered() $container = $this->getMockBuilder(ContainerBuilder::class)->getMock(); $container - ->expects(self::exactly(8)) + ->expects(self::exactly(9)) ->method('setDefinition') ->withConsecutive( [ @@ -168,6 +170,19 @@ function ($value) { $this->assertEquals(RemoveAttIdOptions::class, $value->getClass()); $this->assertCount(1, $value->getTag('kernel.event_listener')); + return true; + } + ) + ], + [ + FileWidgetModeOptions::class, + self::callback( + function ($value) { + /** @var Definition $value */ + $this->assertInstanceOf(Definition::class, $value); + $this->assertEquals(null, $value->getClass()); + $this->assertCount(1, $value->getTag('kernel.event_listener')); + return true; } )