You are here

current_page_entity_tokens.module in Current Page Entity Tokens 2.0.x

File

current_page_entity_tokens.module
View source
<?php

/**
 * @file
 * Contains current_page_entity_tokens.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_help().
 */
function current_page_entity_tokens_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the current_page_entity_tokens module.
    case 'help.page.current_page_entity_tokens':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Make tokens from the current request&#039;s entity available') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_token_info().
 */
function current_page_entity_tokens_token_info() {
  $info = [];
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $entity_type_id => $entity_type) {

    // Do not generate tokens if the entity doesn't define a token type or
    // is not a content entity.
    if (!$entity_type
      ->get('token_type') || !$entity_type instanceof ContentEntityTypeInterface) {
      continue;
    }
    $info['tokens']['current-page'][$entity_type_id] = [
      'name' => t('The current %type', [
        '%type' => $entity_type_id,
      ]),
      'description' => t("The current page object if that's a %type", [
        '%type' => $entity_type_id,
      ]),
      'type' => \Drupal::service('token.entity_mapper')
        ->getTokenTypeForEntityType($entity_type_id),
    ];
  }
  return $info;
}

/**
 * Implements hook_tokens().
 */
function current_page_entity_tokens_tokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'current-page') {
    $request = \Drupal::request();
    $entities = \Drupal::entityTypeManager()
      ->getDefinitions();
    foreach ($tokens as $name => $original) {
      $parts = explode(':', $name);
      $entity_type = $parts[0];
      if (!isset($entities[$entity_type])) {
        continue;
      }

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $request->attributes
        ->get($entity_type);
      if (empty($entity)) {
        continue;
      }

      // For [current-page:ENTITY] (no subproperties), substitute the label.
      if ($name == $entity_type) {
        $label = $entity
          ->label();
        $replacements[$original] = $label;
      }
      else {
        $entity_tokens = \Drupal::token()
          ->findWithPrefix($tokens, $entity_type);
        $token_type = \Drupal::service('token.entity_mapper')
          ->getTokenTypeForEntityType($entity_type);
        $replacements += \Drupal::token()
          ->generate($token_type, $entity_tokens, [
          $token_type => $entity,
        ], $options, $bubbleable_metadata);
      }
    }
  }
  return $replacements;
}