Skip to content

Instantly share code, notes, and snippets.

@tmbritton
Created February 23, 2018 19:32
Show Gist options
  • Save tmbritton/197c36dd290807a71390b0af94832637 to your computer and use it in GitHub Desktop.
Save tmbritton/197c36dd290807a71390b0af94832637 to your computer and use it in GitHub Desktop.
Custom token in Drupal 7
/**
* Implements hook_token_info().
*/
function modulename_token_info() {
// Add tokens.
$info['types']['token-type'] = array(
'name' => t('ModuleName Tokens'),
'description' => t('ModuleName custom tokens'),
);
$info['tokens']['token-type']['token-name'] = array(
'name' => t('Token Name Token'),
'description' => t('Does what your tokens do.'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function modulename_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'token-type') {
foreach ($tokens as $name => $original) {
switch ($name) {
// Get the file id from a media field and return the image url.
case 'token-name':
$replacement = // Do something here.
$replacements[$original] = $replacement;
break;
}
}
}
// Return the replacements.
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment