Skip to content

Commit

Permalink
fix(admin): form locale and on jmn component (ems-project#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei authored Oct 18, 2023
1 parent 5680aab commit d9b266b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ private function createFormItem(JsonMenuNestedConfig $config, JsonMenuNestedNode
'field_type' => $node->getFieldType(),
'content_type' => $config->revision->giveContentType(),
'item' => $item,
'locale' => $config->locale,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class JsonMenuNestedConfig implements ConfigInterface
{
public ?string $template;
public ?string $locale;
/** @var array<string, mixed> */
public array $context = [];
public ?string $contextBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(private readonly RevisionService $revisionService)
* context: array<string, mixed>,
* context_block: ?string,
* template: ?string,
* locale: ?string,
* columns: JsonMenuNestedColumn[]
* } $options
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions EMS/core-bundle/src/Form/DataField/DataFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -190,7 +192,7 @@ public function reverseViewTransform($data, FieldType $fieldType): DataField
/**
* @return array<string, string>
*/
private function getChoices(FieldType $fieldType): array
private function getChoices(FieldType $fieldType, ?string $locale = null): array
{
$choices = [];
$labels = $fieldType->getDisplayOption(self::LABELS_DISPLAY_OPTION) ?? '';
Expand All @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions EMS/core-bundle/src/Form/Extension/LocaleFormExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace EMS\CoreBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LocaleFormExtension extends AbstractTypeExtension
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions EMS/core-bundle/src/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -457,5 +457,9 @@
<argument type="service" id="ems.service.data" />
<tag name="form.type"/>
</service>

<service id="emsco.form_extension.locale_form_extension" class="EMS\CoreBundle\Form\Extension\LocaleFormExtension">
<tag name="form.type_extension"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
<p class="text-red">{{ 'view.data.json-menu-nested-json-preview.field-type-not-found'|trans }}</p>
<pre class="ems-code-editor" data-language="ace/mode/json" data-them="ace/theme/chrome">
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %}
Expand All @@ -16,6 +16,7 @@
'compare': compare,
'compareRawData': compareRawData,
'path': path|merge([dataField.fieldType.name]),
'locale': locale
}
%}
{{ block(blockName) }}
Expand Down Expand Up @@ -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 %}


Expand Down Expand Up @@ -296,7 +297,7 @@
<div class="row">
{% for grandchild in child.getChildren %}
<div class="{% if grandchild.fieldType.displayOptions.class is defined and grandchild.fieldType.displayOptions.class %}{{ grandchild.fieldType.displayOptions.class }}{%else%}col-md-12{% endif %}">
{{ _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) }}
</div>
{% if grandchild.fieldType.displayOptions.lastOfRow is defined and grandchild.fieldType.displayOptions.lastOfRow %}
<div class="clearfix"></div>
Expand All @@ -315,7 +316,7 @@
{% block EMS_CoreBundle_Form_DataField_HolderFieldType %}
{% for child in dataField.getChildren|filter(c => false == c.fieldType.deleted) %}
<div class="{{ child.fieldType.displayOptions.class|default('col-md-12') }}">
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path) }}
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path, locale) }}
</div>
{% if child.fieldType.displayOptions.lastOfRow|default(false) %}
<div class="clearfix"></div>
Expand Down Expand Up @@ -350,7 +351,7 @@
<div class="row">
{% for child in dataField.getChildren|filter(c => false == c.fieldType.deleted) %}
<div class="{% if child.fieldType.displayOptions.class is defined and child.fieldType.displayOptions.class %}{{ child.fieldType.displayOptions.class }}{%else%}col-md-12{% endif %}">
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path) }}
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path, locale) }}
</div>
{% if child.fieldType.displayOptions.lastOfRow is defined and child.fieldType.displayOptions.lastOfRow %}
<div class="clearfix"></div>
Expand Down Expand Up @@ -389,7 +390,7 @@

{% for child in dataField.getChildren|filter(child => not child.fieldType.deleted) %}
<div class="{% if child.fieldType.displayOptions.class is defined and child.fieldType.displayOptions.class %}{{ child.fieldType.displayOptions.class }}{%else%}col-md-12{% endif %}">
{{ _self.renderDataFieldBlock(child, source, compare, compareSubRawData, '', path) }}
{{ _self.renderDataFieldBlock(child, source, compare, compareSubRawData, '', path, locale) }}
</div>
{% if child.fieldType.displayOptions.lastOfRow is defined and child.fieldType.displayOptions.lastOfRow %}
<div class="clearfix"></div>
Expand All @@ -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 %}



<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
{% for value in values|default([]) %}
Expand All @@ -432,7 +436,7 @@
{% set childData = source[value]|default([]) %}
{% set childCompareData = compareRawData[value]|default([]) %}
{% for grandchild in dataField.getChildren.get(value).getChildren|default([]) %}
{{ _self.renderDataFieldBlock(grandchild, childData, compare, childCompareData, '', path|merge([loop.index0])) }}
{{ _self.renderDataFieldBlock(grandchild, childData, compare, childCompareData, '', path|merge([loop.index0]), locale) }}
{% endfor %}
</div>
{% endfor %}
Expand Down Expand Up @@ -508,15 +512,15 @@
{% 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 %}
</div>
{% endwith %}
{% set counter = counter + 1 %}
{% endfor %}
{% elseif 'EMS\\CoreBundle\\Form\\DataField\\ContainerFieldType' == child.fieldType.type|default(null) %}
<div class="tab-pane{% if counter == 1%} active{% endif %}" id="item_{{ path|join('_')~'_'~counter }}__tab">
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData) }}
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', [], locale) }}
</div>
{% set counter = counter + 1 %}
{% else %}
Expand All @@ -526,7 +530,7 @@
}
%}
<div class="tab-pane{% if counter == 1%} active{% endif %}" id="item_{{ path|join('_')~'_'~counter }}__tab">
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path|merge([counter])) }}
{{ _self.renderDataFieldBlock(child, source, compare, compareRawData, '', path|merge([counter]), locale) }}
</div>
{% endwith %}
{% set counter = counter + 1 %}
Expand Down Expand Up @@ -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 %}
<div class="panel panel-warning">
<div class="panel-heading">
Expand Down

0 comments on commit d9b266b

Please sign in to comment.