You are here

function filehash_tokens in File Hash 7

Same name and namespace in other branches
  1. 8 filehash.tokens.inc \filehash_tokens()

Implements hook_tokens().

File

./filehash.tokens.inc, line 34
Tokens for File Hash module.

Code

function filehash_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  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']->uri);
            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;
}