From d9b266bd205c619b9934859d1f56d65134750c74 Mon Sep 17 00:00:00 2001 From: David mattei Date: Wed, 18 Oct 2023 09:01:46 +0200 Subject: [PATCH] fix(admin): form locale and on jmn component (#642) --- .../Component/JsonMenuNestedController.php | 1 + .../Config/JsonMenuNestedConfig.php | 1 + .../Config/JsonMenuNestedConfigFactory.php | 3 ++ .../DataField/CollectionItemFieldType.php | 1 + .../src/Form/DataField/DataFieldType.php | 1 + .../MultiplexedTabContainerFieldType.php | 11 ++++-- .../Form/Extension/LocaleFormExtension.php | 29 ++++++++++++++++ .../Form/Form/RevisionJsonMenuNestedType.php | 1 + EMS/core-bundle/src/Resources/config/form.xml | 4 +++ .../components/json_menu_nested/template.twig | 2 +- .../views/macros/data-field-type.html.twig | 34 +++++++++++-------- 11 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 EMS/core-bundle/src/Form/Extension/LocaleFormExtension.php diff --git a/EMS/core-bundle/src/Controller/Component/JsonMenuNestedController.php b/EMS/core-bundle/src/Controller/Component/JsonMenuNestedController.php index 5b9460af3..d3cfd67ae 100644 --- a/EMS/core-bundle/src/Controller/Component/JsonMenuNestedController.php +++ b/EMS/core-bundle/src/Controller/Component/JsonMenuNestedController.php @@ -222,6 +222,7 @@ private function createFormItem(JsonMenuNestedConfig $config, JsonMenuNestedNode 'field_type' => $node->getFieldType(), 'content_type' => $config->revision->giveContentType(), 'item' => $item, + 'locale' => $config->locale, ]); } diff --git a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfig.php b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfig.php index ccbc2ee3b..3358e03b0 100644 --- a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfig.php +++ b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfig.php @@ -11,6 +11,7 @@ class JsonMenuNestedConfig implements ConfigInterface { public ?string $template; + public ?string $locale; /** @var array */ public array $context = []; public ?string $contextBlock; diff --git a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfigFactory.php b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfigFactory.php index ca73938f2..1be845005 100644 --- a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfigFactory.php +++ b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Config/JsonMenuNestedConfigFactory.php @@ -27,6 +27,7 @@ public function __construct(private readonly RevisionService $revisionService) * context: array, * context_block: ?string, * template: ?string, + * locale: ?string, * columns: JsonMenuNestedColumn[] * } $options */ @@ -56,6 +57,7 @@ protected function create(string $hash, array $options): JsonMenuNestedConfig $config->contextBlock = $options['context_block']; $config->template = $options['template']; $config->columns = $options['columns']; + $config->locale = $options['locale']; return $config; } @@ -71,6 +73,7 @@ protected function resolveOptions(array $options): array 'context' => [], 'context_block' => null, 'template' => null, + 'locale' => null, ]) ->setNormalizer('ems_link', function (Options $options, EMSLink|string $value): EMSLink { return \is_string($value) ? EMSLink::fromText($value) : $value; diff --git a/EMS/core-bundle/src/Form/DataField/CollectionItemFieldType.php b/EMS/core-bundle/src/Form/DataField/CollectionItemFieldType.php index 5729e21bf..dd350525f 100644 --- a/EMS/core-bundle/src/Form/DataField/CollectionItemFieldType.php +++ b/EMS/core-bundle/src/Form/DataField/CollectionItemFieldType.php @@ -72,6 +72,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'raw_data' => $options['raw_data'], 'disabled_fields' => $options['disabled_fields'], 'referrer-ems-id' => $options['referrer-ems-id'], + 'locale' => $options['locale'], ], $fieldType->getDisplayOptions()); $builder->add($fieldType->getName(), $fieldType->getType(), $options); $builder->get($fieldType->getName()) diff --git a/EMS/core-bundle/src/Form/DataField/DataFieldType.php b/EMS/core-bundle/src/Form/DataField/DataFieldType.php index 2447c87ba..393a5df21 100644 --- a/EMS/core-bundle/src/Form/DataField/DataFieldType.php +++ b/EMS/core-bundle/src/Form/DataField/DataFieldType.php @@ -504,6 +504,7 @@ protected function buildChildForm(FieldType $fieldType, mixed $options, FormBuil 'raw_data' => $options['raw_data'], 'disabled_fields' => $options['disabled_fields'], 'referrer-ems-id' => $options['referrer-ems-id'], + 'locale' => $options['locale'], ], $fieldType->getDisplayOptions()); $builder->add($fieldType->getName(), $fieldType->getType(), $options); diff --git a/EMS/core-bundle/src/Form/DataField/MultiplexedTabContainerFieldType.php b/EMS/core-bundle/src/Form/DataField/MultiplexedTabContainerFieldType.php index b6f705a55..7756c77f0 100644 --- a/EMS/core-bundle/src/Form/DataField/MultiplexedTabContainerFieldType.php +++ b/EMS/core-bundle/src/Form/DataField/MultiplexedTabContainerFieldType.php @@ -135,7 +135,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void if (!$fieldType instanceof FieldType) { throw new \RuntimeException('Unexpected FieldType type'); } - foreach ($this->getChoices($fieldType) as $label => $value) { + + foreach ($this->getChoices($fieldType, $options['locale']) as $label => $value) { $builder->add($value, ContainerFieldType::class, [ 'metadata' => $fieldType, 'label' => $label, @@ -145,6 +146,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'raw_data' => $options['raw_data'], 'disabled_fields' => $options['disabled_fields'], 'referrer-ems-id' => $options['referrer-ems-id'], + 'locale' => $value, ]); $builder->get($value) @@ -190,7 +192,7 @@ public function reverseViewTransform($data, FieldType $fieldType): DataField /** * @return array */ - private function getChoices(FieldType $fieldType): array + private function getChoices(FieldType $fieldType, ?string $locale = null): array { $choices = []; $labels = $fieldType->getDisplayOption(self::LABELS_DISPLAY_OPTION) ?? ''; @@ -203,6 +205,11 @@ private function getChoices(FieldType $fieldType): array $choices[$value] = $labels[$counter++] ?? $value; } } + + if ($locale && isset($choices[$locale])) { + $choices = [...[$locale => $choices[$locale]], ...\array_filter($choices, static fn ($l) => $l !== $locale)]; + } + $choices = \array_flip($choices); $localePreferredFirst = $fieldType->getDisplayOption(self::LOCALE_PREFERRED_FIRST_DISPLAY_OPTION); diff --git a/EMS/core-bundle/src/Form/Extension/LocaleFormExtension.php b/EMS/core-bundle/src/Form/Extension/LocaleFormExtension.php new file mode 100644 index 000000000..9211abb62 --- /dev/null +++ b/EMS/core-bundle/src/Form/Extension/LocaleFormExtension.php @@ -0,0 +1,29 @@ +setDefaults(['locale' => null]); + } + + public function buildView(FormView $view, FormInterface $form, array $options): void + { + $view->vars['locale'] = $options['locale']; + } + + public static function getExtendedTypes(): iterable + { + return [FormType::class]; + } +} diff --git a/EMS/core-bundle/src/Form/Form/RevisionJsonMenuNestedType.php b/EMS/core-bundle/src/Form/Form/RevisionJsonMenuNestedType.php index c4bcd49dc..c3959158e 100644 --- a/EMS/core-bundle/src/Form/Form/RevisionJsonMenuNestedType.php +++ b/EMS/core-bundle/src/Form/Form/RevisionJsonMenuNestedType.php @@ -48,6 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'metadata' => $fieldType, 'error_bubbling' => false, 'disabled_fields' => $contentType->getDisabledDataFields(), + 'locale' => $options['locale'], ]); if ($item) { diff --git a/EMS/core-bundle/src/Resources/config/form.xml b/EMS/core-bundle/src/Resources/config/form.xml index 3d931b534..34ab7fb64 100644 --- a/EMS/core-bundle/src/Resources/config/form.xml +++ b/EMS/core-bundle/src/Resources/config/form.xml @@ -457,5 +457,9 @@ + + + + diff --git a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig index 965797a08..fadfe468f 100644 --- a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig +++ b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig @@ -188,7 +188,7 @@ {%- block jmn_modal_view -%} {%- if dataFields -%} {%- import "@EMSCore/macros/data-field-type.html.twig" as macros -%} - {{ macros.renderDataField(dataFields, rawData, false, []) }} + {{ macros.renderDataField(dataFields, rawData, false, [], config.locale) }} {%- else -%}

{{ 'view.data.json-menu-nested-json-preview.field-type-not-found'|trans }}

diff --git a/EMS/core-bundle/src/Resources/views/macros/data-field-type.html.twig b/EMS/core-bundle/src/Resources/views/macros/data-field-type.html.twig
index f7fb24d79..10af3901e 100644
--- a/EMS/core-bundle/src/Resources/views/macros/data-field-type.html.twig
+++ b/EMS/core-bundle/src/Resources/views/macros/data-field-type.html.twig
@@ -1,11 +1,11 @@
 {% trans_default_domain 'EMSCoreBundle' %}
 
-{% macro renderDataField(dataField, source, compare, compareRawData) %}
-	{{ _self.renderDataFieldBlock(dataField, source, compare, compareRawData) }}
+{% macro renderDataField(dataField, source, compare, compareRawData, locale = null) %}
+	{{ _self.renderDataFieldBlock(dataField, source, compare, compareRawData, '', [], locale) }}
 	{{ _self.messages(dataField) }}
 {% endmacro %}
 
-{% macro renderDataFieldBlock(dataField, source, compare, compareRawData, blockName = '', path = []) %}
+{% macro renderDataFieldBlock(dataField, source, compare, compareRawData, blockName = '', path = [], locale = null) %}
 	{% if blockName is same as('') %}
 		{% set blockName = dataField.fieldType.type|replace({'\\': '_'}) %}
 	{% endif %}
@@ -16,6 +16,7 @@
 		'compare': compare,
 		'compareRawData': compareRawData,
 		'path': path|merge([dataField.fieldType.name]),
+        'locale': locale
 		}
 	%}
 		{{ block(blockName) }}
@@ -43,7 +44,7 @@
 {% endblock %}
 
 {% block EMS_CoreBundle_Form_DataField_IndexedAssetFieldType %}
-	{{ _self.renderDataFieldBlock(dataField, source, compare, compareRawData, 'EMS_CoreBundle_Form_DataField_AssetFieldType', path) }}
+	{{ _self.renderDataFieldBlock(dataField, source, compare, compareRawData, 'EMS_CoreBundle_Form_DataField_AssetFieldType', path, locale) }}
 {% endblock %}
 
 
@@ -296,7 +297,7 @@
 							
{% for grandchild in child.getChildren %}
- {{ _self.renderDataFieldBlock(grandchild, source, compare, (attribute(compareCollectionRawData, idx) is defined?attribute(compareCollectionRawData, idx):null), '', subpath) }} + {{ _self.renderDataFieldBlock(grandchild, source, compare, (attribute(compareCollectionRawData, idx) is defined?attribute(compareCollectionRawData, idx):null), '', subpath, locale) }}
{% if grandchild.fieldType.displayOptions.lastOfRow is defined and grandchild.fieldType.displayOptions.lastOfRow %}
@@ -315,7 +316,7 @@ {% block EMS_CoreBundle_Form_DataField_HolderFieldType %} {% for child in dataField.getChildren|filter(c => false == c.fieldType.deleted) %}
- {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path) }} + {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path, locale) }}
{% if child.fieldType.displayOptions.lastOfRow|default(false) %}
@@ -350,7 +351,7 @@
{% for child in dataField.getChildren|filter(c => false == c.fieldType.deleted) %}
- {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path) }} + {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path, locale) }}
{% if child.fieldType.displayOptions.lastOfRow is defined and child.fieldType.displayOptions.lastOfRow %}
@@ -389,7 +390,7 @@ {% for child in dataField.getChildren|filter(child => not child.fieldType.deleted) %}
- {{ _self.renderDataFieldBlock(child, source, compare, compareSubRawData, '', path) }} + {{ _self.renderDataFieldBlock(child, source, compare, compareSubRawData, '', path, locale) }}
{% if child.fieldType.displayOptions.lastOfRow is defined and child.fieldType.displayOptions.lastOfRow %}
@@ -410,12 +411,15 @@ {% for value in values %} {% set choices = choices|merge({(value): attribute(labels, loop.index0)|default(value)}) %} {% endfor %} + + {% if locale != null and locale in values %} + {% set values = [locale]|merge(values|filter(v => v != locale)) %} + {% endif %} + {% if dataField.fieldType.displayOptions.localePreferredFirst|default(false) and app.user.localePreferred|default(false) and app.user.localePreferred in values %} {% set values = values|sort((a, b) => a == app.user.localePreferred ? -1 : b == app.user.localePreferred ? 1 : 0) %} {% endif %} - - {% endfor %} @@ -508,7 +512,7 @@ {% set childData = source[key]|default([]) %} {% set childCompareData = compareRawData[key]|default([]) %} {% for grandchild in child.getChildren|default([]) %} - {{ _self.renderDataFieldBlock(grandchild, childData, compare, childCompareData, '', path|merge([counter])) }} + {{ _self.renderDataFieldBlock(grandchild, childData, compare, childCompareData, '', path|merge([counter]), locale) }} {% endfor %}
{% endwith %} @@ -516,7 +520,7 @@ {% endfor %} {% elseif 'EMS\\CoreBundle\\Form\\DataField\\ContainerFieldType' == child.fieldType.type|default(null) %}
- {{ _self.renderDataFieldBlock(child, source, compare, compareRawData) }} + {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', [], locale) }}
{% set counter = counter + 1 %} {% else %} @@ -526,7 +530,7 @@ } %}
- {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path|merge([counter])) }} + {{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path|merge([counter]), locale) }}
{% endwith %} {% set counter = counter + 1 %} @@ -1326,7 +1330,7 @@ {% set errorMessage = 'view.macros.data-field-type.form.not-found' %} {% endif %} {% if form is defined and form %} - {{ _self.renderDataFieldBlock(emsco_get_data_field(form), source, compare, compareRawData, '', path) }} + {{ _self.renderDataFieldBlock(emsco_get_data_field(form), source, compare, compareRawData, '', path, locale) }} {% else %}