function TokenHooks::tokens

Implements hook_tokens().

File

core/modules/file/src/Hook/TokenHooks.php, line 30

Class

TokenHooks
Hook implementations for file tokens.

Namespace

Drupal\file\Hook

Code

public function tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) : array {
    $url_options = [
        'absolute' => TRUE,
    ];
    if (isset($options['langcode'])) {
        $url_options['language'] = $this->languageManager
            ->getLanguage($options['langcode']);
        $langcode = $options['langcode'];
    }
    else {
        $langcode = NULL;
    }
    $replacements = [];
    if ($type == 'file' && !empty($data['file'])) {
        $dateFormatStorage = $this->entityTypeManager
            ->getStorage('date_format');
        
        /** @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;
                case 'uuid':
                    $replacements[$original] = $file->uuid();
                    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] = ByteSizeMarkup::create($file->getSize());
                    break;
                case 'url':
                    // Ideally, this would use return a relative URL, but because tokens
                    // are also often used in emails, it's better to keep absolute file
                    // URLs. The 'url.site' cache context is associated to ensure the
                    // correct absolute URL is used in case of a multisite setup.
                    $replacements[$original] = $file->createFileUrl(FALSE);
                    $bubbleable_metadata->addCacheContexts([
                        'url.site',
                    ]);
                    break;
                // These tokens are default variations on the chained tokens handled below.
                case 'created':
                    $date_format = $dateFormatStorage->load('medium');
                    $bubbleable_metadata->addCacheableDependency($date_format);
                    $replacements[$original] = $this->dateFormatter
                        ->format($file->getCreatedTime(), 'medium', '', NULL, $langcode);
                    break;
                case 'changed':
                    $date_format = $dateFormatStorage->load('medium');
                    $bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
                    $replacements[$original] = $this->dateFormatter
                        ->format($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 = $this->token
            ->findWithPrefix($tokens, 'created')) {
            $replacements += $this->token
                ->generate('date', $date_tokens, [
                'date' => $file->getCreatedTime(),
            ], $options, $bubbleable_metadata);
        }
        if ($date_tokens = $this->token
            ->findWithPrefix($tokens, 'changed')) {
            $replacements += $this->token
                ->generate('date', $date_tokens, [
                'date' => $file->getChangedTime(),
            ], $options, $bubbleable_metadata);
        }
        if (($owner_tokens = $this->token
            ->findWithPrefix($tokens, 'owner')) && $file->getOwner()) {
            $replacements += $this->token
                ->generate('user', $owner_tokens, [
                'user' => $file->getOwner(),
            ], $options, $bubbleable_metadata);
        }
    }
    return $replacements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.