Created
February 23, 2018 19:32
-
-
Save tmbritton/197c36dd290807a71390b0af94832637 to your computer and use it in GitHub Desktop.
Custom token in Drupal 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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