0

All my node paths have a URL alias, provided by Pathauto. The pattern is [node:title]. The generated aliases for nodes with the same title are by default

  • title,
  • title-0,
  • title-1,

etc. I would like to change this numbering into

  • title-1,
  • title-2,
  • title-3,

etc. So, when a node with the title Test is submitted, a URL alias test should be generated. Once a second node with the same title is published, another alias test-1 should be created for the first node and a new alias test-2 should be added for the newly created node.

How can I do that?

1

pathauto_alias_uniquify(&$alias, $source, $langcode) is the function that does the suffixing to generate uniqueness. It has a hard-coded value that starts at 0.

However, Pathauto evokes a hook, hook_pathauto_is_alias_reserved() when it checks for uniqueness when generating its path alias. If you have a custom module evoke this hook, you can check reject instances of the -0 suffixing and tell pathauto to move onto the next entry.

/**
 * Implements hook_pathauto_is_alias_reserved().
 */
function MYMODULE_pathauto_is_alias_reserved($alias, $source, $langcode) {
  // Return FALSE when the alias ends with '-0'
  $zero_suffix = variable_get('pathauto_separator', '-') . '0';
  return (substr($alias, -strlen($zero_suffix)) == $zero_suffix);
}

This might catch entries that legitimately have "-0" in the path, but I'll leave to the reader to devise a workaround for that scenario.

  • Thanks, I adapted your code to start the suffixing from 2. Now, for consistency, I would like to add a new alias test-1 for the first node with title Test (that means this node will have two aliases: test and test-1). Which hook should I use to do that? Or do you advise against this? I will already give you an upvote. – Jeroen Aug 27 '16 at 16:03
  • Look at the pathauto.api.php to see what's available. You can use path_save() to duplicate/modify the created pathauto path. – Shawn Conn Aug 27 '16 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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