You are here

function youtube_token_info_alter in YouTube Field 8

Same name and namespace in other branches
  1. 7 youtube.inc \youtube_token_info_alter()

Implements hook_token_info_alter().

Alters and adds tokens for each youtube field.

File

./youtube.module, line 467
Youtube field module adds a field for YouTube videos.

Code

function youtube_token_info_alter(&$data) {

  // Get all youtube fields. Gather entity_type and bundle information.
  $youtube_fields = [];
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $field_map = $entity_field_manager
    ->getFieldMap();
  foreach ($field_map as $entity_type_name => $fields) {
    foreach ($fields as $field_name => $field) {
      foreach ($field['bundles'] as $bundle) {
        if ($field['type'] == 'youtube') {
          $youtube_fields[] = [
            'entity_type' => $entity_type_name,
            'bundle' => $bundle,
            'field_name' => $field_name,
          ];
        }
      }
    }
  }
  foreach ($youtube_fields as $field) {
    $field_info = FieldConfig::loadByName($field['entity_type'], $field['bundle'], $field['field_name']);
    $field_label = $field_info ? $field_info
      ->getLabel() : '';

    // Modify the default field token.
    $data['tokens'][$field['entity_type']][$field['field_name']] = [
      'name' => $field_label . t(": Default"),
      'description' => t("The YouTube video field value's Default (or Token if exists) view mode output."),
    ];

    // Add two new tokens.
    $data['tokens'][$field['entity_type']][$field['field_name'] . '__youtube_video_url'] = [
      'name' => $field_label . t(": Video URL"),
      'description' => t("The YouTube video field value's youtube.com URL."),
    ];
    $data['tokens'][$field['entity_type']][$field['field_name'] . '__youtube_image_url'] = [
      'name' => $field_label . t(": Image URL"),
      'description' => t("The YouTube video field value's local image URL."),
    ];
  }
}