Refactor
This commit is contained in:
parent
9709d4fee0
commit
b7f13b4be5
3 changed files with 47 additions and 26 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
services:
|
||||
Drupal\opd_daily_emails\AddRandomCtaToDailyEmail:
|
||||
autowire: true
|
||||
Drupal\opd_daily_emails\DailyEmailNodeRepository:
|
||||
autowire: true
|
||||
|
|
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