Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .check-author.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ exclude:
mapping:
"Ingolf Steinhardt <info@e-spin.de>":
- "Ingolf Steinhardt <git@e-spin.de>"
- "e-spin <gitlab@e-spin.de>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of MetaModels/attribute_translatedfile.
*
* (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.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/attribute_translatedfile
* @author Ingolf Steinhardt <info@e-spin.de>
* @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;
}
}
10 changes: 10 additions & 0 deletions src/Resources/config/event_listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,7 +14,8 @@
* @author David Molineus <david.molineus@netzmacht.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright 2012-2021 The MetaModels team.
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2023 The MetaModels team.
* @license https://github.com/MetaModels/attribute_translatedfile/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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;
}
)
Expand Down