function filefield_paths_tokens in File (Field) Paths 8
Same name and namespace in other branches
- 7 filefield_paths.tokens.inc \filefield_paths_tokens()
Implements hook_tokens().
File
- ./
filefield_paths.tokens.inc, line 35 - Tokens for the File (Field) Paths module.
Code
function filefield_paths_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$url_options = [
'absolute' => TRUE,
];
if (isset($language)) {
$url_options['language'] = $language;
}
$replacements = [];
if ($type == 'file' && !empty($data['file'])) {
$file_system = \Drupal::service('file_system');
/** @var \Drupal\file\Entity\File $file */
$file = $data['file'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'ffp-name-only':
$basename = $file_system
->basename($file->filename->value);
$extension = preg_match('/\\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
$replacements[$original] = !is_null($extension) ? mb_substr($basename, 0, mb_strlen($basename) - mb_strlen($extension)) : $basename;
break;
case 'ffp-name-only-original':
$basename = $file_system
->basename($file->origname->value);
$extension = preg_match('/\\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
$replacements[$original] = !is_null($extension) ? mb_substr($basename, 0, mb_strlen($basename) - mb_strlen($extension)) : $basename;
break;
case 'ffp-extension-original':
$replacements[$original] = preg_match('/[^.]+$/', $file_system
->basename($file->origname->value), $matches) ? $matches[0] : NULL;
break;
}
}
}
return $replacements;
}