Creating a token for a specific term

Last updated on
28 November 2024

This snippet allows you to use it on pathauto URL aliases in order to be replaced with one term of a specific vocabulary.

The first variable sets the default text that will be use for the token if no term is found for a specific vocabulary.
The second variable sets the vocabulary ID to search a term for.

$term_name = 'no-term';
$vocabulary_to_find = 1;
foreach ((array) $node->taxonomy as $term) {
  // Numeric values can happen on node submission
  if (is_numeric($term)) {
    $term = taxonomy_get_term($term);
  }
  if ($term->vid == $vocabulary_to_find) {
     $brand = strtolower(str_replace(' ', '-', check_plain($term->name)));
     break;
  }
}
return $term_name;

Help improve this page

Page status: Not set

You can: