Skip to content

Commit 63ad8a1

Browse files
authored
Merge pull request #244 from maximehuran/feature/add-locale
2 parents bd47fbf + 59b1d00 commit 63ad8a1

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

assets/js/app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ global.MonsieurBizRichEditorManager = class {
146146
/**
147147
*
148148
*/
149-
constructor(config, tags) {
149+
constructor(config, tags, locale) {
150150
config.input.setAttribute('data-rich-editor-uid', config.uid);
151151

152152
this.config = config;
@@ -162,6 +162,8 @@ global.MonsieurBizRichEditorManager = class {
162162
}
163163
}
164164

165+
this.locale = locale;
166+
165167
let initInterfaceCallback = function () {
166168
this.initInterface();
167169
}.bind(this);
@@ -603,7 +605,7 @@ global.MonsieurBizRichEditorManager = class {
603605
loadUiElementCreateForm(element, callback) {
604606
let req = new XMLHttpRequest();
605607
req.onload = callback;
606-
let url = this.config.createElementFormUrl;
608+
let url = this.config.createElementFormUrl + '?locale=' + this.locale;
607609
req.open("get", url.replace('__CODE__', element.code), true);
608610
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
609611
req.send();
@@ -616,7 +618,7 @@ global.MonsieurBizRichEditorManager = class {
616618
req.open("post", url.replace('__CODE__', element.code), true);
617619
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
618620
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
619-
req.send(new URLSearchParams({data: JSON.stringify(element.data)}).toString());
621+
req.send(new URLSearchParams({data: JSON.stringify(element.data), locale: this.locale}).toString());
620622
}
621623

622624
submitUiElementForm(form, callback) {

src/Controller/FormController.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(RegistryInterface $uiElementRegistry)
4747
*
4848
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
4949
*/
50-
public function viewAction(Request $request, string $code): Response
50+
public function viewAction(Request $request, SwitchAdminLocaleInterface $switchAdminLocale, string $code): Response
5151
{
5252
// Find UI Element from type
5353
try {
@@ -59,6 +59,14 @@ public function viewAction(Request $request, string $code): Response
5959
// Check data in post
6060
$data = [];
6161
$isEdition = $request->isMethod('post');
62+
$locale = $request->get('locale');
63+
64+
// if we have a locale value in the post data, we change the current
65+
// admin locale to make the ui elements in the correct version.
66+
if (($locale = $request->get('locale')) && \is_string($locale)) {
67+
$switchAdminLocale->switchLocale($locale);
68+
}
69+
6270
if ($isEdition && ($data = $request->get('data'))) {
6371
if (!\is_string($data)) {
6472
throw $this->createNotFoundException();
@@ -70,10 +78,12 @@ public function viewAction(Request $request, string $code): Response
7078
}
7179

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

7584
return new JsonResponse([
7685
'code' => $uiElement->getCode(),
86+
'locale' => $locale,
7787
'form_html' => $this->renderView($uiElement->getAdminFormTemplate(), [
7888
'form' => $form->createView(),
7989
'uiElement' => $uiElement,

src/Resources/public/js/rich-editor-highlight.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/public/js/rich-editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/views/Admin/app.html.twig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@
180180
181181
let editors = document.querySelectorAll('[data-component="rich-editor"]');
182182
let uielements = {{ monsieurbiz_richeditor_list_elements() }};
183+
let fallBackLocale = '{{ app.request.locale | default(app.request.defaultLocale) | default(sylius_base_locale) | escape('js') }}';
183184
184-
function setupRichEditor(editor, tags) {
185+
function setupRichEditor(editor, tags, locale) {
185186
let config = new MonsieurBizRichEditorConfig(
186187
editor,
187188
uielements,
@@ -200,12 +201,13 @@
200201
'{{ 'monsieurbiz_richeditor_plugin.error.ajax_error'|trans|e('js') }}',
201202
'{{ 'monsieurbiz_richeditor_plugin.error.unallowed_uielement'|trans|e('js') }}'
202203
);
203-
editor.manager = new MonsieurBizRichEditorManager(config, tags);
204+
editor.manager = new MonsieurBizRichEditorManager(config, tags, locale);
204205
}
205206
206207
editors.forEach(function (editor) {
207208
let tags = editor.dataset.tags.length === 0 ? [] : editor.dataset.tags.split(',')
208-
setupRichEditor(editor, tags);
209+
let locale = editor.dataset.locale ? editor.dataset.locale : fallBackLocale;
210+
setupRichEditor(editor, tags, locale);
209211
});
210212
211213
// JQuery event triggered by @SyliusUiBundle/Resources/private/js/sylius-form-collection.js
@@ -222,7 +224,7 @@
222224
let editors = document.querySelectorAll('[data-component="rich-editor"]:not([data-rich-editor-uid])');
223225
let manager = e.detail.manager;
224226
editors.forEach(function (editor) {
225-
setupRichEditor(editor, manager.tags); // Retrieve tags from the parent manager
227+
setupRichEditor(editor, manager.tags, manager.locale); // Retrieve tags and locale from the parent manager
226228
});
227229
228230
let form = e.detail.form;

0 commit comments

Comments
 (0)