You are here

function field_tokens_token_info in Field tokens 8

Same name and namespace in other branches
  1. 7 field_tokens.tokens.inc \field_tokens_token_info()

Implements hook_token_info().

File

./field_tokens.tokens.inc, line 18
Token functions for Field tokens module.

Code

function field_tokens_token_info() {

  /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $field_types = $field_type_manager
    ->getDefinitions();

  /** @var \Drupal\Core\Field\FormatterPluginManager $formatter_manager */
  $formatter_manager = \Drupal::service('plugin.manager.field.formatter');
  $formatters = $formatter_manager
    ->getDefinitions();

  /** @var \Drupal\field\FieldConfigStorage $field_config_storage */
  $field_config_storage = \Drupal::entityTypeManager()
    ->getStorage('field_config');
  $fields = $field_config_storage
    ->loadMultiple();

  /** @var \Drupal\token\TokenEntityMapper $token_entity_mapper */
  $token_entity_mapper = \Drupal::service('token.entity_mapper');
  $types = [];
  $tokens = [];

  // Build token types for each field type.
  foreach ($field_types as $field_type_name => $field_type_info) {

    // Build tokens for each formatter of the current field type.
    foreach ($formatters as $formatter_name => $formatter_info) {
      if (in_array($field_type_name, $formatter_info['field_types'])) {
        $tokens["formatted_field-{$field_type_name}"][$formatter_name] = [
          'name' => $formatter_info['label'],
          'description' => t('@label formatter.', [
            '@label' => $formatter_info['label'],
          ]),
          'dynamic' => TRUE,
        ];

        // Adjust token name and description if formatter has no settings.
        $default_settings = $formatter_manager
          ->getDefaultSettings($formatter_name);
        if (empty($default_settings)) {
          unset($tokens["formatted_field-{$field_type_name}"]["{$formatter_name}"]['dynamic']);
        }
        else {
          $settings = [];
          foreach ($default_settings as $key => $default) {
            $settings[] = $key;
          }
          $tokens["formatted_field-{$field_type_name}"]["{$formatter_name}"]['description'] .= ' ' . t("Replace '?' with formatter settings in the following format: 'SETTING-VALUE:SETTING-VALUE-...'. Available settings: @settings", [
            '@settings' => implode(', ', $settings),
          ]);
        }
      }
    }
    if (!empty($tokens["formatted_field-{$field_type_name}"])) {
      $types["formatted_field-{$field_type_name}"] = [
        'name' => t('Formatted @label field', [
          '@label' => $field_type_info['label'],
        ]),
        'description' => t('Tokens related to Formatted @label fields.', [
          '@label' => $field_type_info['label'],
        ]),
        'needs-data' => "formatted_field-{$field_type_name}",
      ];
    }

    // Build tokens for each property of the current field type.

    /** @var \Drupal\field\Entity\FieldConfig $field */
    foreach ($fields as $field) {
      if ($field
        ->getType() == $field_type_name) {
        $properties = $field
          ->getFieldStorageDefinition()
          ->getPropertyDefinitions();

        /** @var \Drupal\Core\TypedData\DataDefinition $data_definition */
        foreach ($properties as $property_name => $data_definition) {
          $tokens["field_property-{$field_type_name}"][$property_name] = [
            'name' => $data_definition
              ->getLabel(),
            'description' => t('@label property.', [
              '@label' => $data_definition
                ->getLabel(),
            ]),
          ];
          if ($data_definition instanceof DataReferenceDefinition) {
            $tokens["field_property-{$field_type_name}"][$property_name]['dynamic'] = TRUE;
            if ($data_definition
              ->getConstraint('EntityType')) {
              $token_type = $token_entity_mapper
                ->getTokenTypeForEntityType($data_definition
                ->getConstraint('EntityType'));
              $tokens["field_property-{$field_type_name}"][$property_name]['dynamic'] = FALSE;
              $tokens["field_property-{$field_type_name}"][$property_name]['type'] = $token_type;
            }
          }
        }
      }
    }
    if (!empty($tokens["field_property-{$field_type_name}"])) {
      $types["field_property-{$field_type_name}"] = [
        'name' => t('@label field properties', [
          '@label' => $field_type_info['label'],
        ]),
        'description' => t('Properties of the @label fields.', [
          '@label' => $field_type_info['label'],
        ]),
        'needs-data' => "field_property-{$field_type_name}",
      ];
    }
  }
  return [
    'types' => $types,
    'tokens' => $tokens,
  ];
}