Skip to content
Snippets Groups Projects
Commit f048c322 authored by Christian Foidl's avatar Christian Foidl Committed by Christian Foidl
Browse files

Issue #3417737: Add config option for mail token replacement message ids

parent ce37d8bb
No related branches found
No related tags found
1 merge request!3Issue #3417737: Add config option for mail token replacement message ids
Pipeline #84703 passed
<?php
/**
* @file
* Hooks provided by magic_code module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Add custom magic-code operations to tokens.
*
* This example would add the token [user:magic-code-my-custom-operation].
*/
function hook_magic_code_user_mail_token_operations(&$operations) {
$operations[] = 'my-custom-operation';
}
/**
* Alters if given message should have token replaced by magic-code module.
*/
function hook_magic_code_replace_token_in_email(&$replaceInEmail, $context) {
$message = $context['message'];
if ($message['id'] === 'my-message-id') {
$replaceInEmail = TRUE;
}
}
/**
* @} End of "addtogroup hooks"
*/
......@@ -61,7 +61,21 @@ function magic_code_entity_update(EntityInterface $entity) {
* Implements hook_mail_alter().
*/
function magic_code_mail_alter(&$message) {
$replaceInEmail = FALSE;
$context = [
'message' => $message,
];
// Alow user emails by default.
if (strpos($message['id'], 'user_') === 0) {
$replaceInEmail = TRUE;
}
// Let modules alter the decision.
\Drupal::moduleHandler()->alter('magic_code_replace_token_in_email', $replaceInEmail, $context);
if ($replaceInEmail) {
_magic_code_replace_mail_tokens($message);
}
}
......
......@@ -11,3 +11,12 @@
function magic_code_test_magic_code_user_mail_token_operations_alter(&$operations) {
$operations[] = 'added-operation';
}
/**
* Implements hook_magic_code_replace_token_in_email_alter().
*/
function magic_code_test_magic_code_replace_token_in_email_alter(&$replaceInEmail, &$context) {
if ($context['message']['id'] === 'user_register_no_approval_required') {
$replaceInEmail = FALSE;
}
}
......@@ -70,6 +70,10 @@ class MagicCodeTokenTest extends EntityKernelTestBase {
$registerConfig['body'] = $registerConfig['body'] . PHP_EOL . '[user:magic-code-added-operation]';
$mailConfig->set('register_admin_created', $registerConfig)->save();
$reg2Config = $mailConfig->get('register_no_approval_required');
$reg2Config['body'] = $reg2Config['body'] . PHP_EOL . '[user:magic-code-added-operation]';
$mailConfig->set('register_no_approval_required', $reg2Config)->save();
$this->manager = $this->container->get('magic_code.manager');
}
......@@ -116,4 +120,19 @@ class MagicCodeTokenTest extends EntityKernelTestBase {
$this->assertMailString('body', $codeEntity->label(), 1);
}
/**
* Test disabled token replacement.
*/
public function testDisabledMailTokenReplacement() {
$user = $this->drupalCreateUser();
_user_mail_notify('register_no_approval_required', $user);
$query = $this->entityTypeManager->getStorage('magic_code')->getQuery();
$ids = $query->accessCheck(FALSE)->execute();
// No magic code should be generated.
$this->assertCount(0, $ids);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment