Skip to content

Instantly share code, notes, and snippets.

@driesd
Created September 3, 2013 07:36
Show Gist options
  • Save driesd/6420744 to your computer and use it in GitHub Desktop.
Save driesd/6420744 to your computer and use it in GitHub Desktop.
Provides a custom token in Drupal
name = "Custom tokens"
description = "Custom module to provide custom tokens"
package = "Custom"
core = 7.x
<?php
/**
* Implementation of hook_token_info().
*/
function custom_tokens_token_info() {
$type = array(
'name' => t("Custom token"),
'description' => t("Custom tokens"),
);
$custom['vocabulary_name'] = array(
'name' => t("Vocabulary name"),
'description' => t("Sanitized vocabulary name for us in the url alias"),
);
return array(
'types' => array(
'custom' => $type,
),
'tokens' => array(
'custom' => $custom,
),
);
}
/**
* Implements hook_tokens
*/
function custom_tokens_tokens($type, $tokens, array $data = array(), array $options = array('sanitize' => FALSE)) {
$replacements = array(); // init the replacement list
// only execute in case of the correct token
if ($type == "custom" && !empty($data['term'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'vocabulary_name': // only execute in case of the correct token
// load the vocabulary you need
$vocabulary = taxonomy_vocabulary_load($data['term']->vid);
// get the translated taxonomy vocabulary name
$voc_i18n_name = i18n_taxonomy_vocabulary_name($vocabulary, $data['term']->language);
// add it to the list of tokens that have to be replaced
$replacements[$original] = $voc_i18n_name;
break;
}
}
}
// return the replacement list
// if nothing was added it will fallback to the empty list
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment