Skip to content

Commit 1bc0de4

Browse files
authored
Merge pull request #1549 from MetaModels/hotfix/add_own_attribute_for_option_label
Add own attribute for option label
2 parents 98e3b3e + 46decfa commit 1bc0de4

File tree

7 files changed

+284
-30
lines changed

7 files changed

+284
-30
lines changed

src/CoreBundle/EventListener/DcGeneral/Table/FilterSetting/AttributeListener.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This file is part of MetaModels/core.
55
*
6-
* (c) 2012-2024 The MetaModels team.
6+
* (c) 2012-2026 The MetaModels team.
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -14,7 +14,7 @@
1414
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
1515
* @author Sven Baumann <baumann.sv@gmail.com>
1616
* @author Ingolf Steinhardt <info@e-spin.de>
17-
* @copyright 2012-2024 The MetaModels team.
17+
* @copyright 2012-2026 The MetaModels team.
1818
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
1919
* @filesource
2020
*/
@@ -75,9 +75,9 @@ public function getOptions(GetPropertyOptionsEvent $event): void
7575
assert($dataDefinition instanceof ContainerInterface);
7676

7777
if (
78-
('tl_metamodel_filtersetting' !== $dataDefinition->getName())
79-
|| ('attr_id' !== $event->getPropertyName())
80-
|| null !== $event->getOptions()
78+
null !== $event->getOptions()
79+
|| ('tl_metamodel_filtersetting' !== $dataDefinition->getName())
80+
|| !\in_array($event->getPropertyName(), ['attr_id', 'label_attr_id'], true)
8181
) {
8282
return;
8383
}
@@ -115,11 +115,7 @@ public function decodeValue(DecodePropertyValueForWidgetEvent $event): void
115115
{
116116
$dataDefinition = $event->getEnvironment()->getDataDefinition();
117117
assert($dataDefinition instanceof ContainerInterface);
118-
119-
if (
120-
('tl_metamodel_filtersetting' !== $dataDefinition->getName())
121-
|| ('attr_id' !== $event->getProperty())
122-
) {
118+
if (!$this->wantToHandle($dataDefinition, $event)) {
123119
return;
124120
}
125121

@@ -149,10 +145,7 @@ public function encodeValue(EncodePropertyValueFromWidgetEvent $event): void
149145
$dataDefinition = $event->getEnvironment()->getDataDefinition();
150146
assert($dataDefinition instanceof ContainerInterface);
151147

152-
if (
153-
('tl_metamodel_filtersetting' !== $dataDefinition->getName())
154-
|| ('attr_id' !== $event->getProperty())
155-
) {
148+
if (!$this->wantToHandle($dataDefinition, $event)) {
156149
return;
157150
}
158151

@@ -170,4 +163,12 @@ public function encodeValue(EncodePropertyValueFromWidgetEvent $event): void
170163
assert($attribute instanceof IAttribute);
171164
$event->setValue($attribute->get('id'));
172165
}
166+
167+
public function wantToHandle(
168+
ContainerInterface $dataDefinition,
169+
DecodePropertyValueForWidgetEvent|EncodePropertyValueFromWidgetEvent $event
170+
): bool {
171+
return ('tl_metamodel_filtersetting' === $dataDefinition->getName())
172+
&& \in_array($event->getProperty(), ['attr_id', 'label_attr_id'], true);
173+
}
173174
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* This file is part of MetaModels/core.
5+
*
6+
* (c) 2012-2026 The MetaModels team.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* This project is provided in good faith and hope to be usable by anyone.
12+
*
13+
* @package MetaModels/core
14+
* @author Ingolf Steinhardt <info@e-spin.de>
15+
* @copyright 2012-2026 The MetaModels team.
16+
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
17+
* @filesource
18+
*/
19+
20+
namespace MetaModels\CoreBundle\EventListener\DcGeneral\Table\FilterSetting;
21+
22+
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator;
23+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ConditionChainInterface;
24+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ConditionInterface;
25+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\Condition\Property\NotCondition;
26+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionChain;
27+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\PropertyInterface;
28+
use ContaoCommunityAlliance\DcGeneral\Factory\Event\BuildDataDefinitionEvent;
29+
use MetaModels\Attribute\IAliasConverter;
30+
use MetaModels\DcGeneral\DataDefinition\Palette\Condition\Property\FilterSettingAttributeInstanceOfCondition;
31+
use MetaModels\Filter\Setting\IFilterSettingFactory;
32+
33+
final readonly class FilterSettingsVisibilityListener
34+
{
35+
public function __construct(
36+
private RequestScopeDeterminator $scopeMatcher,
37+
private IFilterSettingFactory $filterSettingFactory,
38+
) {
39+
}
40+
41+
public function __invoke(BuildDataDefinitionEvent $event): void
42+
{
43+
if (!$this->scopeMatcher->currentScopeIsBackend()) {
44+
return;
45+
}
46+
47+
$container = $event->getContainer();
48+
49+
if ('tl_metamodel_filtersetting' !== $container->getName()) {
50+
return;
51+
}
52+
53+
$palettes = $container->getPalettesDefinition();
54+
foreach ($palettes->getPalettes() as $palette) {
55+
foreach ($palette->getProperties() as $property) {
56+
if ('label_attr_id' !== $property->getName()) {
57+
continue;
58+
}
59+
$condition = new FilterSettingAttributeInstanceOfCondition(
60+
$this->filterSettingFactory,
61+
IAliasConverter::class
62+
);
63+
64+
$this->addCondition($property, new NotCondition($condition));
65+
}
66+
}
67+
}
68+
69+
private function addCondition(PropertyInterface $property, ConditionInterface $condition): void
70+
{
71+
$currentCondition = $property->getVisibleCondition();
72+
if (
73+
(!($currentCondition instanceof ConditionChainInterface))
74+
|| ($currentCondition->getConjunction() != ConditionChainInterface::OR_CONJUNCTION)
75+
) {
76+
if ($currentCondition === null) {
77+
$currentCondition = new PropertyConditionChain([$condition]);
78+
} else {
79+
$currentCondition = new PropertyConditionChain([$currentCondition, $condition]);
80+
}
81+
$currentCondition->setConjunction(ConditionChainInterface::OR_CONJUNCTION);
82+
$property->setVisibleCondition($currentCondition);
83+
} else {
84+
$currentCondition->addCondition($condition);
85+
}
86+
}
87+
}

src/CoreBundle/Resources/config/dc-general/table/tl_filtersetting.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@ services:
8989
- name: kernel.event_listener
9090
event: dc-general.view.contao2backend.get-property-options
9191
method: handle
92+
93+
MetaModels\CoreBundle\EventListener\DcGeneral\Table\FilterSetting\FilterSettingsVisibilityListener:
94+
arguments:
95+
- '@cca.dc-general.scope-matcher'
96+
- '@metamodels.filter_setting_factory'
97+
tags:
98+
- name: kernel.event_listener
99+
event: 'dc-general.factory.build-data-definition'

src/CoreBundle/Resources/contao/dca/tl_metamodel_filtersetting.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This file is part of MetaModels/core.
55
*
6-
* (c) 2012-2025 The MetaModels team.
6+
* (c) 2012-2026 The MetaModels team.
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -20,7 +20,7 @@
2020
* @author Ingolf Steinhardt <info@e-spin.de>
2121
* @author Richard Henkenjohann <richardhenkenjohann@googlemail.com>
2222
* @author Sven Baumann <baumann.sv@gmail.com>
23-
* @copyright 2012-2025 The MetaModels team.
23+
* @copyright 2012-2026 The MetaModels team.
2424
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
2525
* @filesource
2626
*/
@@ -229,6 +229,9 @@
229229
],
230230
],
231231
'simplelookup extends _attribute_' => [
232+
'+config' => [
233+
'label_attr_id',
234+
],
232235
'+fefilter' => [
233236
'urlparam',
234237
'predef_param',
@@ -341,6 +344,18 @@
341344
],
342345
'sql' => "int(10) unsigned NOT NULL default '0'"
343346
],
347+
'label_attr_id' => [
348+
'label' => 'label_attr_id.label',
349+
'description' => 'label_attr_id.description',
350+
'exclude' => true,
351+
'inputType' => 'select',
352+
'eval' => [
353+
'includeBlankOption' => true,
354+
'tl_class' => 'w50',
355+
'chosen' => true
356+
],
357+
'sql' => 'varchar(255) NOT NULL default \'\''
358+
],
344359
'all_langs' => [
345360
'label' => 'all_langs.label',
346361
'description' => 'all_langs.description',

src/CoreBundle/Resources/translations/tl_metamodel_filtersetting.en.xlf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<trans-unit id="attr_id.description" resname="attr_id.description">
8888
<source>Attribute this setting relates to.</source>
8989
</trans-unit>
90+
<trans-unit id="label_attr_id.label" resname="label_attr_id.label">
91+
<source>Attribute for option label</source>
92+
</trans-unit>
93+
<trans-unit id="label_attr_id.description" resname="label_attr_id.description">
94+
<source>Here you can select a second attribute to be displayed as an option label.</source>
95+
</trans-unit>
9096
<trans-unit id="all_langs.label" resname="all_langs.label">
9197
<source>Search all languages</source>
9298
</trans-unit>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* This file is part of MetaModels/core.
5+
*
6+
* (c) 2012-2026 The MetaModels team.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* This project is provided in good faith and hope to be usable by anyone.
12+
*
13+
* @package MetaModels/core
14+
* @author Ingolf Steinhardt <info@e-spin.de>
15+
* @copyright 2012-2026 The MetaModels team.
16+
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
17+
* @filesource
18+
*/
19+
20+
namespace MetaModels\DcGeneral\DataDefinition\Palette\Condition\Property;
21+
22+
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
23+
use ContaoCommunityAlliance\DcGeneral\Data\PropertyValueBag;
24+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionInterface;
25+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\LegendInterface;
26+
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\PropertyInterface;
27+
use MetaModels\Filter\Setting\IFilterSettingFactory;
28+
use MetaModels\IMetaModel;
29+
30+
/**
31+
* @SuppressWarnings(PHPMD.LongClassName)
32+
*/
33+
final readonly class FilterSettingAttributeInstanceOfCondition implements PropertyConditionInterface
34+
{
35+
public function __construct(
36+
private IFilterSettingFactory $filterFactory,
37+
private string $attributeBaseClass,
38+
) {
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
#[\Override]
45+
public function match(
46+
ModelInterface $model = null,
47+
PropertyValueBag $input = null,
48+
PropertyInterface $property = null,
49+
LegendInterface $legend = null
50+
) {
51+
if (!$model) {
52+
return false;
53+
}
54+
$attrId = $model->getProperty('attr_id');
55+
if (!$attrId) {
56+
return false;
57+
}
58+
$metaModel = $this->getMetaModel($model);
59+
$attribute = $metaModel->getAttributeById((int) $attrId);
60+
61+
if (!$attribute) {
62+
return false;
63+
}
64+
65+
return $attribute instanceof $this->attributeBaseClass;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
#[\Override]
72+
public function __clone()
73+
{
74+
}
75+
76+
private function getMetaModel(ModelInterface $model): IMetaModel
77+
{
78+
$filterSetting = $this->filterFactory->createCollection($model->getProperty('fid'));
79+
80+
return $filterSetting->getMetaModel();
81+
}
82+
}

0 commit comments

Comments
 (0)