$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(). * * @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 $tokens * @param array $data * @param array $options * * @return array */ 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; }