function filehash_tokens in File Hash 8
Same name and namespace in other branches
- 7 filehash.tokens.inc \filehash_tokens()
Implements hook_tokens().
File
- ./
filehash.tokens.inc, line 36 - Tokens for file hash module.
Code
function filehash_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'file' && !empty($data['file'])) {
foreach (filehash_algos() as $algo) {
// Generate the file hash if it is needed but does not yet exist.
if (empty($data['file']->filehash[$algo])) {
foreach ($tokens as $name => $original) {
if (strpos($name, "filehash-{$algo}") === 0) {
$data['file']->filehash[$algo] = hash_file($algo, $data['file']
->getFileUri());
break;
}
}
}
if (isset($tokens["filehash-{$algo}"])) {
$replacements[$tokens["filehash-{$algo}"]] = $data['file']->filehash[$algo];
}
if (isset($tokens["filehash-{$algo}-pair-1"])) {
$replacements[$tokens["filehash-{$algo}-pair-1"]] = substr($data['file']->filehash[$algo], 0, 2);
}
if (isset($tokens["filehash-{$algo}-pair-2"])) {
$replacements[$tokens["filehash-{$algo}-pair-2"]] = substr($data['file']->filehash[$algo], 2, 2);
}
}
}
return $replacements;
}