Commit 358aff50 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3278054 by AdamPS: Fix replacement of variables in subject

parent 6687db23
Loading
Loading
Loading
Loading
+0 −18
Changes for src/BaseEmailInterface.php: 0 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -22,24 +22,6 @@ use Symfony\Component\Mime\Header\Headers;
 */
interface BaseEmailInterface {

  /**
   * Sets the email subject.
   *
   * @param \Drupal\Component\Render\MarkupInterface|string $subject
   *   The email subject.
   *
   * @return $this
   */
  public function setSubject($subject);

  /**
   * Gets the email subject.
   *
   * @return \Drupal\Component\Render\MarkupInterface|string
   *   The email subject, or NULL if not set.
   */
  public function getSubject();

  /**
   * Sets the sender address.
   *
+0 −18
Changes for src/BaseEmailTrait.php: 0 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -16,13 +16,6 @@ trait BaseEmailTrait {
   */
  protected $inner;

  /**
   * The email subject.
   *
   * @var \Drupal\Component\Render\MarkupInterface|string
   */
  protected $subject;

  /**
   * The addresses.
   *
@@ -43,17 +36,6 @@ trait BaseEmailTrait {
   */
  protected $sender;

  public function setSubject($subject) {
    // We must not force conversion of the subject to a string as this could
    // cause translation before switching to the correct language.
    $this->subject = $subject;
    return $this;
  }

  public function getSubject() {
    return $this->subject;
  }

  public function setSender($address) {
    $this->sender = Address::create($address);
    return $this;
+41 −0
Changes for src/Email.php: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -75,6 +75,20 @@ class Email implements InternalEmailInterface {
   */
  protected $body = [];

  /**
   * The email subject.
   *
   * @var \Drupal\Component\Render\MarkupInterface|string
   */
  protected $subject;

  /**
   * Whether to replace variables in the email subject.
   *
   * @var bool
   */
  protected $replaceSubject;

  /**
   * @var array
   */
@@ -284,6 +298,24 @@ class Email implements InternalEmailInterface {
    return $this->body;
  }

  /**
   * {@inheritdoc}
   */
  public function setSubject($subject, bool $replace = FALSE) {
    // We must not force conversion of the subject to a string as this could
    // cause translation before switching to the correct language.
    $this->subject = $subject;
    $this->subjectReplace = $replace;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getSubject() {
    return $this->subject;
  }

  /**
   * {@inheritdoc}
   */
@@ -440,6 +472,15 @@ class Email implements InternalEmailInterface {
    $this->valid(self::PHASE_PRE_RENDER, self::PHASE_PRE_RENDER);

    // Render subject.
    if ($this->subjectReplace && $this->variables) {
      $subject = [
        '#type' => 'inline_template',
        '#template' => $this->subject,
        '#context' => $this->variables,
      ];
      $this->subject = $this->renderer->renderPlain($subject);
    }

    if ($this->subject instanceof MarkupInterface) {
      $this->subject = PlainTextOutput::renderFromHtml($this->subject);
    }
+20 −0
Changes for src/EmailInterface.php: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -218,6 +218,26 @@ interface EmailInterface extends BaseEmailInterface {
   */
  public function getBody();

  /**
   * Sets the email subject.
   *
   * @param \Drupal\Component\Render\MarkupInterface|string $subject
   *   The email subject.
   * @param bool $replace
   *   (Optional) TRUE to replace variables in the email subject.
   *
   * @return $this
   */
  public function setSubject($subject, bool $replace = FALSE);

  /**
   * Gets the email subject.
   *
   * @return \Drupal\Component\Render\MarkupInterface|string
   *   The email subject, or NULL if not set.
   */
  public function getSubject();

  /**
   * Sets variables available in the email template.
   *
+1 −13
Changes for src/Plugin/EmailAdjuster/SubjectEmailAdjuster.php: 1 added line, 13 removed lines.
Original line number Diff line number Diff line
@@ -22,19 +22,7 @@ class SubjectEmailAdjuster extends EmailAdjusterBase {
   */
  public function build(EmailInterface $email) {
    $subject = $this->configuration['value'];

    if ($variables = $email->getVariables()) {
      // Apply TWIG template.
      $subject = [
        '#type' => 'inline_template',
        '#template' => $subject,
        '#context' => $variables,
      ];
      $renderer = \Drupal::service('renderer');
      $subject = $renderer->renderPlain($subject);
    }

    $email->setSubject($subject);
    $email->setSubject($subject, TRUE);
  }

  /**