You are here

function file_entity_tokens in File Entity (fieldable files) 8.2

Same name and namespace in other branches
  1. 7.3 file_entity.tokens.inc \file_entity_tokens()
  2. 7 file_entity.tokens.inc \file_entity_tokens()
  3. 7.2 file_entity.tokens.inc \file_entity_tokens()

Implements hook_tokens().

File

./file_entity.tokens.inc, line 64
Token integration for the file_entity module.

Code

function file_entity_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['langcode'])) {
    $langcode = $options['langcode'];
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($langcode);
  }
  else {
    $langcode = NULL;
  }

  // File tokens.
  if ($type == 'file' && !empty($data['file'])) {
    $file = $data['file'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'type':
          if ($file_type = FileType::load($file
            ->bundle())) {
            $bubbleable_metadata
              ->addCacheableDependency($file_type);
            $replacements[$original] = $file_type
              ->label();
          }
          break;
        case 'download-url':
          $replacements[$original] = $file
            ->downloadUrl($url_options)
            ->toString();
          break;
      }
    }

    // Chained token relationships.
    $token_service = \Drupal::service('token');
    if (($file_type_tokens = $token_service
      ->findWithPrefix($tokens, 'type')) && ($file_type = FileType::load($file
      ->bundle()))) {
      $replacements += $token_service
        ->generate('file-type', $file_type_tokens, array(
        'file_type' => $file_type,
      ), $options, $bubbleable_metadata);
    }
    if ($download_url_tokens = $token_service
      ->findWithPrefix($tokens, 'download-url')) {
      $replacements += $token_service
        ->generate('url', $download_url_tokens, $file
        ->downloadUrl()
        ->toString(), $options, $bubbleable_metadata);
    }
  }

  // File type tokens.
  if ($type == 'file-type' && !empty($data['file_type'])) {
    $file_type = $data['file_type'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $file_type
            ->label();
          break;
        case 'machine-name':

          // This is a machine name so does not ever need to be sanitized.
          $replacements[$original] = $file_type
            ->id();
          break;
        case 'count':
          $query = \Drupal::database()
            ->select('file_managed');
          $query
            ->condition('type', $file_type
            ->id());
          $query
            ->addTag('file_type_file_count');
          $count = $query
            ->countQuery()
            ->execute()
            ->fetchField();
          $replacements[$original] = (int) $count;
          break;
        case 'edit-url':
          $replacements[$original] = Url::fromUri('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options)
            ->toString();
          break;
      }
    }
  }
  return $replacements;
}