Commit 02b66b94 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3262883 by AdamPS: Extend address policy elements

parent 7008de06
Loading
Loading
Loading
Loading
+9 −34
Changes for config/schema/symfony_mailer.schema.yml: 9 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -66,9 +66,8 @@ symfony_mailer.mailer_policy.*:
      sequence:
        type: symfony_mailer.email_adjuster_plugin.[%key]

symfony_mailer.email_adjuster_plugin.email_bcc:
symfony_mailer.address_adjuster_base:
  type: mapping
  label: 'Bcc header'
  mapping:
    value:
      type: email
@@ -77,6 +76,10 @@ symfony_mailer.email_adjuster_plugin.email_bcc:
      type: label
      label: 'Display name'

symfony_mailer.email_adjuster_plugin.email_bcc:
  label: 'Bcc header'
  type: symfony_mailer.address_adjuster_base

symfony_mailer.email_adjuster_plugin.email_body:
  type: mapping
  label: 'Body'
@@ -86,26 +89,12 @@ symfony_mailer.email_adjuster_plugin.email_body:
      label: 'Email body'

symfony_mailer.email_adjuster_plugin.email_cc:
  type: mapping
  type: symfony_mailer.address_adjuster_base
  label: 'Cc header'
  mapping:
    value:
      type: email
      label: 'E-mail address'
    display:
      type: label
      label: 'Display name'

symfony_mailer.email_adjuster_plugin.email_from:
  type: mapping
  type: symfony_mailer.address_adjuster_base
  label: 'From header'
  mapping:
    value:
      type: email
      label: 'E-mail address'
    display:
      type: label
      label: 'Display name'

symfony_mailer.email_adjuster_plugin.email_plain:
  type: mapping
@@ -124,15 +113,8 @@ symfony_mailer.email_adjuster_plugin.email_priority:
      label: 'Email subject'

symfony_mailer.email_adjuster_plugin.email_reply_to:
  type: mapping
  type: symfony_mailer.address_adjuster_base
  label: 'Reply-to header'
  mapping:
    value:
      type: email
      label: 'E-mail address'
    display:
      type: label
      label: 'Display name'

symfony_mailer.email_adjuster_plugin.email_skip_sending:
  type: mapping
@@ -159,15 +141,8 @@ symfony_mailer.email_adjuster_plugin.email_theme:
      label: 'Email theme'

symfony_mailer.email_adjuster_plugin.email_to:
  type: mapping
  type: symfony_mailer.address_adjuster_base
  label: 'To header'
  mapping:
    value:
      type: email
      label: 'E-mail address'
    display:
      type: label
      label: 'Display name'

symfony_mailer.email_adjuster_plugin.email_transport:
  type: mapping
+22 −1
Changes for src/MailerHelper.php: 22 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\symfony_mailer;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -11,6 +12,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterManager;
use Drupal\symfony_mailer\Processor\EmailBuilderManager;
use Symfony\Component\Mime\Address;

/**
 * Provides the mailer helper service.
@@ -38,6 +40,13 @@ class MailerHelper implements MailerHelperInterface {
   */
  protected $builderManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs the MailerHelper object.
   *
@@ -47,11 +56,23 @@ class MailerHelper implements MailerHelperInterface {
   *   The email adjuster manager.
   * @param \Drupal\symfony_mailer\Processor\EmailBuilderManager $email_builder_manager
   *   The email builder manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EmailAdjusterManager $email_adjuster_manager, EmailBuilderManager $email_builder_manager) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EmailAdjusterManager $email_adjuster_manager, EmailBuilderManager $email_builder_manager, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->adjusterManager = $email_adjuster_manager;
    $this->builderManager = $email_builder_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getSiteAddress() {
    $site_config = $this->configFactory->get('system.site');
    $site_mail = $site_config->get('mail') ?: ini_get('sendmail_from');
    return new Address($site_mail, $site_config->get('name'));
  }

  /**
+8 −0
Changes for src/MailerHelperInterface.php: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,14 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
 */
interface MailerHelperInterface {

  /**
   * Get an address using the site mail and name.
   *
   * @return \Symfony\Component\Mime\Address
   *   The address.
   */
  public function getSiteAddress();

  /**
   * Renders an element that lists policy related to a config entity.
   *
+19 −5
Changes for src/Plugin/EmailAdjuster/AddressAdjusterBase.php: 19 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,8 +3,10 @@
namespace Drupal\symfony_mailer\Plugin\EmailAdjuster;

use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\EmailInterface;
use Drupal\symfony_mailer\MailerHelperTrait;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\user\Entity\User;
use Symfony\Component\Mime\Address;

/**
@@ -14,6 +16,8 @@ abstract class AddressAdjusterBase extends EmailAdjusterBase {
  // @todo Allow multiple values
  // @todo Setting whether to replace existing addresses or add to them.

  use MailerHelperTrait;

  /**
   * Sets the address in the appropriate header.
   *
@@ -28,9 +32,19 @@ abstract class AddressAdjusterBase extends EmailAdjusterBase {
   * {@inheritdoc}
   */
  public function postRender(EmailInterface $email) {
    $mail = $this->configuration['value'];
    $value = $this->configuration['value'];
    $display = $this->configuration['display'];
    $address = $display ? new Address($mail, $display) : $mail;

    if ($value === '<site>') {
      $address = $this->helper()->getSiteAddress();
    }
    elseif ((strpos($value, '@') === FALSE) && ($user = User::load($value))) {
      $address = new Address($user->getEmail(), $user->getDisplayName());
    }
    else {
      $address = $display ? new Address($value, $display) : $value;
    }

    $this->setAddress($email, $address);
  }

@@ -43,14 +57,14 @@ abstract class AddressAdjusterBase extends EmailAdjusterBase {
      '#title' => t('Address'),
      '#default_value' => $this->configuration['value'] ?? NULL,
      '#required' => TRUE,
      '#description' => $this->t('Email address.'),
      '#description' => $this->t('Enter an email address, a user ID, or %site to use the site email address.', ['%site' => '<site>']),
    ];

    $form['display'] = [
      '#type' => 'textfield',
      '#title' => t('Display name'),
      '#default_value' => $this->configuration['display'] ?? NULL,
      '#description' => $this->t('Human-readable display name.'),
      '#description' => $this->t('Human-readable display name (ignored for user or site address).'),
    ];

    return $form;
+4 −43
Changes for src/Plugin/EmailAdjuster/DefaultsEmailAdjuster.php: 4 added lines, 43 removed lines.
Original line number Diff line number Diff line
@@ -2,13 +2,10 @@

namespace Drupal\symfony_mailer\Plugin\EmailAdjuster;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterBase;
use Drupal\symfony_mailer\Entity\MailerTransport;
use Drupal\symfony_mailer\EmailInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\Address;
use Drupal\symfony_mailer\MailerHelperTrait;

/**
 * Defines the Default headers Email Adjuster.
@@ -20,51 +17,15 @@ use Symfony\Component\Mime\Address;
 *   weight = 100,
 * )
 */
class DefaultsEmailAdjuster extends EmailAdjusterBase implements ContainerFactoryPluginInterface {
class DefaultsEmailAdjuster extends EmailAdjusterBase {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new DefaultsEmailAdjuster object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory')
    );
  }
  use MailerHelperTrait;

  /**
   * {@inheritdoc}
   */
  public function preRender(EmailInterface $email) {
    $site_config = $this->configFactory->get('system.site');
    $site_mail = $site_config->get('mail') ?: ini_get('sendmail_from');
    $sender = new Address($site_mail, $site_config->get('name'));
    $sender = $this->helper()->getSiteAddress();
    $theme = $email->getTheme();
    $email->setSender($sender)
      ->addTextHeader('X-Mailer', 'Drupal')
Loading