You are here

function file_tokens in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/file.module \file_tokens()

Implements hook_tokens().

File

core/modules/file/file.module, line 937
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

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

    /** @var \Drupal\file\FileInterface $file */
    $file = $data['file'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Basic keys and values.
        case 'fid':
          $replacements[$original] = $file
            ->id();
          break;

        // Essential file data
        case 'name':
          $replacements[$original] = $file
            ->getFilename();
          break;
        case 'path':
          $replacements[$original] = $file
            ->getFileUri();
          break;
        case 'mime':
          $replacements[$original] = $file
            ->getMimeType();
          break;
        case 'size':
          $replacements[$original] = format_size($file
            ->getSize());
          break;
        case 'url':
          $replacements[$original] = file_create_url($file
            ->getFileUri());
          break;

        // These tokens are default variations on the chained tokens handled below.
        case 'created':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata
            ->addCacheableDependency($date_format);
          $replacements[$original] = format_date($file
            ->getCreatedTime(), 'medium', '', NULL, $langcode);
          break;
        case 'changed':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata = $bubbleable_metadata
            ->addCacheableDependency($date_format);
          $replacements[$original] = format_date($file
            ->getChangedTime(), 'medium', '', NULL, $langcode);
          break;
        case 'owner':
          $owner = $file
            ->getOwner();
          $bubbleable_metadata
            ->addCacheableDependency($owner);
          $name = $owner
            ->label();
          $replacements[$original] = $name;
          break;
      }
    }
    if ($date_tokens = $token_service
      ->findWithPrefix($tokens, 'created')) {
      $replacements += $token_service
        ->generate('date', $date_tokens, array(
        'date' => $file
          ->getCreatedTime(),
      ), $options, $bubbleable_metadata);
    }
    if ($date_tokens = $token_service
      ->findWithPrefix($tokens, 'changed')) {
      $replacements += $token_service
        ->generate('date', $date_tokens, array(
        'date' => $file
          ->getChangedTime(),
      ), $options, $bubbleable_metadata);
    }
    if (($owner_tokens = $token_service
      ->findWithPrefix($tokens, 'owner')) && $file
      ->getOwner()) {
      $replacements += $token_service
        ->generate('user', $owner_tokens, array(
        'user' => $file
          ->getOwner(),
      ), $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}