From b7f13b4be5604c8b80d0348d80b76eb4172368ad Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 21 Jun 2025 00:24:21 +0100 Subject: [PATCH] Refactor --- .../opd_daily_emails/opd_daily_emails.module | 28 +----------- .../opd_daily_emails.services.yml | 2 + .../src/AddRandomCtaToDailyEmail.php | 43 +++++++++++++++++++ 3 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php diff --git a/modules/opd_daily_emails/opd_daily_emails.module b/modules/opd_daily_emails/opd_daily_emails.module index fc311bd3e..84cbdea60 100644 --- a/modules/opd_daily_emails/opd_daily_emails.module +++ b/modules/opd_daily_emails/opd_daily_emails.module @@ -4,8 +4,7 @@ declare(strict_types=1); use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\node\NodeInterface; -use Drupal\opd_daily_emails\Ctas; +use Drupal\opd_daily_emails\AddRandomCtaToDailyEmail; use Drupal\opd_daily_emails\DailyEmail; use Drupal\opd_daily_emails\DailyEmailNodeRepository; @@ -28,30 +27,7 @@ function opd_daily_emails_entity_presave(Drupal\Core\Entity\EntityInterface $ent return; } - if (!$entity->isNew()) { - return; - } - - if ($entity->get('field_daily_email_cta')->getValue() !== []) { - return; - } - - // TODO: Don't add a CTA if the email already contains a "P.S.". - - $nodeStorage = \Drupal::entityTypeManager()->getStorage('node'); - $query = $nodeStorage->getQuery(); - $query->condition('status', NodeInterface::PUBLISHED); - $query->condition('type', 'daily_email_cta'); - $query->accessCheck(); - $ctaNodes = $nodeStorage->loadMultiple($query->execute()); - - $ctas = Ctas::fromNodes($ctaNodes); - - if ($ctas->isEmpty()) { - return; - } - - $entity->set('field_daily_email_cta', $ctas->getRandomCta()); + \Drupal::service(AddRandomCtaToDailyEmail::class)($entity); } /** diff --git a/modules/opd_daily_emails/opd_daily_emails.services.yml b/modules/opd_daily_emails/opd_daily_emails.services.yml index 43fbb8e41..0bc1a892c 100644 --- a/modules/opd_daily_emails/opd_daily_emails.services.yml +++ b/modules/opd_daily_emails/opd_daily_emails.services.yml @@ -1,3 +1,5 @@ services: + Drupal\opd_daily_emails\AddRandomCtaToDailyEmail: + autowire: true Drupal\opd_daily_emails\DailyEmailNodeRepository: autowire: true diff --git a/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php b/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php new file mode 100644 index 000000000..ad35f7c76 --- /dev/null +++ b/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php @@ -0,0 +1,43 @@ +isNew()) { + return; + } + + if ($email->get('field_daily_email_cta')->getValue() !== []) { + return; + } + + // TODO: Don't add a CTA if the email already contains a "P.S.". + + $nodeStorage = $this->entityTypeManager->getStorage('node'); + $query = $nodeStorage->getQuery(); + $query->condition('status', NodeInterface::PUBLISHED); + $query->condition('type', 'daily_email_cta'); + $query->accessCheck(); + $ctaNodes = $nodeStorage->loadMultiple($query->execute()); + + $ctas = Ctas::fromNodes($ctaNodes); + + if ($ctas->isEmpty()) { + return; + } + + $email->set('field_daily_email_cta', $ctas->getRandomCta()); + } + +}