113 lines
2.9 KiB
PHP
113 lines
2.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
}
|
|
|
|
// 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());
|
|
}
|
|
|
|
/**
|
|
* Implements hook_token_info().
|
|
*
|
|
* @return array{tokens: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}[]}, types: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}}}
|
|
*/
|
|
function opd_daily_emails_token_info(): array {
|
|
$tokens = [];
|
|
|
|
$type = [
|
|
'description' => t('Tokens related to daily emails.'),
|
|
'name' => t('Daily emails'),
|
|
];
|
|
|
|
$tokens['email-count'] = [
|
|
'description' => t('The number of sent daily emails.'),
|
|
'name' => t('Daily email count'),
|
|
];
|
|
|
|
return [
|
|
'tokens' => [
|
|
'opd-daily-emails' => $tokens,
|
|
],
|
|
'types' => [
|
|
'opd-daily-emails' => $type,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Implements hook_tokens().
|
|
*
|
|
* @param array<non-empty-string, non-empty-string> $tokens
|
|
* @param array<non-empty-string, mixed> $data
|
|
* @param array<non-empty-string, mixed> $options
|
|
*
|
|
* @return array<non-empty-string, mixed>
|
|
*/
|
|
function opd_daily_emails_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleableMetadata) : array {
|
|
$replacements = [];
|
|
|
|
if ($type === 'opd-daily-emails') {
|
|
foreach ($tokens as $name => $original) {
|
|
switch ($name) {
|
|
case 'email-count':
|
|
$dailyEmailRepository = \Drupal::service(DailyEmailNodeRepository::class);
|
|
|
|
$dailyEmails = $dailyEmailRepository->getAll();
|
|
|
|
$replacements[$original] = $dailyEmails->count();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $replacements;
|
|
}
|
|
|