You are here

function views_field_formatter_token_info_alter in Views field formatter 8.2

Implements hook_token_info_alter().

File

./views_field_formatter.module, line 35

Code

function views_field_formatter_token_info_alter(array &$info) {
  $type_info = \Drupal::service('plugin.manager.field.field_type')
    ->getDefinitions();
  if (!\Drupal::moduleHandler()
    ->moduleExists('token')) {
    return;
  }
  foreach (\Drupal::entityTypeManager()
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
      continue;
    }

    // Make sure a token type exists for this entity.
    $token_type = \Drupal::service('token.entity_mapper')
      ->getTokenTypeForEntityType($entity_type_id);
    if (empty($token_type) || !isset($info['types'][$token_type])) {
      continue;
    }
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions($entity_type_id);
    foreach ($fields as $field_name => $field) {

      /** @var \Drupal\field\FieldStorageConfigInterface $field */

      // Ensure the token implements FieldStorageConfigInterface or is defined
      // in token module.
      $provider = '';
      if (isset($info['types'][$token_type]['module'])) {
        $provider = $info['types'][$token_type]['module'];
      }
      if (!$field instanceof FieldStorageConfigInterface && $provider !== 'token') {
        continue;
      }
      if ($token_type === 'comment' && $field_name === 'comment_body') {

        // Core provides the comment field as [comment:body].
        continue;
      }

      // Do not define the token type if the field has no properties.
      if (!$field
        ->getPropertyDefinitions()) {
        continue;
      }

      // Generate a description for the token.
      $labels = _token_field_label($entity_type_id, $field_name);
      $label = \array_shift($labels);
      $params['@type'] = $type_info[$field
        ->getType()]['label'];
      $cardinality = $field
        ->getCardinality();
      $field_token_name = $token_type . '-' . $field_name;
      if ($cardinality === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $cardinality > 1) {
        $info['tokens']["list<{$field_token_name}>"]['delta'] = [
          'name' => t('@type type current delta', [
            '@type' => Html::escape($label),
          ]),
          'module' => 'token',
          'type' => $field_token_name,
        ];
        $info['tokens']["list<{$field_token_name}>"]['current'] = [
          'name' => t('@type type with current delta', [
            '@type' => Html::escape($label),
          ]),
          'module' => 'token',
          'type' => $field_token_name,
        ];
        $info['tokens']["list<{$field_token_name}>"]['concat:,'] = [
          'name' => t('@type type concatenated values', [
            '@type' => Html::escape($label),
          ]),
          'module' => 'token',
          'type' => $field_token_name,
        ];
      }
    }
  }
}