Afform - Email link doesn't render on individual emails for contacts with preferred locale
Overview
Afform allows you define email tokens that make quick-links to a form (eg {afform.afformProfileUrl}
). The token doesn't render in certain contexts.
This issue was identified by @larssg in MM (https://chat.civicrm.org/civicrm/pl/rwsgdztjpfbx5dgcpqkzw6x6se). It appears to be a regression circa 5.54. It does not affect CiviMail.
Reproduction steps
- Setup a D7 site. (D8+ and BD should have the same bug.)
- Find or create an afform. Ensure that it has a URL and email token.
- Find or create a contact. Ensure that Preferred Language is configured.
- View the contact (
civicrm/contact/view
). Use Actions: Send Email to send a message with the token (e.g.{afform.afformProfileUrl}
) - Check the email
Current behavior
In the resulting email, the token appears blank.
Expected behavior
The token shows a URL/hyperlink.
Comments
Under the hood, here are some key ingredients:
- afform registers for token events via afform_civicrm_config(), which includes a static-guard against re-registration.
- 5.54 included various updates with an aim toward supporting localized email. Consequently, while sending the email, it calls
CRM_Utils_System_*::setUFLocale()
. - On D7/D8+/BD,
setUFLocale()
calls this:// Config must be re-initialized to reset the base URL // otherwise links will have the wrong language prefix/domain. $config = CRM_Core_Config::singleton(); $config->free();
- Consequently, the next time anything calls
CRM_Core_Config::singleton()
, it will reboot the container. This effectively removes any listeners. For afform, the listeners are lost.