Add CTAs to daily emails

Add a daily email CTA node type and reference field to attach a CTA to a
daily email.

Also update the RSS feed view to include the referenced body field
value.
This commit is contained in:
Oliver Davies 2025-06-13 11:14:11 +01:00
parent 76868079ac
commit 2d55ea78f1
24 changed files with 1058 additions and 3 deletions

View file

@ -4,8 +4,54 @@ 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\DailyEmail;
use Drupal\opd_daily_emails\DailyEmailNodeRepository;
/**
* Implements hook_entity_bundle_info_alter().
*
* @param array<non-empty-string, array{class: non-empty-string}> $bundles
*/
function opd_daily_emails_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) {
$bundles['node'][DailyEmail::NODE_TYPE]['class'] = DailyEmail::class;
}
}
/**
* Implements hook_entity_presave().
*/
function opd_daily_emails_entity_presave(Drupal\Core\Entity\EntityInterface $entity): void {
if (!$entity instanceof DailyEmail) {
return;
}
if (!$entity->isNew()) {
return;
}
if ($entity->get('field_daily_email_cta')->getValue() !== []) {
return;
}
$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());
}
/**
* Implements hook_token_info().
*