Skip to content

Commit

Permalink
Add current locale during form create or edit
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Oct 8, 2024
1 parent 168a4c3 commit 6b3c0c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
6 changes: 4 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -162,6 +162,8 @@ global.MonsieurBizRichEditorManager = class {
}
}

this.locale = locale;

let initInterfaceCallback = function () {
this.initInterface();
}.bind(this);
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions src/Resources/views/Admin/app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 6b3c0c0

Please sign in to comment.