You are here

function smart_date_token_info in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 smart_date.tokens.inc \smart_date_token_info()
  2. 8 smart_date.tokens.inc \smart_date_token_info()
  3. 3.x smart_date.tokens.inc \smart_date_token_info()
  4. 3.1.x smart_date.tokens.inc \smart_date_token_info()
  5. 3.2.x smart_date.tokens.inc \smart_date_token_info()
  6. 3.3.x smart_date.tokens.inc \smart_date_token_info()
  7. 3.4.x smart_date.tokens.inc \smart_date_token_info()

Implements hook_token_info().

File

./smart_date.tokens.inc, line 17
Provides tokens for the smart_date module.

Code

function smart_date_token_info() {
  if (!\Drupal::hasService('token.entity_mapper')) {
    return;
  }
  $types = [];
  $tokens = [];
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->entityClassImplements(ContentEntityInterface::class)) {
      continue;
    }
    $token_type = \Drupal::service('token.entity_mapper')
      ->getTokenTypeForEntityType($entity_type_id);
    if (empty($token_type)) {
      continue;
    }

    // Build property tokens for all smart date fields.
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions($entity_type_id);
    foreach ($fields as $field_name => $field) {
      if ($field
        ->getType() != 'smartdate') {
        continue;
      }
      $tokens[$token_type . '-' . $field_name]['value-custom'] = [
        'name' => t('Start, custom format'),
        'description' => NULL,
        'dynamic' => TRUE,
        'module' => 'smart_date',
      ];
      $tokens[$token_type . '-' . $field_name]['end_value-custom'] = [
        'name' => t('End, custom format'),
        'description' => NULL,
        'dynamic' => TRUE,
        'module' => 'smart_date',
      ];
      $tokens[$token_type . '-' . $field_name]['format'] = [
        'name' => t('Formatted by Smart Date Format'),
        'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
        'dynamic' => TRUE,
        'module' => 'smart_date',
      ];
      $tokens[$token_type . '-' . $field_name]['value-format'] = [
        'name' => t('Start, formatted by Smart Date Format'),
        'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
        'dynamic' => TRUE,
        'module' => 'smart_date',
      ];
      $tokens[$token_type . '-' . $field_name]['end_value-format'] = [
        'name' => t('End, formatted by Smart Date Format'),
        'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
        'dynamic' => TRUE,
        'module' => 'smart_date',
      ];
    }
  }
  return [
    'types' => $types,
    'tokens' => $tokens,
  ];
}