From 2e7c07660fc72557dd8b707d2fbea925e9b4f147 Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Tue, 21 Jan 2025 15:10:59 +0100 Subject: [PATCH] feat: add current locale on submit to display preview in right version --- src/Controller/FormController.php | 10 +++++++++- src/Resources/views/Admin/form.html.twig | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Controller/FormController.php b/src/Controller/FormController.php index f4cbc6dd..a84b9365 100644 --- a/src/Controller/FormController.php +++ b/src/Controller/FormController.php @@ -151,8 +151,10 @@ public function renderElementsAction(Request $request, SwitchAdminLocaleInterfac /** * Validate submitted data and return an UI Element JSON if everything is OK. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function submitAction(Request $request, FileUploaderInterface $fileUploader, string $code, bool $isEdition): Response + public function submitAction(Request $request, FileUploaderInterface $fileUploader, SwitchAdminLocaleInterface $switchAdminLocale, string $code, bool $isEdition): Response { // Find UI Element from type try { @@ -161,6 +163,12 @@ public function submitAction(Request $request, FileUploaderInterface $fileUpload throw $this->createNotFoundException($exception->getMessage()); } + // 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); + } + // Create and validate form $form = $this->createForm($uiElement->getFormClass(), null, $this->getFormOptions($uiElement)); $form->handleRequest($request); diff --git a/src/Resources/views/Admin/form.html.twig b/src/Resources/views/Admin/form.html.twig index 05cac578..6c1c7eae 100644 --- a/src/Resources/views/Admin/form.html.twig +++ b/src/Resources/views/Admin/form.html.twig @@ -32,5 +32,6 @@
- {{ form(form, {'action': url('monsieurbiz_richeditor_admin_form_submit', {'code': uiElement.code, 'isEdition': isEdition})}) }} + {% set locale = form.vars.attr['data-locale']|default(sylius.localeCode) %} + {{ form(form, {'action': url('monsieurbiz_richeditor_admin_form_submit', {'code': uiElement.code, 'isEdition': isEdition, 'locale': locale})}) }}