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

user.preferredLanguage variable not available in Sprout Email 4.x #322

Open
medoingthings opened this issue May 2, 2024 · 3 comments
Open

Comments

@medoingthings
Copy link

Description

I use a front-end form to register new users in Craft. It’s a multi-site website, which is why I set preferredLanguage on the form:

        <input type="hidden" name="preferredLanguage" value="{{ currentSite.language }}">

In the Craft 3.x version of Sprout Email, I was able to use user.preferredLanguage to…

  • …switch content for the notifications subject line {preferredLanguage == 'de' ? 'Willkommen' : 'Welcome'}
  • …switch between two custom fields, for localized content: {% set emailBody = user.preferredLanguage == 'de' ? email.emailBodyHtml_de : email.emailBodyHtml_en %}

This seems to have stopped working after upgrading to Craft 4. When I use {{ currentUser.preferredLanguage }} in a regular template on any page for a newly created front-end-user, I get a correct country code like en.

But when I use {{ user.preferredLanguage }} in the email template or in the notification config for the subject line, it’s empty.

Any hint how to get back the functionality?

How to reproduce

  1. Make sure the preferredLanguage variable is set for a user
  2. Trigger a Sprout Email notification on a user event
  3. Try to output {{ user.preferredLanguage }} within the triggered notification email

Sprout Version

4.44.444-beta.9

Craft Version

4.8.10

Database Type Version

MySQL 5.7.39

@medoingthings
Copy link
Author

Any hint on this one?

@BenParizek
Copy link
Member

BenParizek commented May 25, 2024

@medoingthings The main things that changed here is that when moving to Craft 4, what once was a single User Save Notification event trigger is now two different events:

  • UserCreatedNotificationEvent
  • UserUpdatedNotificationEvent

And the event we trigger off of used to be User::EVENT_AFTER_SAVE and is now User::EVENT_AFTER_PROPAGATE (the latter only firing once for multi-site installs for things like Entries that can be translated, but it shouldn't matter much here cause users only exist once in the system).

I'm not sure why this was working before, but I think the main issue here is that the user language preference does not get saved until after the user is saved (so the information is not present when the email event fires) and also, a new user is not logged in, and it appears the language preference only gets saved if a $currentUser exists. See the logic in Craft's codebase here: https://github.com/craftcms/cms/blob/12bb8e12b82ec4638a2d0ddc64572b82894bcc95/src/controllers/UsersController.php#L1485-L1513

Right now, I think the best thing you have to go on is the site language

{{ currentSite.language }}

Or perhaps trying to grab the value you posted in the form directly in the template:

{{ craft.app.request.getParam('preferredLanguage') }}

I am seeing values for the user.preferredLanguage and currentUser.preferredLanguage when testing a "When a user is updated". Which makes sense as that data is already in the db. But the way that Craft stores the preferences is in a second table so even their action to save them doesn't trigger a resave of the User element to watch.

If you'd like to tell me a bit more about which specific event you are using and what event/code you are using to trigger the email where you are seeing this issue I'm happy to try to consider options to better support this. I think checking for the posted param in the template might be the easiest path forward for now.

@medoingthings
Copy link
Author

Found the time to give it a go…

Using craft.app.request.getParam('preferredLanguage') works great in the templates when using the when user was created event. So one step closer on this one, thanks!

The remaining issue is, how do I know which language to pick for the email that is triggered by the when user was activated event.

I understand now, that Craft doesn’t set the preferredLanguage flag on user create (only for logged-in-users).

So… what do I do? Any chance that I can store the value with Sprout Email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants