Skip to content

Instantly share code, notes, and snippets.

@soulston
Created July 9, 2013 13:40
Show Gist options
  • Save soulston/5957438 to your computer and use it in GitHub Desktop.
Save soulston/5957438 to your computer and use it in GitHub Desktop.
/**
* @file
* Provides special token to use with pathauto.
*
* http://drupal.org/node/1308488
*/
// We are using the sanitization functions from pathauto
require_once(drupal_get_path('module', 'pathauto') . '/pathauto.inc');
/**
* Provide information about our custom placeholder/token.
*
* http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_token_info/7
* http://api.lullabot.com/token_example_token_info/7
*
* @return array
* An associative array of available tokens and token types.
*/
function ln_extra_tokens_token_info() {
$info['types']['customdfp'] = array(
'name' => t('Custom DFP'),
'description' => t("Special DFP tokens."),
'type' => 'customdfp',
);
// Tokens for the customdfp token type.
$info['tokens']['customdfp']['termlineage'] = array(
'name' => t('Full term lineage'),
'description' => t("All the parent taxonomy terms for a term ID including the term its self."),
);
$info['tokens']['customdfp']['parent-termlineage'] = array(
'name' => t('Full term lineage'),
'description' => t("All the parent taxonomy terms for a term ID."),
);
// Tokens for the term token type.
$info['tokens']['term']['termlineage'] = array(
'name' => t('Full term lineage'),
'description' => t("All the parent taxonomy terms for a term ID including the term its self."),
);
$info['tokens']['term']['parent-termlineage'] = array(
'name' => t('Parent term lineage'),
'description' => t("All the parent taxonomy terms for a term ID."),
);
$info['tokens']['term']['parent-termlineage-dash'] = array(
'name' => t('Parent term lineage'),
'description' => t("All the parent taxonomy terms for a term ID delimited with -."),
);
return $info;
}
/**
* Provide a token giving all term parents and the term itsself on any taxonomy/term/tid page.
*
* http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_tokens/7
* http://api.lullabot.com/token_example_tokens/7
*
* @param string $type
* The machine-readable name of the type (group) of token being replaced, such
* as 'node', 'user', or another type defined by a hook_token_info()
* implementation.
* @param array $tokens
* An array of tokens to be replaced. The keys are the machine-readable token
* names, and the values are the raw [type:token] strings that appeared in the
* original text.
* @param array $data (optional)
* An associative array of data objects to be used when generating replacement
* values, as supplied in the $data parameter to token_replace().
* @param array $options (optional)
* An associative array of options for token replacement; see token_replace()
* for possible values.
* @return array
* An associative array of replacement values, keyed by the raw [type:token]
* strings from the original text.
*/
function ln_extra_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
if ($type == 'customdfp') {
$data = '';
foreach ($tokens as $name => $original) {
switch ($name) {
case 'termlineage':
$lineage = '';
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$lineage = _ln_extra_tokens_full_term_lineage(arg(2));
}
$replacements[$original] = $lineage;
break;
case 'parent-termlineage':
$lineage = '';
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
$lineage = _ln_extra_tokens_full_term_lineage(arg(2), FALSE);
}
$replacements[$original] = $lineage;
break;
}
}
}
// Taxonomy term tokens.
if ($type == 'term' && !empty($data['term'])) {
$term = $data['term'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'termlineage':
$replacements[$original] = _ln_extra_tokens_full_term_lineage($term->tid);
break;
case 'parent-termlineage':
$replacements[$original] = _ln_extra_tokens_full_term_lineage($term->tid, FALSE);
break;
case 'parent-termlineage-dash':
$replacements[$original] = _ln_extra_tokens_full_term_lineage($term->tid, FALSE, '-');
break;
}
}
}
return $replacements;
}
/**
* Concatenate all the parent terms and the current term.
*/
function _ln_extra_tokens_full_term_lineage($tid, $include_current = TRUE, $delimiter = '/') {
if (is_numeric($tid)) {
$terms = array();
$lineage = array();
$terms = taxonomy_get_parents_all($tid);
foreach ($terms as $key) {
if ($include_current || $key->tid != $tid) {
// Use pathauto_cleanstring to clean term names
$lineage[] = pathauto_cleanstring($key->name);
}
}
$lineage = array_reverse($lineage);
$lineage = implode($delimiter, $lineage);
return $lineage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment