I have a button linked through a controller to open a custom form in a modal:
The controller callback is:
/**
* Callback for opening the modal form.
*/
public function openShareLinkModalForm() {
$response = new AjaxResponse();
// Get the modal form using the form builder.
$modal_form = $this->formBuilder->getForm('Drupal\ssc_wsl\Form\ShareLinkModalForm');
// Add an AJAX command to open a modal dialog with the form as the content.
$response->addCommand(new OpenModalDialogCommand($this->t('Share the link to this page.'), $modal_form, ['width' => '1200']));
return $response;
}
}
this works fine except that when my button is on a FR page, the modal still opens in EN.
From a trick I have used elsewhere, I tried adding this code in the callback just before the getForm() call:
$language_manager = \Drupal::languageManager();
$langcode = $language_manager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$language = $language_manager->getLanguage($langcode);
$language_manager->setConfigOverrideLanguage($language);
This has no impact. The controller callback seems to be basing translation off the value in: $language_manager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) and that value is always the default value set for the site (if I set default site language to FR; then the modal only shows in FR).
The question is, is this the proper way to set the language used by the modal?