Skip to content

Instantly share code, notes, and snippets.

@cube-dan
Forked from stevesmename/yourmodule.tokens.inc
Created September 9, 2021 19:57
Show Gist options
  • Save cube-dan/625b665108675268da14502fe4857fe9 to your computer and use it in GitHub Desktop.
Save cube-dan/625b665108675268da14502fe4857fe9 to your computer and use it in GitHub Desktop.
[file:description] token working with 'Download link' format
<?php
/**
* @file
* Token integration for the file_entity module.
*/
/**
* Implements hook_token_info().
*/
function hook_token_info() {
// File tokens.
$info['tokens']['file']['description'] = array(
'name' => t('File description'),
'description' => t('The description of the file.'),
'type' => 'file',
);
return $info;
}
/**
* Implements hook_tokens().
*/
function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
// File tokens.
if ($type == 'file' && !empty($data['file'])) {
$file = $data['file'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'description':
$replacements[$original] = $sanitize ? check_plain($file->description) : $file->description;
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment