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().
*

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\node\NodeInterface;
use Webmozart\Assert\Assert;
final class Ctas {
public function isEmpty(): bool {
return empty($this->ctas);
}
public function getRandomCta(): NodeInterface {
return $this->ctas[array_rand($this->ctas)];
}
/**
* @param list<NodeInterface> $nodes
*/
public static function fromNodes(array $nodes): self {
return new self($nodes);
}
/**
* @param list<NodeInterface> $ctas
*/
private function __construct(
private array $ctas,
) {
Assert::allIsInstanceOf($ctas, NodeInterface::class);
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
final class DailyEmail extends Node implements NodeInterface {
public const NODE_TYPE = 'daily_email';
}

View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_daily_emails\Functional;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class CallToActionTest extends ExistingSiteBase {
public function test_saving_an_email_node_without_a_cta_will_populate_one(): void {
$cta = $this->createNode([
'type' => 'daily_email_cta',
]);
$email = $this->createNode([
'field_daily_email_cta' => NULL,
'type' => 'daily_email',
]);
$this->assertNotEmpty($email->get('field_daily_email_cta')->getValue());
// TODO: assert the returned text.
}
public function test_saving_an_email_node_with_a_cta_will_keep_the_same_cta(): void {
$cta = $this->createNode([
'type' => 'daily_email_cta',
]);
$email = $this->createNode([
'field_daily_email_cta' => $cta,
'type' => 'daily_email',
]);
$email->set('title', 'Updated');
$email->save();
$this->assertSame(
actual: $email->get('field_daily_email_cta')->getValue()[0]['target_id'],
expected: $cta->id(),
);
}
}