Score:1

What is the correct way to set language used by a modal form?

us flag

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?

Jaypan avatar
de flag
How are you loading the modal, and how is language negotiation handled on your site? If language is by URL or path prefix, you'll need to make sure the AJAX call is to the correct URL or path prefix, to load the correct language. If you show some code, and answer the first questions in my comment, it will be easier to provide support.
liquidcms avatar
us flag
@Jaypan, yes.. that was it.. Doahh!! Thank you.
Jaypan avatar
de flag
I've added my comment as a response. If you could mark it please and thank you :)
Score:1
de flag

If language is by URL or path prefix, you'll need to make sure the AJAX call is to the correct URL or path prefix, to load the correct language.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.