file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata)
Implements hook_tokens().
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': // Ideally, this would use file_url_transform_relative(), but because // tokens are also often used in e-mails, 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_create_url($file->getFileUri()); $bubbleable_metadata->addCacheContexts(['url.site']); 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; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/file_tokens/8.1.x