Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current locale during form create or edit #244

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 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 @@ -603,7 +605,7 @@ global.MonsieurBizRichEditorManager = class {
loadUiElementCreateForm(element, callback) {
let req = new XMLHttpRequest();
req.onload = callback;
let url = this.config.createElementFormUrl;
let url = this.config.createElementFormUrl + '?locale=' + this.locale;
req.open("get", url.replace('__CODE__', element.code), true);
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
req.send();
Expand All @@ -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
14 changes: 12 additions & 2 deletions 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,14 @@ 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 All @@ -70,10 +78,12 @@ public function viewAction(Request $request, string $code): Response
}

// 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
2 changes: 1 addition & 1 deletion src/Resources/public/js/rich-editor-highlight.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Resources/public/js/rich-editor.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/Resources/views/Admin/app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@

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) {
function setupRichEditor(editor, tags, locale) {
let config = new MonsieurBizRichEditorConfig(
editor,
uielements,
Expand All @@ -200,12 +201,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 ? editor.dataset.locale : fallBackLocale;
setupRichEditor(editor, tags, locale);
});

// JQuery event triggered by @SyliusUiBundle/Resources/private/js/sylius-form-collection.js
Expand All @@ -222,7 +224,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
Loading