Skip to content
Snippets Groups Projects
Commit dfccf928 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3265253 by AdamPS: Add policy listings to module entity/settings forms (user and contact)

parent ab958d0a
Branches
Tags
No related merge requests found
......@@ -13,3 +13,5 @@ configuration:
<p>[user:display-name],</p>
<p>Your account on <a href="[site:url]">[site:name]</a> has been blocked.</p>
format: email_html
email_skip_sending:
message: 'Notification disabled in settings'
......@@ -13,3 +13,5 @@ configuration:
<p>[user:display-name],</p>
<p>Your account on <a href="[site:url]">[site:name]</a> has been canceled.</p>
format: email_html
email_skip_sending:
message: 'Notification disabled in settings'
<?php
namespace Drupal\symfony_mailer_bc;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
/**
* Example configuration override.
*/
class MailerBcConfigOverride implements ConfigFactoryOverrideInterface {
/**
* {@inheritdoc}
*/
public function loadOverrides($names) {
$overrides = [];
if (in_array('user.setting', $names)) {
$overrides['user.setting']['notify'] = [
'cancel_confirm' => TRUE,
'password_reset' => TRUE,
'status_activated' => TRUE,
'status_blocked' => TRUE,
'status_canceled' => TRUE,
'register_admin_created' => TRUE,
'register_no_approval_required' => TRUE,
'register_pending_approval' => TRUE,
];
}
return $overrides;
}
/**
* {@inheritdoc}
*/
public function getCacheSuffix() {
return 'MailerBcConfigOverride';
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
/**
* {@inheritdoc}
*/
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
}
......@@ -2,8 +2,12 @@
namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;
use Drupal\contact\Entity\ContactForm;
use Drupal\Core\Url;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Entity\MailerPolicy;
use Drupal\symfony_mailer\MailerHelperTrait;
use Drupal\symfony_mailer\Processor\MailerPolicyImportInterface;
/**
* Defines the Email Builder plug-in for contact module page forms.
......@@ -16,12 +20,16 @@ use Drupal\symfony_mailer\EmailInterface;
* "autoreply" = @Translation("Auto-reply"),
* },
* has_entity = TRUE,
* common_adjusters = {"email_subject", "email_from"},
* import = @Translation("Contact form recipients"),
* )
*
* @todo Notes for adopting Symfony Mailer into Drupal core. This builder can
* set langcode, to, reply-to so the calling code doesn't need to.
*/
class ContactPageEmailBuilder extends ContactEmailBuilderBase {
class ContactPageEmailBuilder extends ContactEmailBuilderBase implements MailerPolicyImportInterface {
use MailerHelperTrait;
/**
* {@inheritdoc}
......@@ -36,4 +44,19 @@ class ContactPageEmailBuilder extends ContactEmailBuilderBase {
}
}
/**
* {@inheritdoc}
*/
public function import() {
$helper = $this->helper();
foreach (ContactForm::loadMultiple() as $id => $form) {
if ($id != 'personal') {
$addresses = $helper->parseAddress(implode(',', $form->getRecipients()));
$config['email_to'] = $helper->policyFromAddresses($addresses);
MailerPolicy::import("contact_form.mail.$id", $config);
}
}
}
}
......@@ -2,9 +2,12 @@
namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\Entity\MailerPolicy;
use Drupal\symfony_mailer\MailerHelperTrait;
use Drupal\symfony_mailer\Processor\EmailProcessorBase;
use Drupal\symfony_mailer\Processor\MailerPolicyImportInterface;
use Drupal\symfony_mailer\Processor\TokenProcessorTrait;
use Drupal\symfony_mailer\EmailInterface;
/**
* Defines the Email Builder plug-in for user module.
......@@ -22,19 +25,62 @@ use Drupal\symfony_mailer\EmailInterface;
* "status_blocked" = @Translation("Account blocked"),
* "status_canceled" = @Translation("Account cancelled"),
* },
* common_adjusters = {"email_subject", "email_body"},
* import = @Translation("User email settings"),
* import_warning = @Translation("This overrides the default HTML messages with imported plain text versions."),
* )
*
* @todo Notes for adopting Symfony Mailer into Drupal core. This builder can
* set langcode, to, reply-to so the calling code doesn't need to.
*/
class UserEmailBuilder extends EmailProcessorBase {
class UserEmailBuilder extends EmailProcessorBase implements MailerPolicyImportInterface {
use MailerHelperTrait;
use TokenProcessorTrait;
/**
* {@inheritdoc}
*/
public function preRender(EmailInterface $email) {
if ($email->getSubType() != 'register_pending_approval_admin') {
$email->setTo($email->getParam('user')->getEmail());
}
$this->tokenOptions(['callback' => 'user_mail_tokens', 'clear' => TRUE]);
}
/**
* {@inheritdoc}
*/
public function import() {
$config_factory = $this->helper()->config();
$notify = $config_factory->get('user.settings')->get('notify');
$mail = $config_factory->get('user.mail')->get();
unset($mail['langcode']);
if ($mail_notification = $config_factory->get('system.site')->get('mail_notification')) {
$notification_policy = $this->helper()->policyFromAddresses($this->helper()->parseAddress($mail_notification));
$config['email_reply_to'] = $notification_policy;
MailerPolicy::import("user", $config);
}
foreach ($mail as $sub_type => $values) {
$config = [
'email_subject' => ['value' => $values["subject"]],
'email_body' => [
'content' => [
'value' => $values["body"],
'format' => 'plain_text',
],
],
];
if (isset($notify[$sub_type]) && !$notify[$sub_type]) {
$config['email_skip_sending']['message'] = 'Notification disabled in settings';
}
if (($sub_type == 'register_pending_approval_admin') && isset($notification_policy)) {
$config['email_to'] = $notification_policy;
}
MailerPolicy::import("user.$sub_type", $config);
}
}
}
......@@ -55,6 +55,11 @@ function symfony_mailer_bc_module_implements_alter(&$implementations, $hook) {
*/
function symfony_mailer_bc_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config_all = [
'contact_form_edit_form' => [
'remove' => ['recipients'],
'entity' => ['mail'],
],
'simplenews_admin_settings_newsletter' => [
'remove' => ['simplenews_default_options', 'simplenews_sender_info'],
],
......@@ -66,6 +71,23 @@ function symfony_mailer_bc_form_alter(&$form, FormStateInterface $form_state, $f
'remove' => ['email', 'simplenews_sender_information', 'simplenews_subject'],
'entity' => ['node'],
],
'user_admin_settings' => [
'remove' => [
'mail_notification_address',
'email_admin_created',
'email_pending_approval',
'email_pending_approval_admin',
'email_no_approval_required',
'email_password_reset',
'email_activated',
'email_blocked',
'email_cancel_confirm',
'email_canceled',
// For user_registrationpassword.
'email_user_registrationpassword',
],
'type' => ['user', ['email_body', 'email_subject']],
],
];
if ($config = $config_all[$form_id] ?? NULL) {
......@@ -95,6 +117,11 @@ function symfony_mailer_bc_mailer_bc_contact_alter(array &$message, ?ConfigEntit
if (isset($message['params']['contact_form'])) {
// Set the associated config entity.
$entity = $message['params']['contact_form'];
if ($message['key'] == 'page_copy') {
// Remove the 'to' address as it will be set by Mailer Policy.
$message['to'] = '';
}
}
// This creates two separate email types, so no longer need the key to
......@@ -110,8 +137,7 @@ function symfony_mailer_bc_mailer_bc_simplenews_alter(array &$message, ?ConfigEn
// Set the associated config entity.
$entity = $message['params']['simplenews_mail']->getNewsletter();
// Remove the reply-to address because we use policy from this module
// instead.
// Remove the reply-to address as it will be set by Mailer Policy.
$message['reply'] = '';
// Use a parameter for 'test' rather than the key. We don't want test
......@@ -139,6 +165,12 @@ function symfony_mailer_bc_mailer_bc_user_alter(array &$message, ?ConfigEntityIn
// Use the entity type as the parameter key.
// @see \Drupal\symfony_mailer\EmailInterface::setParam()
$message['params'] = ['user' => $message['params']['account']];
// Remove the 'to' address as we calculate it.
$message['to'] = '';
// Remove the 'reply-to' address as it will be set by Mailer Policy.
$message['reply'] = '';
}
/**
......
services:
symfony_mailer_bc.overrider:
class: Drupal\symfony_mailer_bc\MailerBcConfigOverride
tags:
- {name: config.factory.override}
......@@ -97,8 +97,7 @@ class MailerHelper implements MailerHelperInterface {
$display = $address->getName();
}
// @todo Support multiple addresses.
$config = [
$config['addresses'][] = [
'value' => $value,
'display' => $display ?? '',
];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment