Problem/Motivation

Multilingual support for Registration Messages and Email Notifications was introduced in #3475611: Allow to translate registration messages and mails.

However, in a project I've been working on, an additional requirement emerged: the language used for sending notifications to registrants is not necessarily the same as the language they are using to navigate the site.

Although a large number of languages have been installed in our Drupal CMS to support translations for notification messages, not all of these languages are available for site navigation. The general multilingual experience of the website is intentionally limited to a smaller set of languages - for example, English, Spanish, Korean, and Arabic.

That said, the list of languages used to translate notification messages is larger - around 13 languages. For this reason, users are allowed to select their preferred language for notifications, even if it differs from the language they use to navigate the site.

To support this scenario, we added a custom field to the registrant entity that presents a list of available languages, allowing users to explicitly choose the language in which they want to receive notifications. For example, a user might browse the site in Arabic, but at the momen of registering for an event instance, they could select Farsi or Armenian as their notification language.

Proposed resolution

To implement this behavior, I introduced a solution based on a new hook, triggered from the
Drupal\recurring_events_registration\NotificationService::getConfigValue() method. This hook allows modules to alter or explicitly define the language used for notifications—independently of the current navigation language.

The new hook is:
hook_recurring_events_registration_notification_language_alter()

From a custom module, a different language for notifications can then be defined as follows:

/**
 * Implements hook_recurring_events_registration_notification_language_alter().
 */
function mymodule_recurring_events_registration_notification_language_alter(
  LanguageInterface &$language,
  string $key,
  RegistrantInterface $registrant
) {
  // Example usage of the hook to override the language for notifications
  // based on a custom field and the notification key.
  if ($key !== 'series_modification_notification') {
    if (!$registrant->get('field_language_preference')->isEmpty()) {
      // Get the language preference from a custom field on the Registrant entity.
      $lang_code = $registrant->get('field_language_preference')->value;

      // Override the language to be used for the notification.
      $language = \Drupal::languageManager()->getLanguage($lang_code);
    }
  }
}

I'm sharing this approach to open a discussion on whether this feature would be valuable to include and release as part of the module.

Remaining tasks

Discuss the feature, the proposed approach, and the benefits provided by the added flexibility.

User interface changes

None.

API changes

New hook_recurring_events_registration_notification_language_alter(LanguageInterface &$language, string $key, RegistrantInterface $registrant).

Data model changes

None.

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

camilo.escobar created an issue. See original summary.

chrisla’s picture

Issue summary: View changes

I saw this issue and it made me think of an issue I am having with clients who use this module -- they would like to send out multiple notifications.

I wonder if these notifications would be better off as their own entities and then things like translation tools would become available for them as well. You could assign various notification entities to an event and they could have their own custom fields such as language.

camilo.escobar’s picture

Hi @chrisla,

It looks like you are reconsidering the architecture for email notifications and proposing a significantly different approach. I wanted to share my perspective, as I believe the current notifications subsystem is already serving its purpose very well.

The existing system is primarily based on:

  • The configuration object schema defined in recurring_events_registration.schema.yml
  • Translatable configurations, so once the config_translation module is enabled, email notification messages can be translated directly at: /admin/structure/events/registrant/settings/translate
  • hook_recurring_events_registration_notification_types_alter(), which allows modules to define and register custom notification types

From my experience, this covers the translation needs quite effectively.

Regarding sending multiple notifications, and determining which specific notifications should be sent and when, this can already be handled by defining custom notification types and triggering them at the appropriate moments in code. For example, in one of my projects, the client required an additional email to be sent when a user successfully registered for an event - specifically, an extra confirmation email related to a translator request, sent alongside the standard registration_notification.

To achieve this, I defined a custom notification type like this:

/**
 * Implements hook_recurring_events_registration_notification_types_alter().
 */
function mymodule_recurring_events_registration_notification_types_alter(array &$notification_types) {
  $notification_types['translator_requested_notification'] => [
    'name' => t('Requested Translator Notification'),
    'description' => t('Send an email to the registrant confirming their request of a translator.'),
  ];
}

Then, when a new registrant entity is created, I evaluate the relevant conditions and dispatch the notification via recurring_events_registration_send_notification() if needed:

/**
 * Implements hook_ENTITY_TYPE_insert() for registrant entities.
 */
function mymodule_registrant_insert(RegistrantInterface $registrant) {
  // Send `translator_requested_notification` email if translator field is
  // checked.
  if ($registrant->get('field_translator_requested')->value) {
    recurring_events_registration_send_notification('translator_requested_notification', $registrant);
  }
}

Because of this flexibility, I don't believe an additional architecture is required to handle translations or notification dispatching.

Coming back to the multilingual aspect: by default, when configuration values are retrieved programmatically through Drupal's config.factory service, those values are automatically returned in the current site navigation language. This is the behavior currently implemented in \Drupal\recurring_events_registration\NotificationService::getConfigValue():

$notifications = $this->configFactory->get($this->getConfigName())->get('notifications');

In most cases, this default behavior is sufficient.

However, as described in this issue, there are edge cases where notifications must be sent in a language different from the one used to navigate the site. Determining the correct language in those scenarios may require additional computation or custom business logic. This is precisely the use case that the proposed hook,
hook_recurring_events_registration_notification_language_alter(), is intended to support.

chrisla’s picture

Thanks very much for responding. Maybe this could be moved to its own issue?

I would also like to have fieldable notifications and so that is another point in favour of using entities. One use case could be fields for different languages apart from what are used in site translation tools. In my own clients' use cases, we use custom fields to adapt the content and delivery of the notifications per event and even per event instance.

Your use case for creating new notifications does not allow content editors to create new notifications. Only someone who could access the codebase can create new notifications. They cannot be created on the fly, which an entity approach would allow.

EDIT: I should have added, I would see this as an optional submodule, not imposed on current users.

camilo.escobar’s picture

It sounds like a powerful addition to the events system. The ability to create notification entities with their own fields, attached them to a specific event - and to control how and when they are sent - beyond the default notification types already provided by the module, would be a significant enhancement. This would add great flexibility to event management.

That said, I agree that this feature should be proposed and discussed in a separate issue.

If you go ahead and create a case, I'll be happy to follow it to see how the discussion evolves and to help move the proposal forward.