How to Add Machine Names to Content Types in Drupal 7

Machine names are all over the place in Drupal 7. When you create a content type or field it will appear next to the label. The concept of a machine name is simple. It’s an alphanumeric value which shouldn’t change once created and it must be unique.

Fig 1.3

A custom machine name can be implemented in two ways: as a field or form element. If you’re creating a form you can implement one using machine_name form element. On content types, you can use the Machine name field.

In this tutorial, you’ll learn how to setup and use the machine name field on an article content type.

Getting Started

Before we begin, go download and install Machine name like any other module.

If you use Drush, run the following:

drush dl machine_name

Create Field on Article Content Type

Now that the module has been installed, let’s add a machine name field to the Article content type.

1. Go to Structure, “Content types” and click on “manage fields” within the Article row.

2. Create a Article name field using the values defined in Table 1-0.

Table 1-0. Create Article name field

Option Value
Label Article name
Machine name field_article_name
Field type Machine name
Widget Machine name

Fig 1.0

Create Article

Now that we’ve created our field, let’s test it out.

Go to Content, “Add content” and click on Article. Enter “Test article” into Title and “test_article” into the Article name field. Then click on Save.

Fig 1.1

A new article should have been created, so far so good.

Now let’s test out the validation, go back to the Create article page (node/add/article), and enter in the same title and article name. You should see a message; “The machine-readable name is already in use. It must be unique.”.

Fig 1.2

The reason why we got this message is because the value “test_article” is already used by another article.

It’s important to understand that the module will only check for unique values on a per field basis. For example, if you created another machine name field called “page_name”, you could use “test_article” within it because it’s a different field.

Summary

Machine name is a simple module that does one thing and it does it well. But if the module is not right for you, then I would recommend you also look at Unique field and Field validation.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of WebWash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 12 years and has successfully completed several large Drupal projects in Australia.

2 thoughts on “How to Add Machine Names to Content Types in Drupal 7”

    1. Hi Adedapo,

      A good example of a machine name is how it’s used for content types. The label of a content type can change, but the machine name can’t. If you write any code around a content type, you’ll use the machine name to determine which content it is. You’re guaranteed that the name won’t change.

      Use a machine name if you need to string to be created ONCE and never changed.

      hope this helps.

      Cheers,
      Ivan

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.