I'm using Pathauto to produce nice URLs for my Drupal 7 site, and have run into the need to edit the URLs based on a node field value.

Specifically, when the content is archived on my website, the URL needs to be automatically changed to include the substring 'archive'. So when editing this page:


and node:field-status is changed from none to archived, I need the URL to become:


Also, pages that already feature a substring, such as mysite.com/blog/mypage would ideally have the new substring prepended to the relative URL, so:


I have tried using Flags and Rules to rewrite the URL pattern based on the field value, but with no luck. Any ideas?

2 Answers 2


You can build a small module, called myalias and use this code to generate custom alias

function myalias_node_insert($node){
    //here you can access the $node variable and get its fields value  
    $node->path['alias'] = 'your_custom_path';

You can also change the hook to myalias_node_update($node) to alter the node alias once it is updated.


You can use hook_pathauto_pattern_alter(). Something like this should work for a boolean field (untested)...

 * Alter the pattern to be used before an alias is generated by Pathauto.
 * This hook will only be called if a default pattern is configured (on
 * admin/config/search/path/patterns).
 * @param string $pattern
 *   The alias pattern for Pathauto to pass to token_replace() to generate the
 *   URL alias.
 * @param array $context
 *   An associative array of additional options, with the following elements:
 *   - 'module': The module or entity type being aliased.
 *   - 'op': A string with the operation being performed on the object being
 *     aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
 *   - 'source': A string of the source path for the alias (e.g. 'node/1').
 *   - 'data': An array of keyed objects to pass to token_replace().
 *   - 'type': The sub-type or bundle of the object being aliased.
 *   - 'language': A string of the language code for the alias (e.g. 'en').
 *     This can be altered by reference.
function MYMODULE_pathauto_pattern_alter(&$pattern, array $context) {
  // Exit if we don't care about this entity type and bundle.
  if ($context['module'] != 'node' || $context['type'] != 'article') {

  // Exit if the archive field doesn't exist or isn't set.
  $node = $context['data']['node'];
  $items = field_get_items($context['module'], $node, 'FIELDNAME');
  if ($items === FALSE || empty($items[0]['value']) {

  // Modify the pattern.
  // (I'd try to avoid hardcoding any patterns in this code, but rather use
  // variables. These can either be updated programatically or tied to a UI
  // element.)
  $pattern = '...';
  • I'd love to see an answer to this for Drupal 8. I imagine it's similar.
    – Spudley
    Feb 6, 2020 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.