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.
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.
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
|Field type||Machine name|
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.
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.”.
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.
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.
2 thoughts on “How to Add Machine Names to Content Types in Drupal 7”
Great article, I think it will be okay if you site a real life use case of machine name.
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.