Wrong subscriber's prefered language for anonymous user when interface language is not default

Created on 17 May 2024, 6 months ago
Updated 19 September 2024, about 2 months ago

Problem/Motivation

The preferred language of anonymous users is NOT set based on the interface language of the page they visit for subscription.

Steps to reproduce

  1. Configure site to have multiple languages, enable interface translation, disable subscription confirmation.
  2. Add newsletter, add newsletter subscription block.
  3. Visit page with newsletter subscription block as anonymous, change page language to other that default, subscibe to newsletter.
  4. As admin user goto admin/people/simplenews page and visit recently created subscriber.
  5. Expect to see NON default website language.

Proposed resolution

There are two ways:

  • Update README.md and remove statement about this feature (introduced here https://git.drupalcode.org/project/simplenews/-/commit/3effad447439012c6...). Then developer needs alter form SubscriptionsBlockForm via hook_form_FORM_ID_alter() and implement a single fix.
  • Fix src/Form/SubscriptionsBlockForm.php
    public function form(array $form, FormStateInterface $form_state) {
      //....
      if (\Drupal::currentUser()->isAnonymous()) {
        $form['langcode']['widget'][0]['value']['#default_value'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
      }
    }
    
  • Remaining tasks

    • [x] Write test to reproduce the problem
    • [x] Discuss/Implement fix
🐛 Bug report
Status

Needs work

Version

4.0

Component

Code

Created by

🇺🇦Ukraine vlad.dancer Kyiv

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @vlad.dancer
  • 🇺🇦Ukraine vlad.dancer Kyiv

    vlad.dancer changed the visibility of the branch 3.x to hidden.

  • Pipeline finished with Failed
    6 months ago
    Total: 393s
    #175375
  • 🇺🇦Ukraine vlad.dancer Kyiv

    Results for the test case:

    // Switch to non default language.
    $this->drupalGet('/es');
    // Subscibe.
    $this->submitForm($edit, 'Subscribe');
    
    $subscriber = $this->getLatestSubscriber();
    // Expect to get non default language
    $this->assertEquals('es', $subscriber->getLangcode(), 'Subscriber prefered language is set to user interface language');
    
  • Pipeline finished with Failed
    6 months ago
    Total: 421s
    #175381
  • Status changed to Needs review 6 months ago
  • 🇺🇦Ukraine vlad.dancer Kyiv

    For the moment developers can set language in the next way:

    /**
     * Implements hook_form_FORM_ID_alter().
     *
     * Set anonymous subscriber's preferred language based on current user interface.
     * Fix Issue # 3447905.
     */
    function mymodule_form_simplenews_subscriptions_block_[NEWSLETTER_ID]_alter(array &$form, FormStateInterface &$form_state): void {
      if (\Drupal::currentUser()->isAnonymous()) {
        array_unshift($form['actions']['submit']['#submit'], 'mymodule_alter_preferred_language');
      }
    }
    
    function mymodule_alter_preferred_language(array &$form, FormStateInterface $form_state): void {
      $form_state->setValue(['langcode', 0, 'value'], \Drupal::languageManager()->getCurrentLanguage()->getId());
    }
    

    Or even add language dropdown:

    function mymodule_form_simplenews_subscriptions_block_[NEWSLETTER_ID]_alter(array &$form, FormStateInterface &$form_state): void {
      if (\Drupal::currentUser()->isAnonymous()) {
        array_unshift($form['actions']['submit']['#submit'], 'mymodule_alter_prefered_language');
        $form['lang'] = [
          '#type' => 'language_select',
          '#title' => t('Preferred language'),
          '#default_value' => \Drupal::languageManager()->getCurrentLanguage()->getId(),
          '#languages' => LanguageInterface::STATE_CONFIGURABLE,
        ];
      }
    }
    
    function mymodule_alter_prefered_language(array &$form, FormStateInterface $form_state): void {
      $selectedLang = $form_state->getValue('lang');
      $form_state->setValue(['langcode', 0, 'value'], $selectedLang ?? \Drupal::languageManager()->getCurrentLanguage()->getId());
    }
    
  • Issue was unassigned.
  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 336s
    #219071
  • 🇮🇳India Aditiup

    Hello sir,
    For the issue #3447905 I recommend the following alterations to be made

    Wrong subscriber's prefered language for anonymous user when interface language is not default

    Changes to be made:

    1) Changes in SubscriptionsBlockForm.php
    form Method Modification:

    Inside the form method, check if the current user is anonymous using \Drupal::currentUser()->isAnonymous().
    If the user is anonymous, set the default value of the langcode field to the current interface language using \Drupal::languageManager()->getCurrentLanguage()->getId().

    2) Changes in simplenews.module

    -simplenews_form_alter Hook Implementation:

    This function alters forms based on their ID. It checks the form ID and calls specific functions to modify the forms.
    Newly Added: simplenews_form_alter function is added to handle different form alterations in a centralized manner.

    -simplenews_form_user_register_form_alter:

    Adds subscription options to the user registration form.
    Previously Provided: This function was given earlier and is now included in the simplenews.module.

    -simplenews_user_profile_form_submit:

    Handles the submission of the user registration form, processing newsletter subscriptions.
    Previously Provided: This function was given earlier and is now included in the simplenews.module.

    -simplenews_form_simplenews_subscriptions_block_alter:

    Ensures anonymous subscribers get the correct language based on the current user interface language.
    Newly Added: This function was added to fix the issue with the wrong preferred language for anonymous users.

  • 🇺🇦Ukraine vlad.dancer Kyiv

    @Aditiup 16.92MB patch with over 234053 lines changed, is it purposely?

    Regarding last commit in MR54 with +117/-168 changes in simplenews.module. These changes will never be reviewed/accepted at once.

  • 🇹🇳Tunisia Ahmed Aziz ABBASSI

    @vlad.dancer As her mentor for GSoC (Google Summer of Code) 2024, I would like to dedicate time this weekend to review and test her work. If you have any other suggestions or ideas, I would be happy to hear them.

  • 🇹🇳Tunisia Ahmed Aziz ABBASSI

    I was busy last weekend by work and family plans. I will dedicate this weekend for testing @Aditiup work.

  • Status changed to Needs work about 2 months ago
  • 🇬🇧United Kingdom adamps

    Unfortunately I can't understand the last commit added in #9. It makes many changes that don't seem related to this issue.

    I suggest we go back to the previous commit which basically looks good but there were some test failures.

  • First commit to issue fork.
  • Pipeline finished with Success
    about 2 months ago
    Total: 443s
    #294992
  • MR's branch reset to last stable commits and rebased on to 4.x. Ready to review.

  • Pipeline finished with Skipped
    about 1 month ago
    #299118
  • 🇬🇧United Kingdom adamps

    Great thanks

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024