2025-05-12 00:23:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use Drupal\Core\Render\BubbleableMetadata;
|
2025-05-29 18:12:17 +01:00
|
|
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
2025-06-23 22:25:39 +01:00
|
|
|
use Drupal\opd_daily_emails\Action\AddRandomCtaToDailyEmail;
|
2025-06-13 11:14:11 +01:00
|
|
|
use Drupal\opd_daily_emails\DailyEmail;
|
2025-06-12 02:10:08 +01:00
|
|
|
use Drupal\opd_daily_emails\DailyEmailNodeRepository;
|
2025-05-12 00:23:28 +01:00
|
|
|
|
2025-06-13 11:14:11 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2025-06-21 00:24:21 +01:00
|
|
|
\Drupal::service(AddRandomCtaToDailyEmail::class)($entity);
|
2025-06-13 11:14:11 +01:00
|
|
|
}
|
|
|
|
|
2025-07-04 08:27:59 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_form_FORM_ID_alter().
|
|
|
|
*
|
|
|
|
* @param array{'#action': string, '#attributes': array<non-empty-string, mixed>} $form
|
|
|
|
*/
|
|
|
|
function opd_daily_emails_form_opd_daily_emails_kit_subscription_form_alter(array &$form): void {
|
|
|
|
$form['#action'] = 'https://app.convertkit.com/forms/3546728/subscriptions';
|
|
|
|
|
|
|
|
$form['#attributes']['data-format'] = 'inline';
|
|
|
|
$form['#attributes']['data-sv-form'] = '3546728';
|
|
|
|
$form['#attributes']['data-uid'] = 'f0c1d2b57f';
|
|
|
|
$form['#attributes']['data-version'] = 5;
|
|
|
|
}
|
|
|
|
|
2025-05-12 00:23:28 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_token_info().
|
2025-05-29 18:12:17 +01:00
|
|
|
*
|
2025-05-30 00:43:11 +01:00
|
|
|
* @return array{tokens: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}[]}, types: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}}}
|
2025-05-12 00:23:28 +01:00
|
|
|
*/
|
|
|
|
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().
|
2025-05-29 18:12:17 +01:00
|
|
|
*
|
|
|
|
* @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>
|
2025-05-12 00:23:28 +01:00
|
|
|
*/
|
2025-05-29 18:12:17 +01:00
|
|
|
function opd_daily_emails_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleableMetadata) : array {
|
2025-05-12 00:23:28 +01:00
|
|
|
$replacements = [];
|
|
|
|
|
|
|
|
if ($type === 'opd-daily-emails') {
|
|
|
|
foreach ($tokens as $name => $original) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'email-count':
|
2025-05-12 01:13:25 +01:00
|
|
|
$dailyEmailRepository = \Drupal::service(DailyEmailNodeRepository::class);
|
|
|
|
|
2025-05-12 08:31:19 +01:00
|
|
|
$dailyEmails = $dailyEmailRepository->getAll();
|
|
|
|
|
|
|
|
$replacements[$original] = $dailyEmails->count();
|
2025-05-12 00:23:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|
|
|
|
|