Skip to content

Commit

Permalink
Set locale in form in dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Oct 8, 2024
1 parent 6124e01 commit d1cf6ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function viewAction(Request $request, SwitchAdminLocaleInterface $switchA
$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)) {
Expand All @@ -77,10 +78,12 @@ public function viewAction(Request $request, SwitchAdminLocaleInterface $switchA
}

// Create form depending on UI Element with data
$form = $this->createForm($uiElement->getFormClass(), $data, $this->getFormOptions($uiElement));
$formOptions = array_merge(['attr' => ['data-locale' => $locale]], $this->getFormOptions($uiElement));
$form = $this->createForm($uiElement->getFormClass(), $data, $formOptions);

return new JsonResponse([
'code' => $uiElement->getCode(),
'locale' => $locale,
'form_html' => $this->renderView($uiElement->getAdminFormTemplate(), [
'form' => $form->createView(),
'uiElement' => $uiElement,
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/views/Admin/app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
let editors = document.querySelectorAll('[data-component="rich-editor"]');
let uielements = {{ monsieurbiz_richeditor_list_elements() }};
let fallBackLocale = '{{ app.request.locale | default(app.request.defaultLocale) | default(sylius_base_locale) | escape('js') }}';
function setupRichEditor(editor, tags, locale) {
let config = new MonsieurBizRichEditorConfig(
Expand All @@ -205,7 +206,7 @@
editors.forEach(function (editor) {
let tags = editor.dataset.tags.length === 0 ? [] : editor.dataset.tags.split(',')
let locale = editor.dataset.locale | default(app.request.locale) | default(app.request.defaultLocale) | default(sylius_base_locale);
let locale = editor.dataset.locale ? editor.dataset.locale : fallBackLocale;
setupRichEditor(editor, tags, locale);
});
Expand Down

0 comments on commit d1cf6ef

Please sign in to comment.