From 9abf64b5045a46942642acdd00dde073c5818e50 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 21 Jun 2025 00:34:47 +0100 Subject: [PATCH] Don't add a CTA if an emails contains a P.S. Don't automatically add a CTA to a daily email if the email body text already contains a "P.S.". --- .../src/AddRandomCtaToDailyEmail.php | 4 +++- .../tests/src/Functional/CallToActionTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php b/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php index ad35f7c76..770f76fb1 100644 --- a/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php +++ b/modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php @@ -22,7 +22,9 @@ readonly final class AddRandomCtaToDailyEmail { return; } - // TODO: Don't add a CTA if the email already contains a "P.S.". + if (str_contains(haystack: $email->get('body')->value, needle: 'P.S.')) { + return; + } $nodeStorage = $this->entityTypeManager->getStorage('node'); $query = $nodeStorage->getQuery(); diff --git a/modules/opd_daily_emails/tests/src/Functional/CallToActionTest.php b/modules/opd_daily_emails/tests/src/Functional/CallToActionTest.php index 2082d43da..76309cfd1 100644 --- a/modules/opd_daily_emails/tests/src/Functional/CallToActionTest.php +++ b/modules/opd_daily_emails/tests/src/Functional/CallToActionTest.php @@ -46,4 +46,17 @@ final class CallToActionTest extends ExistingSiteBase { ); } + public function test_cta_in_body_prevents_automatically_adding_a_cta(): void { + $email = $this->createNode([ + 'body' => [ + 'format' => 'basic_html', + 'value' => 'P.S. This email already contains a CTA.', + ], + 'field_daily_email_cta' => NULL, + 'type' => 'daily_email', + ]); + + $this->assertEmpty($email->get('field_daily_email_cta')); + } + }