Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_visibility_conditions' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Fix visibility conditions

See merge request metamodels/core!317
  • Loading branch information
zonky2 committed Aug 18, 2024
2 parents 76f5a46 + aecc4bf commit 5ea99b7
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 24 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.19",
"contao-community-alliance/dc-general": "^2.3.20",
"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
Expand Up @@ -56,7 +56,10 @@ public function __invoke(
TranslatorInterface $translator,
ContaoFramework $framework,
): Response {
$containerName = (string) $request->query->get('table', 'tl_metamodel');
$containerName = (string) $request->query->get('table', '');
if ('' === $containerName) {
$containerName = (string) ($request->attributes->get('_route_params', [])['tableName'] ?? 'tl_metamodel');
}
$controllerResult = $this->bootDcGeneralAndProcess(
$request,
$containerName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function modelToLabel(ModelToLabelEvent $event)
->setLabel(
'<div class="field_heading cte_type"><strong>%s</strong> <em>[%s%s]</em></div>
<div class="field_type block">
%s<strong>%s</strong> - %s
%s <strong>%s</strong> - %s
</div>'
)
->setArgs([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function drawAttribute(ModelToLabelEvent $event)
$event
->setLabel('<div class="field_heading cte_type %s"><strong>%s</strong> <em>[%s%s]</em></div>
<div class="field_type block">
%s<strong>%s</strong><span class="mandatory">%s</span> <span class="tl_class">%s</span>
%s <strong>%s</strong><span class="mandatory">%s</span> <span class="tl_class">%s</span>
</div>')
->setArgs([
$model->getProperty('published') ? 'published' : 'unpublished',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@

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

use Contao\System;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\DecodePropertyValueForWidgetEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\EncodePropertyValueFromWidgetEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPropertyOptionsEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\ManipulateWidgetEvent;
use ContaoCommunityAlliance\DcGeneral\Data\DataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\Properties\PropertyInterface;
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;
use ContaoCommunityAlliance\DcGeneral\Event\AbstractEnvironmentAwareEvent;
Expand All @@ -37,6 +40,12 @@
use MetaModels\ITranslatedMetaModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use function is_array;
use function method_exists;
use function str_replace;
use function str_starts_with;
use function substr;

/**
* This handles the rendering of models to labels.
*
Expand Down Expand Up @@ -68,7 +77,7 @@ public function getValueOptions(GetPropertyOptionsEvent $event)

$attribute = $metaModel->getAttributeById((int) $attributeId);
if ($attribute) {
$options = $this->getOptionsViaDcGeneral($metaModel, $event->getEnvironment(), $attribute);
$options = $this->getOptionsViaDcGeneral($metaModel, $event->getEnvironment(), $attribute, $model);
$mangled = [];
foreach ($options as $key => $option) {
$mangled['value_' . $key] = $option;
Expand Down Expand Up @@ -104,7 +113,7 @@ public function decodeValue(DecodePropertyValueForWidgetEvent $event)

$currentLanguage = $this->extractCurrentLanguageContext($metaModel);

if (\is_array($event->getValue())) {
if (is_array($event->getValue())) {
$values = [];

foreach ($event->getValue() as $value) {
Expand Down Expand Up @@ -133,17 +142,19 @@ public function encodeValue(EncodePropertyValueFromWidgetEvent $event)

$model = $event->getPropertyValueBag();
$metaModel = $this->getMetaModel($event->getEnvironment());
if (null === $attributeId = $model->getPropertyValue('attr_id')) {
if (null === $attributeOption = $model->getPropertyValue('attr_id')) {
return;
}

if (null === $attribute = $metaModel->getAttributeById((int) $attributeId)) {
// Cut off the 'mm_xyz_' prefix.
$attrColName = \substr($attributeOption, \strlen($metaModel->getTableName() . '_'));
if (null === $attribute = $metaModel->getAttribute($attrColName)) {
return;
}

$currentLanguage = $this->extractCurrentLanguageContext($metaModel);

if (\is_array($event->getValue())) {
if (is_array($event->getValue())) {
$values = [];

foreach ($event->getValue() as $value) {
Expand Down Expand Up @@ -191,11 +202,11 @@ protected function wantToHandle(AbstractEnvironmentAwareEvent $event)
if (!parent::wantToHandle($event)) {
return false;
}
if (\method_exists($event, 'getPropertyName') && ('value' !== $event->getPropertyName())) {
if (method_exists($event, 'getPropertyName') && ('value' !== $event->getPropertyName())) {
return false;
}

if (\method_exists($event, 'getProperty')) {
if (method_exists($event, 'getProperty')) {
$property = $event->getProperty();
if ($property instanceof PropertyInterface) {
$property = $property->getName();
Expand All @@ -215,10 +226,11 @@ protected function wantToHandle(AbstractEnvironmentAwareEvent $event)
* @param IMetaModel $metaModel The metamodel instance to obtain the values from.
* @param EnvironmentInterface $environment The environment used in the input screen table dc-general.
* @param IAttribute $attribute The attribute to obtain the values for.
* @param ModelInterface $model The model being edited.
*
* @return array
*/
private function getOptionsViaDcGeneral($metaModel, $environment, $attribute)
private function getOptionsViaDcGeneral($metaModel, $environment, $attribute, $model)
{
$factory = DcGeneralFactory::deriveEmptyFromEnvironment($environment)
->setContainerName($metaModel->getTableName());
Expand All @@ -227,6 +239,22 @@ private function getOptionsViaDcGeneral($metaModel, $environment, $attribute)
$subEnv = $dcGeneral->getEnvironment();
$dataProvider = $subEnv->getDataProvider();
assert($dataProvider instanceof DataProviderInterface);
if ($dataProvider instanceof MultiLanguageDataProviderInterface) {
// FIXME: check if language supported.
$locale = System::getContainer()->get('request_stack')->getCurrentRequest()?->getLocale();

Check failure on line 244 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

PossiblyNullReference: Cannot call method getCurrentRequest on possibly null value (reported by psalm)

Check failure on line 244 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

PossiblyNullReference: Cannot call method getCurrentRequest on possibly null value (reported by psalm)

Check failure on line 244 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

PossiblyNullReference: Cannot call method getCurrentRequest on possibly null value (reported by psalm)
$languages = $dataProvider->getLanguages($model->getId());
$found = false;
foreach ($languages as $language) {

Check failure on line 247 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

PossiblyNullIterator: Cannot iterate over nullable var ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationCollectionInterface|null (reported by psalm)

Check failure on line 247 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

PossiblyNullIterator: Cannot iterate over nullable var ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationCollectionInterface|null (reported by psalm)

Check failure on line 247 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

PossiblyNullIterator: Cannot iterate over nullable var ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationCollectionInterface|null (reported by psalm)
if ($language->getLocale() === $locale) {
$dataProvider->setCurrentLanguage($locale);

Check failure on line 249 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

PossiblyNullArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage cannot be null, possibly null value provided (reported by psalm)

Check failure on line 249 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

PossiblyNullArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage cannot be null, possibly null value provided (reported by psalm)

Check failure on line 249 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

PossiblyNullArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage cannot be null, possibly null value provided (reported by psalm)
$found = true;
break;
}
}
if (!$found) {
$dataProvider->setCurrentLanguage($dataProvider->getFallbackLanguage($model->getId()));

Check failure on line 255 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.1 Contao: ~4.13.0

InvalidArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage expects string, but ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationInterface|null provided (reported by psalm)

Check failure on line 255 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.2 Contao: ~4.13.0

InvalidArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage expects string, but ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationInterface|null provided (reported by psalm)

Check failure on line 255 in src/CoreBundle/EventListener/DcGeneral/Table/DcaSettingCondition/ValueListener.php

View workflow job for this annotation

GitHub Actions / PHP: 8.3 Contao: ~4.13.0

InvalidArgument: Argument 1 of ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface::setCurrentLanguage expects string, but ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationInterface|null provided (reported by psalm)
}
}
$optEv = new GetPropertyOptionsEvent($subEnv, $dataProvider->getEmptyModel());
$optEv->setPropertyName($attribute->getColName());
$dispatcher = $subEnv->getEventDispatcher();
Expand All @@ -244,18 +272,18 @@ private function getOptionsViaDcGeneral($metaModel, $environment, $attribute)
* @param IAttribute $attribute The attribute.
* @param string $language The language to used for the convertion.
*
* @return string The value to be saved.
* @return string|null The value to be saved.
*/
private function aliasToId(string $alias, IAttribute $attribute, string $language): string
private function aliasToId(string $alias, IAttribute $attribute, string $language): ?string
{
if ($attribute instanceof IAliasConverter) {
$idForAlias = $attribute->getIdForAlias(\substr($alias, 6), $language);
$idForAlias = $attribute->getIdForAlias(substr($alias, 6), $language);
if ($idForAlias !== null) {
return $idForAlias;
}
}

return \substr($alias, 6);
return substr($alias, 6);
}

/**
Expand All @@ -270,8 +298,9 @@ private function aliasToId(string $alias, IAttribute $attribute, string $languag
*/
private function idToAlias(string $idValue, IAttribute $attribute, string $language): string
{
if (\str_starts_with($idValue, 'value_')) {
$idValue = \substr($idValue, 6);
// FIXME: When can this happen?
if (str_starts_with($idValue, 'value_')) {
$idValue = substr($idValue, 6);
}

if ($attribute instanceof IAliasConverter) {
Expand Down Expand Up @@ -309,6 +338,6 @@ private function extractCurrentLanguageContext(IMetaModel $metaModel): string
}

// Use the current backend language then.
return \str_replace('-', '_', (string) $GLOBALS['TL_LANGUAGE']);
return str_replace('-', '_', (string) $GLOBALS['TL_LANGUAGE']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function handle(ModelToLabelEvent $event)
$event
->setLabel('<div class="field_heading cte_type %s"><strong>%s</strong> <em>[%s%s]</em></div>
<div class="field_type block">
%s<strong>%s</strong>
%s <strong>%s</strong>
</div>')
->setArgs([
$model->getProperty('enabled') ? 'published' : 'unpublished',
Expand Down
3 changes: 2 additions & 1 deletion src/CoreBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ metamodels.rendersetting.add_all:
defaults: { _controller: metamodels.controller.rendersetting.add_all, _scope: backend, _token_check: true }

metamodels.configuration:
path: /contao/metamodels
path: /contao/metamodels/{tableName}
defaults:
_controller: MetaModels\CoreBundle\Controller\Backend\ConfigurationController
_scope: backend
_dcg_referer_update: true
_token_check: true
tableName: ~

metamodels.metamodel:
path: /contao/metamodel/{tableName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@
<source>NOT</source>
</trans-unit>
<trans-unit id="typedesc._default_" resname="typedesc._default_">
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - for attribute &lt;em&gt;%attribute%&lt;/em&gt; (Parameter: %value%)%comment%
</source>
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - for attribute "%attribute%" (Parameter: "%value%")%comment%</source>
</trans-unit>
<trans-unit id="typedesc.conditionor" resname="typedesc.conditionor">
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - any sub conditions must be fulfilled%comment%</source>
Expand All @@ -179,7 +178,13 @@
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - invert the result of the contained condition%comment%</source>
</trans-unit>
<trans-unit id="typedesc.conditionpropertyvalueis" resname="typedesc.conditionpropertyvalueis">
<source>%icon% &lt;strong&gt;%attribute%&lt;/strong&gt; value is %value%%comment%</source>
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - for attribute "%attribute%" (Parameter: "%value%")%comment%</source>
</trans-unit>
<trans-unit id="typedesc.conditionpropertycontainanyof" resname="typedesc.conditionpropertycontainanyof">
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - for attribute "%attribute%" (Parameter(s): "%value%")%comment%</source>
</trans-unit>
<trans-unit id="typedesc.conditionpropertyvisible" resname="typedesc.conditionpropertyvisible">
<source>%icon% &lt;strong&gt;%name%&lt;/strong&gt; - for attribute or legend "%attribute%"%comment%</source>
</trans-unit>
<trans-unit id="deleteConfirm" resname="deleteConfirm">
<source>Do you really want to delete setting ID %id%?</source>
Expand Down

0 comments on commit 5ea99b7

Please sign in to comment.