Make URL Patterns Match Menu Position in Drupal

Posted on November 24, 2015 - 2 Minute Read

Drupal's Pathauto is a must have for any Drupal project. This tutorial will demonstrate a simple yet effective way to utilize Pathauto to create custom URL patterns that match the node's menu position.

Below is an example of the final product. The left side is the menu hierarchy, and the right side is the URL pattern that will be generated.

path alias menu pattern

Enable The Following Modules

In order to create custom URL patterns that match the node's menu position you will need to install and enable the following modules and their dependencies.

  1. Pathauto
  2. Pathauto Menu Link

Screen Shot 2015 11 23 at 8 20 46 PM

The reason we also install ​Pathauto Menu Link can be explained in the module's description.

If you use parent menu links to generate paths within Pathauto, you'll notice that the resulting path is only updated when a node is saved, which means that if you simply move a menu link item within a menu so that it has a different parent, the path is immediately out of date and no longer correct.

This module fixes that by forcing an update of the Pathauto-generated path when a menu link item has been updated, based of the link's new position within the menu tree.

Create A Path Alias

Now that you've installed and enabled Pathauto and Pathauto Menu Link, you can create a path alias that will match the node's menu position.

  1. Navigate to admin/config/search/path/patterns and create a new path alias using [node:menu-link:parents:join-path]/[node:title]. Screen Shot 2015 11 23 at 8 52 47 PM

Add Nodes To a Menu

Now when you add a node to a menu, it will automatically add the node's menu parent to the path alias. If the node doesn't have a parent, the alias will simply default to the node title.

Categorized In:Tagged In:

Learn Ruby on Rails For Free

I created Rails Code Along to help teach you how to build, test, troubleshoot, and launch a REAL production Rails application. This course is free, and does not require you to sign up.

Learn Ruby on Rails For Free

Let's Connect