Make the email count dynamic based on the number

...of daily email nodes
This commit is contained in:
Oliver Davies 2025-05-12 01:13:25 +01:00
parent 6ba4b80645
commit c48f8acd4a
9 changed files with 5666 additions and 5 deletions

View file

@ -3,6 +3,7 @@
declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\opd_daily_emails\Repository\DailyEmailNodeRepository;
/**
* Implements hook_token_info().
@ -40,7 +41,9 @@ function opd_daily_emails_tokens($type, $tokens, array $data, array $options, Bu
foreach ($tokens as $name => $original) {
switch ($name) {
case 'email-count':
$replacements[$original] = 822;
$dailyEmailRepository = \Drupal::service(DailyEmailNodeRepository::class);
$replacements[$original] = count($dailyEmailRepository->getAll());
break;
}
}

View file

@ -0,0 +1,3 @@
services:
Drupal\opd_daily_emails\Repository\DailyEmailNodeRepository:
autowire: true

View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface {
public function __construct(
public readonly EntityTypeManagerInterface $entityTypeManager,
) {
}
public function getAll(): array {
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();
// TODO: add condition for published status. Only return published nodes.
$query->condition('type', 'daily_email');
$query->accessCheck(TRUE);
$result = $query->execute();
// TODO: this returns a list of node IDs. Load and return the nodes.
return $result;
}
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Repository;
interface DailyEmailRepositoryInterface {
public function getAll(): array;
}

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_daily_emails\Kernel\Repository;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\node\NodeInterface;
use Drupal\opd_daily_emails\Repository\DailyEmailNodeRepository;
use Drupal\opd_daily_emails\Repository\DailyEmailRepositoryInterface;
final class DailyEmailNodeRepositoryTest extends EntityKernelTestBase {
use NodeCreationTrait;
public static $modules = [
'node',
'opd_daily_emails',
];
/**
* @testdox Get all daily emails
*/
public function test_get_all(): void {
$this->createNode([
'status' => NodeInterface::PUBLISHED,
'type' => 'daily_email',
]);
$repository = $this->container->get(DailyEmailNodeRepository::class);
$this->assertInstanceOf(
actual: $repository,
expected: DailyEmailRepositoryInterface::class,
);
$this->assertCount(
expectedCount: 1,
haystack: $repository->getAll(),
);
}
}