class HistoryTokensHooks

Token hook implementations for history.

Hierarchy

Expanded class hierarchy of HistoryTokensHooks

File

core/modules/history/src/Hook/HistoryTokensHooks.php, line 15

Namespace

Drupal\history\Hook
View source
class HistoryTokensHooks {
  use StringTranslationTrait;
  
  /**
   * Implements hook_token_info().
   */
  public function tokenInfo() : array {
    $tokens = [];
    // Provides an integration for each entity type except comment.
    foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class)) {
        continue;
      }
      if (\Drupal::service('comment.manager')->getFields($entity_type_id)) {
        // Get the correct token type.
        $token_type = $entity_type_id == 'taxonomy_term' ? 'term' : $entity_type_id;
        $tokens[$token_type]['comment-count-new'] = [
          'name' => $this->t("New comment count"),
          'description' => $this->t("The number of comments posted on an entity since the reader last viewed it."),
        ];
      }
    }
    return [
      'tokens' => $tokens,
    ];
  }
  
  /**
   * Implements hook_tokens().
   */
  public function tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) : array {
    $replacements = [];
    if ($type != 'comment' || empty($data['comment'])) {
      if (!empty($data[$type]) && $data[$type] instanceof FieldableEntityInterface) {
        $entity = $data[$type];
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'comment-count-new':
              $replacements[$original] = \Drupal::service(HistoryManager::class)->getCountNewComments($entity);
              break;

          }
        }
      }
    }
    return $replacements;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.