Pathauto and Token

Next, we are going to install two modules that work together: Pathauto (drupal.org/project/pathauto) and Token (drupal.org/project/token).

Download, install, and enable both of these modules as earlier. You'll see that Pathauto has a dependency on Token, meaning you cannot use Pathauto until the Token module has also been installed.

Earlier in the module, you'll remember that we specified the URLs of pages as node/4, and so on. This is not particularly user friendly, and we saw that the URL of a page can be added manually.

The Pathauto module allows you to create patterns for new content URLs so that a standard pattern is created for the content. This is beneficial to your users and also to search engines.

Install both the Pathauto and the Token module now

Once you have installed both the modules, navigate to:

Configuration | Search and Metadata | URL aliases (admin/config/search/path).

You've been here before, but now you'll see additional tabs on this screen, in particular, a Patterns tab.

Pathauto and Token

Click on Patterns and scroll down to the CONTENT PATHS section.

You will see the list of existing content types with any associated patterns. The default pattern is preset:

Pathauto and Token

Immediately below the patterns, you can see a list of possible replacement patterns; this is where the Token module comes in.

There is a huge array of available tokens you can use:

Pathauto and Token

Enter the following path pattern for the Article content type made up of three tokens (refer to the following screenshot):

  1. [node:field_article_type]
  2. [node:created:custom:Y/m]
  3. [node:title]
    Pathauto and Token

The idea here is to create a URL in the same shape as those that we specified back in the section entitled Introducing your site-building scenario in the early part of Chapter 5, Basic Content.

What we have now done is created patterns so that each article created will have a URL that includes the creation date and the title of the article in a URL-friendly format:

  1. The first token is replaced with the category of the article: article, blog-post, or news.
  2. The second token is replaced with the date of creation of that article in a simple year–month format.
  3. The third token is replaced by the article title.

All the replacement tokens are also cleaned up, in the sense that they are all converted to lowercase, have spaces, and have some predefined words (in, is, that, the, this, and with) removed.

Try creating a new blog article now entitled another-blog-article.

When you view the new article, you should see what would have been the original un-aliased URL (in our case, node/29) is now replaced by something that is much more human readable.

Pathauto and Token

Note that you can still visit node/29, since we have only created an alias of it, not replaced it.

Now, go ahead and supply similar patterns for all the content types.

Note the following:

  • We removed the content/ prefix from the Default path pattern field.
  • We have left the Pattern for all Basic page paths field blank, so it will inherit the new default:
    Pathauto and Token

Generating paths from patterns

Creating and editing URL patterns has no retrospective effect on the existing content. However, click on the Generate tab, and you'll see there's a way to regenerate all content paths based on the patterns you created.

Tip

Note that this will only work on content with no path already set. If you want to overwrite an old pattern, you will have to use the Delete aliases tab first.

Generating paths from patterns

You should now see that all your node paths as seen in your browser address bar are much more friendly, for example, http://drupal-8.dd:8083/services/drupal-training-courses.

Pathauto settings

Finally, the settings tab allows you to fine-tune how the URL patterns are generated. We won't go into depth here as there are many options; hopefully, you will find them self-explanatory. From here, you can decide how to handle spaces and non-alphanumeric characters in your paths.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset