Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

The Drupal Token System

Parent Feed: 

Credits & Thanks

Thank you to:

About Tokens and the Token System

To fully take advantage of patterns, you need to understand a little about the Token system.

Tokens are variables in Drupal. There are thousands of Tokens available for you to use. To get a UI in the admin area for browsing all available tokens (and have tokens that aren’t available in core), then you’ll want to have your developer install the Token module.

To see what they are and get a better understanding of how they work, you can go to Manage > Help and click on the Token link.

drupal token listing


Bear in mind that not all of the tokens are available in all areas. For now, we are going to focus on just a handful of critical Tokens that we’ll use to create our URL path patterns. They are:

[node:title] - The title of the piece of content being displayed.
[term:vocabulary] - The vocabulary (top level category, so to speak) of the current taxonomy page.
[term:name] - The name of the current term (bottom level category).

KEY CONCEPT:
 
Whenever Drupal sends a page to a visitor, it first replaces the tokens with the corresponding text. i.e. the “Today” token might be replaced with “February 22, 2021” or “August 26, 2021”.
 
Don’t worry if this doesn’t make sense yet. What you need to know right now is that we’re going to tell Drupal to create some paths for us, and we’re going to use Tokens to make it happen.

Create Pathauto Patterns

Drupal URL paths operate in patterns. Instead of creating a path to every single piece of content, it’s better to specify a pattern (using tokens) for groups of content. Drupal will follow the pattern to create the path for each new piece of content, ensuring consistency across your website.

You’re going to add a Pathauto pattern for each Content Type and taxonomy that you have.

  1. Go to the URL aliases page: Click Manage > Configuration > Search and metadata > URL aliases (Coffee: url aliases) or or visit https:///admin/config/search/path/patterns.
     
  2. Click the Patterns tab.
     drupal path patterns in pathauto
  3. Click the + Add Pathauto pattern button.
     
  4. From the Pattern type drop-down, select Content. The form will update to reflect that content type’s settings.

    Note: Leave existing path patterns as they are unless you have a good reason to change them. Any changes to these settings will not change existing pages, only pages created moving forward and could create issues with your content siloing efforts.

    drupal pathauto pattern type dropdown selection
  5. Fill out the fields as shown.
     
    • Path pattern: “[node:title]”
    • Content type: select “Article” and “Basic page”
    • Label: Anything goes. We use something descriptive such as “Content Types: Article & Basic”
    • Select the Enabled checkbox.

      drupal pathauto pattern field settings

  6. Click the Save button near the bottom of the page. The resulting page will look something like this:

    pathauto page results
     

  7. Repeat the above steps for each of your Content Types.
     

    Note: When you create new content types, you’ll want to go through this process for each of those at that time.

Now, when you create new pages of these content types, the [node:title] will be replaced with a normalized version of the title of the content. Drupal will change the letters to lowercase, replace spaces with dashes, and remove any odd characters.

For example, if you create a page with the title of “Cat Pictures for the Cat Lover in All of Us” would get converted to something like this:

https:///cat-pictures-cat-lover-all-us

Going a step further with Pathauto

If you create your patterns like the example above, you will have a flat website with no hierarchy. If you created three basic pages called “Our Products”, “Our Team”, and “Our Customers” then you’d have three pages that look like this:

https://www.example.com/our-products
https://www.example.com/our-team
https://www.example.com/our-customers

Maybe that’s what you want, but maybe you want something a little deeper. You can edit the patterns you’ve created or delete them and create new ones. For example, let’s say you’ve created a new Content Type for your blog called “Blog Postings” and you want them to be under the /blog directory. You’d create a Pathauto pattern that looks like this:

pathauto patterns using tokens and static content

As you can see, you’d enter blog/[node:title] in the Path pattern field. Your blog URLs might look like this:

https://www.example.com/blog/my-happy-cat
https://www.example.com/blog/my-big-cat
https://www.example.com/blog/why-i-love-cats

Or, if your blog focuses on a single topic (cats, is it?) then you might put this:

cat-blog/[node:title]

Which produces this:

https://www.example.com/cat-blog/happy-cats
https://www.example.com/cat-blog/sad-cats
https://www.example.com/cat-blog/why-cats

Better! Now you’ve used the powerful key phrase “cat blog” which improves your SEO for that keyword. Now, when you create each new piece of content, it will be in the /cat-blog/ section of your website.

Now let’s update your taxonomy terms. Let’s say you create a new Pathauto pattern that looks like this:

pathauto patterns for taxonomies in drupal

You might be wondering about the Path pattern for the Taxonomy term.

[term:vocabulary] is the top level category that the term belongs to. In this case, Tags.
[term:name] is the name of the tag (i.e., the tag itself).

In use, it might look like this:

https://www.example.com/tags/siamese
https://www.example.com/tags/persian
https://www.example.com/tags/abyssinian/

This is not an exhaustive look at tokens, but it should be a good start when conducting SEO for your website.

Did you like this walk through? Please tell your friends about it!

facebook icon twitter social icon linkedin social icon

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web