Refactor
This commit is contained in:
parent
9709d4fee0
commit
b7f13b4be5
3 changed files with 47 additions and 26 deletions
43
modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php
Normal file
43
modules/opd_daily_emails/src/AddRandomCtaToDailyEmail.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_daily_emails;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opd_daily_emails\Ctas;
|
||||
|
||||
readonly final class AddRandomCtaToDailyEmail {
|
||||
|
||||
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
|
||||
}
|
||||
|
||||
public function __invoke(DailyEmail $email): void {
|
||||
if (!$email->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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue