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
CommentFileSizeAuthor
#11 change.patch16.97 MBaditiup

Issue fork simplenews-3447905

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

vlad.dancer created an issue. See original summary.

vlad.dancer’s picture

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

vlad.dancer’s picture

Issue summary: View changes

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');
vlad.dancer’s picture

Status: Active » Needs review
vlad.dancer’s picture

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());
}

UPD: for 4.x version the form hook is a bit different.

function mymodule_form_alter(array &$form, FormStateInterface &$form_state, $form_id): void {
  if ($form_id === 'simplenews_subscriptions_block_[SIMPLENEWS_ENTITY_UUID]') {
    //... Rest of the code from above.
  }
}
vlad.dancer’s picture

Assigned: vlad.dancer » Unassigned
Issue summary: View changes

Aditiup made their first commit to this issue’s fork.

aditiup’s picture

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.

aditiup’s picture

StatusFileSize
new16.97 MB
vlad.dancer’s picture

@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.

azizos’s picture

@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.

azizos’s picture

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

adamps’s picture

Status: Needs review » Needs work

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.

davps made their first commit to this issue’s fork.

davps’s picture

Status: Needs work » Needs review

MR's branch reset to last stable commits and rebased on to 4.x. Ready to review.

  • adamps committed 4dce8b84 on 4.x authored by vlad.dancer
    Issue #3447905 by vlad.dancer, davps, adamps: Wrong subscriber's...
adamps’s picture

Status: Needs review » Fixed

Great thanks

Status: Fixed » Closed (fixed)

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