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

Issue #3502238 by adamps: Transition to 2.x

parent 36ed4b7b
No related branches found
No related tags found
2 merge requests!149Validate/remove unknown builder parameters in EmailFactory.,!131Issue #3502238: Transition to 2.x
Pipeline #406366 passed
name: Drupal Symfony Mailer Override
type: module
description: 'Overrides email building for key core and contrib modules, improving features and integration with Symfony Mailer.'
core_version_requirement: ^10.3 || ^11
package: Mail
lifecycle: deprecated
lifecycle_link: 'https://www.drupal.org/project/symfony_mailer/issues/3355626'
......@@ -12,6 +12,7 @@ use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;
* Extension of \Drupal\Core\DependencyInjection\AutowireTrait.
* - Add caching to reduce performance issues.
* - Allow passing extra arguments for the constructor.
* - Allow optional dependencies.
*/
trait AutowireTrait {
......@@ -43,15 +44,19 @@ trait AutowireTrait {
}
if (!$container->has($service)) {
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
if (!$parameter->allowsNull()) {
throw new AutowiringFailedException($service, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.', $service, $parameter->getName(), static::class));
}
static::$services[] = NULL;
}
else {
static::$services[] = $service;
}
static::$services[] = $service;
}
}
foreach (static::$services as $service) {
$args[] = $container->get($service);
$args[] = $service ? $container->get($service) : NULL;
}
return new static(...$args);
}
......
......@@ -260,14 +260,6 @@ class BaseEmail implements BaseEmailInterface {
return $this->attach(Attachment::fromPath($path, $name, $mimeType));
}
/**
* {@inheritdoc}
*/
public function attachNoPath(string $body, ?string $name = NULL, ?string $mimeType = NULL) {
@trigger_error('\Drupal\symfony_mailer\Email::attachNoPath() is deprecated in symfony_mailer:1.6.0 and is removed from symfony_mailer:2.0.0. Use ::attach() instead. See https://www.drupal.org/node/3476132', E_USER_DEPRECATED);
return $this->attach(Attachment::fromData($body, $name, $mimeType));
}
/**
* {@inheritdoc}
*/
......
......@@ -276,29 +276,6 @@ interface BaseEmailInterface {
*/
public function attachFromPath(string $path, ?string $name = NULL, ?string $mimeType = NULL);
/**
* Adds an attachment from temporary content that's not related to a path.
*
* If the content comes from a path (such as a local file, or web resource)
* then use attachFromPath(). This is important to ensure security checking
* runs based on the correct path.
*
* @param string $body
* The content of the attachment.
* @param string|null $name
* (optional) The file name.
* @param string|null $mimeType
* (optional) The MIME type. If omitted, the type will be guessed.
*
* @return $this
*
* @deprecated in symfony_mailer:1.6.0 and is removed from symfony_mailer:2.0.0.
* Instead you should use attach().
*
* @see https://www.drupal.org/node/3476132
*/
public function attachNoPath(string $body, ?string $name = NULL, ?string $mimeType = NULL);
/**
* Gets the attachments.
*
......
......@@ -27,7 +27,7 @@ class TestEmailForm extends FormBase {
*/
public function __construct(
protected readonly TestMailerInterface $mailer,
protected readonly PolicyHelperInterface $helper,
protected readonly ?PolicyHelperInterface $helper = NULL,
) {}
/**
......@@ -50,7 +50,9 @@ class TestEmailForm extends FormBase {
'#description' => $this->t('Recipient email address. Leave blank to send to yourself.'),
];
$form['mailer_policy'] = $this->helper->renderTypePolicy('symfony_mailer');
if ($this->helper) {
$form['mailer_policy'] = $this->helper->renderTypePolicy('symfony_mailer');
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
......
......@@ -66,11 +66,6 @@ class MailerManager extends DefaultPluginManager implements MailerManagerInterfa
public function processDefinition(&$definition, $plugin_id) {
// Look up the related entity or module, which can be used to generate the
// label and provider.
if (!empty($definition['has_entity'])) {
@trigger_error('Mailer annotation "has_entity" is deprecated in symfony_mailer:1.6.0 and is removed from symfony_mailer:2.0.0. Use "metadata_key" instead. See https://www.drupal.org/node/3481511', E_USER_DEPRECATED);
$definition['metadata_key'] = $plugin_id;
}
[$base_id] = explode('.', $plugin_id);
if ($meta_key = $definition['metadata_key']) {
if ($entity_def = $this->entityTypeManager->getDefinition($meta_key, FALSE)) {
......
......@@ -5,6 +5,8 @@
* Install, update and uninstall functions for the symfony mailer module.
*/
use Drupal\mailer_policy\Entity\MailerPolicy;
/**
* Implements hook_requirements().
*/
......@@ -34,3 +36,93 @@ function symfony_mailer_requirements($phase) {
return $requirements;
}
/**
* Enable new dependency mailer_policy.
*/
function symfony_mailer_update_20001() {
$module_installer = \Drupal::service('module_installer');
$module_handler = \Drupal::service('module_handler');
if (!$module_handler->moduleExists('mailer_policy')) {
$module_installer->install(['mailer_policy']);
// Delete default policy.
foreach (MailerPolicy::loadMultiple() as $policy) {
$policy->delete();
}
}
}
/**
* Split configuration into the correct sub-modules.
*/
function symfony_mailer_update_20002() {
$config_factory = \Drupal::configFactory();
$map = [
'symfony_mailer.mailer_policy' => 'mailer_policy.mailer_policy',
'symfony_mailer.mailer_transport' => 'mailer_transport.mailer_transport',
];
foreach ($map as $old_prefix => $new_prefix) {
foreach ($config_factory->listAll($old_prefix) as $old_name) {
$new_name = $new_prefix . substr($old_name, strlen($old_prefix));
$config_factory->rename($old_name, $new_name);
}
}
if ($settings = $config_factory->getEditable('symfony_mailer.settings')) {
$config_factory->getEditable('mailer_transport.settings')->set('default_transport', $settings->get('default_transport'))->save();
$config_factory->getEditable('mailer_override.settings')->set('override', $settings->get('override'))->save();
$settings->delete();
}
}
/**
* Adjustments to policy IDs.
*/
function symfony_mailer_update_20003() {
$map = [
'#(^[\w_]+\.[\w_]+)(\.[\w_]+)$#' => '$1.$2',
'#^contact($|\.copy|\.mail)#' => 'contact.user$1',
'#^contact_form#' => 'contact.page',
'#^simplenews($|\.subscribe|\.validate)#' => 'simplenews.subscriber$1',
'#^simplenews_newsletter#' => 'simplenews.newsletter',
'#^commerce_order_type#' => 'commerce_order',
];
foreach (MailerPolicy::loadMultiple() as $id => $policy) {
foreach ($map as $pattern => $replacement) {
$id = preg_replace($pattern, $replacement, $id);
}
if ($id != $policy->id()) {
MailerPolicy::create(['id' => $id, 'configuration' => $policy->getConfiguration()])->save();
$policy->delete();
}
}
}
/**
* Adjustments to variable names.
*/
function symfony_mailer_update_20004() {
$map = [
'commerce_order' => 'commerce_order',
'contact' => 'contact_message',
'simplenews' => 'issue',
];
foreach (MailerPolicy::loadMultiple() as $policy) {
[$initial] = explode('.', $policy->getTag());
if ($replacement = $map[$initial] ?? NULL) {
$config = $policy->getConfiguration();
if (isset($config['email_body']['content']['value'])) {
$config['email_body']['content']['value'] = str_replace('{{ body }}', "{{ $replacement }}", $config['email_body']['content']['value'], $count);
if ($count) {
$policy->setConfiguration($config)->save();
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment