[Drupal] How to create a custom token for setting URL alias pattern

| | 2 min read

Recently, we had a requirement to change the article's URL alias of Drupal sites to a custom format. For creating custom URL paths using taxonomy terms, all you need to do is to configure the URL alias pattern for Article paths. The solution seems simple, however we couldn't build the required pattern as the available tokens were not just enough. After some trial and error we came up with a solution. We have created custom tokens in Drupal and that worked like a charm.

They were following a specific content structure for the website. There was a taxonomy vocabulary based on the following scheme:

  • category-1
    • category-1 > subcategory-1.1
    • category-1 > subcategory-1.2
  • category-2
    • category-2 > subcategory-2.1

We are required to reflect this taxonomy hierarchy in the article page URL path like - content-type/category-name/subcategory-name/nodetitle-nodenumber.

Prerequisites: We need the modules Pathauto, Token and Entity API installed.

Here we needed custom tokens in Drupal to make the complete pattern. To achieve this we did the following:

Steps to create Custom Token in Drupal

1. Created new custom replacement tokens - [node:customcategory] and [node:customsubcategory]

/**
 * Implementation of hook_token_info().
 */

function custommodule_token_info() {
  // Found in the node replacement patterns
  $info['tokens']['node']['customcategorytoken'] = array(
      'name' => t('Custom Category'),
      'description' => t('Custom token created for Category'),
  );

  $info['tokens']['node']['customsubcategorytoken'] = array(
      'name' => t('Custom Sub-category'),
      'description' => t('Custom token created for Sub-category'),
  );

  return $info;
}
/**
 * Implementation of hook_tokens().
 */
 
function custommodule_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'customcategorytoken':
          $to_replace = get_custom_category_token_value($node->field_category[$node->language][0]['tid']);
          $replacements[$original] = $to_replace;
          break;

        case 'customsubcategorytoken':
          $to_replace = get_custom_category_token_value($node->field_category[$node->language][1]['tid']);
          $replacements[$original] = $to_replace;
          break;
      }
    }
  }
  return $replacements;
}
// Get the value for your custom token

function get_custom_category_token_value($tid){
  $term = taxonomy_term_load($tid);
  return $term->name;
}

2. Added the custom URL alias for Article's path - articles/[node:customcategory]/[node:customsubcategory]/[node:title]-[node:nid].

Steps:

  1. Go to URL Aliases Administration » Configuration » Search and metadata » URL aliases.
  2. Expand replacement patterns for 'Content paths' then, expand 'Nodes' to find the required tokens.
  3. Copy the corresponding tokens to the field - 'Pattern for all Article paths' and make the pattern.
  4. Save the configuration.

3. Performed bulk updating URL Alias for all content paths.

Done! Hope this helps.

Zyxware is a leading drupal development company in India that has operations in the US, UK, Canada, Australia, and the Middle East. Check out if you are looking to build a career in Drupal.