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.
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:
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:
Enter the following path pattern for the Article content type made up of three tokens (refer to the following screenshot):
[node:field_article_type]
[node:created:custom:Y/m]
[node:title]
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:
article
, blog-post
, or news
.year–month
format.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.
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:
content/
prefix from the Default path pattern field.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.
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
.