1

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:

www.mysite.com/mypage

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

www.mysite.com/archive/mypage

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:

www.mysite.com/archive/blog/mypage

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

0

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

<?php
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.

0

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') {
    return;
  }

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

  // 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 = '...';
}
1
  • I'd love to see an answer to this for Drupal 8. I imagine it's similar.
    – Spudley
    Commented 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.