In my last post on Menu Tricks, I outlined a module called Menu Block, that made it possible to use just one giant main menu to manage your site navigation, while still displaying lower level items in different regions of your design and on different pages on your site.
Now that we're using the main menu as an architectural framework for our website, not just as a way to place some links on the top of site, more automated functionality will become available. Drupal loves it when we systematize our structures and rewards us.
In this post, I'll discuss how you can automate your page URLs based on where they are positioned in the main menu.
Automating URLs Based on the Main Menu
To automate your page URLs based on menu position, you will need to enable the Pathauto module. This module is readily available on Stanford Sites. For sites hosted elsewhere, it is downloadable from here: http://drupal.org/project/pathauto
Basic Use of Pathauto
Most Drupal builders have used Pathauto before to create simple formulas for their site URLs based on content types. For instance, you might make the path for blog posts into blog/[node-title]. With that formula, this blog post would be blog/menu-tricks-3-automating-page-urls-based-on-menu-position. Pretty straightforward.
However, this method doesn't work great for pages that are used at different locations in your sitemap. Creating a good Pathauto setting for the Basic Page content type requires more finesse.
Pathauto and Menu Parent Token
In the example above, I used a token (a site variable) called [node-title] to build the pattern for the URL. There are a huge number of tokens available in your URL options, but there's one in particular that is required for making a URL based on menu location:
The above token displays the path of the node that is the parent of the current node. If you have a page in your menu called About and another page Contact beneath About, About is the parent of Contact.
To build a pathauto setting that will make the URL for our page Contact into about/contact, we will want to display the parent URL and then the node title. Or:
One thing to remember is that this pattern will only work if the parent page has already been created, so it will be important to build your site pages from the top down, in terms of menu location. And pages will not automatically update when you change a parent path, which means that if you change a parent node's URL, you'll want to: edit the children pages, click "Generate automatic URL alias" and save again.
Pathauto on Stanford Sites
Pathauto is one of the many modules currently available on the Stanford Sites service. The module offers lots of great options for creating URLs on your site, beyond the menu approach above. I highly recommend it!