You are here

aet.module in Advanced Entity Tokens 2.x

Same filename and directory in other branches
  1. 7 aet.module

The AET main module file.

This file contains all of hooks and main programming logic of AET.

File

aet.module
View source
<?php

/**
 * @file
 * The AET main module file.
 *
 * This file contains all of hooks and main programming logic of AET.
 */
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 *
 * Declaring the AET tokens.
 */
function aet_token_info() {

  // Initializing the $info array.
  $info = [
    'types' => [],
    'tokens' => [],
  ];
  $info['types']['aet'] = [];
  $info['types']['aet']['name'] = t('Advanced Entity Tokens');
  $info['types']['aet']['description'] = t('AET Description');

  // Getting the information arrays on all the entities.
  $entities = \Drupal::service('entity_type.manager')
    ->getDefinitions();

  // Looping the entity information array to add the token types.
  foreach ($entities as $entity_type => $entity_info) {

    /** @var \Drupal\Core\Entity\EntityType $entity_info */
    $token_type = $entity_info
      ->get('additional')['token_type'] ?: $entity_type;
    if (empty($info['types'])) {
      $info['types'] = [];
    }
    if (empty($info['tokens'])) {
      $info['tokens'] = [];
    }
    $entity_id_key = $entity_info
      ->getKey('id');

    // Adding the entity element token.
    // The token key consists of the entity key id and a question mark
    // to mark this as the place to enter the entity's id.
    $info['tokens']['aet'][$token_type] = [];
    $info['tokens']['aet'][$token_type]['name'] = 'AET ' . $entity_info
      ->getBundleLabel() . ' entity';
    $info['tokens']['aet'][$token_type]['description'] = t("Reference a @entity_name entity by using it's @id_key", [
      '@entity_name' => $entity_info
        ->getBundleLabel(),
      '@id_key' => $entity_id_key,
    ]);
    $info['tokens']['aet'][$token_type]['dynamic'] = TRUE;
    $info['tokens']['aet'][$token_type]['type'] = $token_type;
  }
  return $info;
}

/**
 * Implements hook_tokens().
 */
function aet_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  if ($type !== 'aet') {
    return [];
  }
  return \Drupal::service('aet.token_replacer')
    ->setOptions($options)
    ->setBubbleableMetadata($bubbleable_metadata)
    ->getReplacements($tokens);
}

Functions

Namesort descending Description
aet_tokens Implements hook_tokens().
aet_token_info Implements hook_token_info().