From 6b3c0c03001ad1e1437418947f787a8dc2e3aed3 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 8 Oct 2024 12:09:22 +0200 Subject: [PATCH] Add current locale during form create or edit --- assets/js/app.js | 6 ++++-- src/Controller/FormController.php | 9 ++++++++- src/Resources/views/Admin/app.html.twig | 9 +++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 9158d217..53b019a7 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -146,7 +146,7 @@ global.MonsieurBizRichEditorManager = class { /** * */ - constructor(config, tags) { + constructor(config, tags, locale) { config.input.setAttribute('data-rich-editor-uid', config.uid); this.config = config; @@ -162,6 +162,8 @@ global.MonsieurBizRichEditorManager = class { } } + this.locale = locale; + let initInterfaceCallback = function () { this.initInterface(); }.bind(this); @@ -616,7 +618,7 @@ global.MonsieurBizRichEditorManager = class { req.open("post", url.replace('__CODE__', element.code), true); req.setRequestHeader("X-Requested-With", "XMLHttpRequest"); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - req.send(new URLSearchParams({data: JSON.stringify(element.data)}).toString()); + req.send(new URLSearchParams({data: JSON.stringify(element.data), locale: this.locale}).toString()); } submitUiElementForm(form, callback) { diff --git a/src/Controller/FormController.php b/src/Controller/FormController.php index 54f91fbc..2eef6b80 100644 --- a/src/Controller/FormController.php +++ b/src/Controller/FormController.php @@ -47,7 +47,7 @@ public function __construct(RegistryInterface $uiElementRegistry) * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function viewAction(Request $request, string $code): Response + public function viewAction(Request $request, SwitchAdminLocaleInterface $switchAdminLocale, string $code): Response { // Find UI Element from type try { @@ -59,6 +59,13 @@ public function viewAction(Request $request, string $code): Response // Check data in post $data = []; $isEdition = $request->isMethod('post'); + $locale = $request->get('locale'); + // if we have a locale value in the post data, we change the current + // admin locale to make the ui elements in the correct version. + if (($locale = $request->get('locale')) && \is_string($locale)) { + $switchAdminLocale->switchLocale($locale); + } + if ($isEdition && ($data = $request->get('data'))) { if (!\is_string($data)) { throw $this->createNotFoundException(); diff --git a/src/Resources/views/Admin/app.html.twig b/src/Resources/views/Admin/app.html.twig index 756a7ad3..8d812d63 100644 --- a/src/Resources/views/Admin/app.html.twig +++ b/src/Resources/views/Admin/app.html.twig @@ -181,7 +181,7 @@ let editors = document.querySelectorAll('[data-component="rich-editor"]'); let uielements = {{ monsieurbiz_richeditor_list_elements() }}; - function setupRichEditor(editor, tags) { + function setupRichEditor(editor, tags, locale) { let config = new MonsieurBizRichEditorConfig( editor, uielements, @@ -200,12 +200,13 @@ '{{ 'monsieurbiz_richeditor_plugin.error.ajax_error'|trans|e('js') }}', '{{ 'monsieurbiz_richeditor_plugin.error.unallowed_uielement'|trans|e('js') }}' ); - editor.manager = new MonsieurBizRichEditorManager(config, tags); + editor.manager = new MonsieurBizRichEditorManager(config, tags, locale); } editors.forEach(function (editor) { let tags = editor.dataset.tags.length === 0 ? [] : editor.dataset.tags.split(',') - setupRichEditor(editor, tags); + let locale = editor.dataset.locale; // @TODO add fallback locale on the twig parameter locale ? + setupRichEditor(editor, tags, locale); }); // JQuery event triggered by @SyliusUiBundle/Resources/private/js/sylius-form-collection.js @@ -222,7 +223,7 @@ let editors = document.querySelectorAll('[data-component="rich-editor"]:not([data-rich-editor-uid])'); let manager = e.detail.manager; editors.forEach(function (editor) { - setupRichEditor(editor, manager.tags); // Retrieve tags from the parent manager + setupRichEditor(editor, manager.tags, manager.locale); // Retrieve tags and locale from the parent manager }); let form = e.detail.form;