Verified Commit a2c8988d authored by Dave Long's avatar Dave Long
Browse files

Issue #3420977 by larowlan, smustgrave, Berdir, mstrelan: Convert Mail plugin...

Issue #3420977 by larowlan, smustgrave, Berdir, mstrelan: Convert Mail plugin discovery to attributes
parent 4cbc8e20
Loading
Loading
Loading
Loading
Loading
+43 −0
Changes for core/lib/Drupal/Core/Mail/Attribute/Mail.php: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\Mail\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a Mail attribute for plugin discovery.
 *
 * Plugin Namespace: Plugin\Mail
 *
 * For a working example, see \Drupal\Core\Mail\Plugin\Mail\PhpMail
 *
 * @see \Drupal\Core\Mail\MailInterface
 * @see \Drupal\Core\Mail\MailManager
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class Mail extends Plugin {

  /**
   * Constructs a Mail attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The label of the plugin.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) A description of the plugin.
   * @param string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly ?string $deriver = NULL,
  ) {}

}
+2 −1
Changes for core/lib/Drupal/Core/Mail/MailManager.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Html;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Mail\Attribute\Mail;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
@@ -79,7 +80,7 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface {
   *   The renderer.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer) {
    parent::__construct('Plugin/Mail', $namespaces, $module_handler, 'Drupal\Core\Mail\MailInterface', 'Drupal\Core\Annotation\Mail');
    parent::__construct('Plugin/Mail', $namespaces, $module_handler, 'Drupal\Core\Mail\MailInterface', Mail::class, 'Drupal\Core\Annotation\Mail');
    $this->alterInfo('mail_backend_info');
    $this->setCacheBackend($cache_backend, 'mail_backend_plugins');
    $this->configFactory = $config_factory;
+7 −6
Changes for core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php: 7 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@

namespace Drupal\Core\Mail\Plugin\Mail;

use Drupal\Core\Mail\Attribute\Mail;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Header\UnstructuredHeader;

@@ -12,13 +14,12 @@

/**
 * Defines the default Drupal mail backend, using PHP's native mail() function.
 *
 * @Mail(
 *   id = "php_mail",
 *   label = @Translation("Default PHP mailer"),
 *   description = @Translation("Sends the message as plain text, using PHP's native mail() function.")
 * )
 */
#[Mail(
  id: 'php_mail',
  label: new TranslatableMarkup('Default PHP Mailer'),
  description: new TranslatableMarkup("Sends the message as plain text, using PHP's native mail() function."),
)]
class PhpMail implements MailInterface {

  /**
+6 −5
Changes for core/lib/Drupal/Core/Mail/Plugin/Mail/SymfonyMailer.php: 6 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,9 +3,11 @@
namespace Drupal\Core\Mail\Plugin\Mail;

use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Mail\Attribute\Mail;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -48,13 +50,12 @@
 *
 * @see https://symfony.com/doc/current/mailer.html#using-built-in-transports
 *
 * @Mail(
 *   id = "symfony_mailer",
 *   label = @Translation("Symfony mailer (Experimental)"),
 * )
 *
 * @internal
 */
#[Mail(
  id: 'symfony_mailer',
  label: new TranslatableMarkup('Symfony mailer (Experimental)'),
)]
class SymfonyMailer implements MailInterface, ContainerFactoryPluginInterface {

  /**
+7 −6
Changes for core/lib/Drupal/Core/Mail/Plugin/Mail/TestMailCollector.php: 7 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -2,19 +2,20 @@

namespace Drupal\Core\Mail\Plugin\Mail;

use Drupal\Core\Mail\Attribute\Mail;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a mail backend that captures sent messages in the state system.
 *
 * This class is for running tests or for development.
 *
 * @Mail(
 *   id = "test_mail_collector",
 *   label = @Translation("Mail collector"),
 *   description = @Translation("Does not send the message, but stores it in Drupal within the state system. Used for testing.")
 * )
 */
#[Mail(
  id: 'test_mail_collector',
  label: new TranslatableMarkup('Mail collector'),
  description: new TranslatableMarkup('Does not send the message, but stores it in Drupal within the state system. Used for testing.'),
)]
class TestMailCollector extends PhpMail implements MailInterface {

  /**
Loading