Commit 7008de06 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3265253 by AdamPS: Add policy listings to module entity/settings forms for simplenews

parent 0557670e
Loading
Loading
Loading
Loading
+9
Changes for modules/symfony_mailer_bc/config/optional/symfony_mailer.mailer_policy.simplenews_newsletter.yml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - simplenews
id: simplenews_newsletter
configuration:
  email_subject:
    value: '[[simplenews-newsletter:name]] [node:title]'
+5 −3
Changes for modules/symfony_mailer_bc/src/Plugin/EmailBuilder/SimplenewsNewsletterEmailBuilder.php: 5 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\symfony_mailer_bc\Plugin\EmailBuilder;

use Drupal\symfony_mailer\Processor\EmailProcessorBase;
use Drupal\symfony_mailer\Processor\TokenProcessorTrait;
use Drupal\symfony_mailer\EmailInterface;

/**
@@ -12,7 +13,6 @@ use Drupal\symfony_mailer\EmailInterface;
 *   id = "simplenews_newsletter",
 *   sub_types = {
 *     "node" = @Translation("Issue"),
 *     "test" = @Translation("Test"),
 *   },
 *   has_entity = TRUE,
 * )
@@ -21,6 +21,8 @@ use Drupal\symfony_mailer\EmailInterface;
 * MailBuilder class, and many methods of MailEntity.
 */
class SimplenewsNewsletterEmailBuilder extends EmailProcessorBase {
  use TokenProcessorTrait;
  // @todo Maybe only replace in the subject as body already done?

  /**
   * {@inheritdoc}
@@ -28,8 +30,8 @@ class SimplenewsNewsletterEmailBuilder extends EmailProcessorBase {
  public function preRender(EmailInterface $email) {
    /** @var \Drupal\simplenews\Mail\MailEntity $mail */
    $mail = $email->getParam('simplenews_mail');
    $email->setSubject($mail->getSubject())
      ->setBody($mail->getBody())
    $this->tokenData($mail->getTokenContext());
    $email->setBody($mail->getBody())
      ->addTextHeader('Precedence', 'bulk');

    if ($unsubscribe_url = \Drupal::token()->replace('[simplenews-subscriber:unsubscribe-url]', $email->getParams(), ['clear' => TRUE])) {
+62
Changes for modules/symfony_mailer_bc/symfony_mailer_bc.module: 62 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme().
 */
@@ -36,6 +38,56 @@ function symfony_mailer_bc_mailer_builder_info_alter(array &$email_builders) {
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function symfony_mailer_bc_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter') {
    // Move our implementation to the end of the list.
    $value = $implementations['symfony_mailer_bc'];
    unset($implementations['symfony_mailer_bc']);
    $implementations['symfony_mailer_bc'] = $value;
  }
}

/**
 * Implements hook_form_alter().
 */
function symfony_mailer_bc_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config_all = [
    'simplenews_admin_settings_newsletter' => [
      'remove' => ['simplenews_default_options', 'simplenews_sender_info'],
    ],
    'simplenews_admin_settings_subscription' => [
      'remove' => ['subscription_mail'],
      'type' => ['simplenews'],
    ],
    'simplenews_newsletter_edit_form' => [
      'remove' => ['email', 'simplenews_sender_information', 'simplenews_subject'],
      'entity' => ['node'],
    ],
  ];

  if ($config = $config_all[$form_id] ?? NULL) {
    $helper = Drupal::service('symfony_mailer.helper');

    // Hide fields that are replaced by Mailer Policy.
    foreach ($config['remove'] ?? [] as $key) {
      $form[$key]['#access'] = FALSE;
    }

    // Add policy elements on entity forms.
    if (!empty($config['entity'])) {
      $form['mailer_policy'] = $helper->renderEntityPolicy($form_state->getFormObject()->getEntity(), ...$config['entity']);
    }

    // Add policy elements on settings forms.
    if (!empty($config['type'])) {
      $form['mailer_policy'] = $helper->renderTypePolicy(...$config['type']);
    }
  }
}

/**
 * Implements hook_mailer_bc_MODULE_alter() for contact.
 */
@@ -57,6 +109,16 @@ function symfony_mailer_bc_mailer_bc_simplenews_alter(string &$key, array &$para
  if ($key == 'node' || $key == 'test') {
    // Set the associated config entity.
    $context['entity'] = $params['simplenews_mail']->getNewsletter();

    // Remove the reply-to address because we use policy from this module
    // instead.
    $context['reply'] = '';

    // Use a parameter for 'test' rather than the key. We don't want test
    // email to use a different policy, template, and so on.
    $params['test'] = ($key == 'test');
    $key = 'node';

  }
  elseif ($key == 'subscribe_combined') {
    $key = 'subscribe';