Introduction to Entities and Fields

In this video, you’ll be introduced to the entity and field system in Drupal. If you have a basic understanding of fields, content types, etc… you can skip this video.


Trainer (00:00):
So we’ve looked at how to create a basic page in Drupal. Now I want to discuss the entity and field system in more detail so you have an understanding of how it works. You don’t have to be a Drupal expert to understand it, but it’s important to have a fundamental understanding of how content is stored in Drupal. So, how is content stored in Drupal? Well, the actual content itself is stored in fields. If we edit this about us page which we created in the last video, you can see that we have two fields, title and body. And you can see the text in the fields, but the entity system groups these fields together, but the content is still stored in the fields.

Trainer (00:51):
To explain this further, let’s go to structure and content types. And here you can see the two content types that come with Drupal when you install it using the standard installation profile. We have article and basic page. Generally you’d have a lot more content types than just these two. The about us page was created using the basic page content type. And if you click on manage fields, you’ll see that there’s only one field and that is the body field. But there’s no title field, and that’s because title is a hard coded field on content types. It can be hidden but not removed. And it doesn’t appear on the manage fields page. A lot of entity types implement their own custom fields and this is not uncommon.

Trainer (01:43):
If we go back and click on manage fields on the article content type, we can see a lot more fields. The article content type has body, comments, image, and tags. And if we go to the article content form, so I’ll click on content, add content, and then click on article, and you can see the fields right here. We have the hard coded title field, body, tags, image. And we also have the comment settings here on the right.

Trainer (02:15):
Let’s now go to structure. So Drupal comes with a lot of entity types out of the box. And if they are fieldable entity types or the values are stored using fields and the entity types are used to contextually group the fields. And the reason for the different entity types is because data is displayed and handled differently, but all content is stored in fields. And the user interface to manage these fields is the same. Adding a field to a content type is the same as adding a field to a custom block.

Trainer (02:53):
Let’s start with content types. Content types are part of the content entity type, and they allow you to create pages with URLs. This is the most common entity type you’ll use because you use it to create actual pages. If you click on manage fields on the basic page row, you’ll see these three tabs, manage fields, manage form display, and manage display.

Trainer (03:21):
Then we have taxonomy terms. They allow you to tag and categorise content and you can create vocabularies which are the same as content types. Or another way to think of it is as a category type. Because this entity type is fieldable, we’ll see the same three tabs, manage fields, manage form display and manage display. And there are a few hard coded fields on this entity type, and that is name, description, and relations.

Trainer (03:57):
Then we have comment types, which is part of the comment entity type. And here you can see that there’s only one comment type called default comments. And if we click on manage fields, you’ll see that there is only a single field called comment. But if we go to an article, so let’s click on content and let’s actually create an article. So I’ll click on add content article and I’ll add in random article as the title and I’ll paste in some random text and then click on save. If we scroll down to the comment section, we can see the comment field right here, but we can also see a subject field. And that field is hard coded.

Trainer (04:44):
And finally, if we go to structure, block layout, and then click on custom block library, you can create a custom block which appears in theme regions. And if you want to see all of the available theme regions, go back to block layout, and then click on demonstrate block regions. And here you can see all of the available regions which a custom block can be added to. And let’s just go back and then back to custom block library, and you can also create custom block types. These block types are fieldable. So you should see the same set of tabs, manage fields, manage form display, and manage display.

Trainer (05:27):
So as you have seen all of the entity types offer different functionality, the way you attach custom fields is identical. The user interface is the same, but they all offer different use cases and functionality. Content types are used for pages. Taxonomy terms are used to create category lists and categorise content. Comment types are used for commenting on articles and custom blocks are used to add content theme regions.

Trainer (05:59):
Now that we’ve discussed entity types, let’s look at fields. And to do that, let’s go back to structure, content types, and click on manage fields on the article row. Fields can be broken up into three parts. You first have the field type which can be managed from this page, the manage fields page. And then you have field widgets, which can be managed from the manage form display. This is the form element that’s used to add content into fields. And the field widgets are what you see when you create and edit articles or basic pages. And then you have field formatters, which can be managed from the manage display page. The field formatter renders the field value to the end user.

Trainer (06:51):
So let’s take the article content type as an example. If we go to content and view this random article, the field values here are rendered using the field formatters, but some formatters do display forms as is the case of the comment form right here. And if we edit the article, so let’s scroll back up and click on the edit tab, these form elements on this page, these are the field widgets.

Trainer (07:22):
Now, another thing I want to show you before we finish up this video is how to reorder field widgets and field formatters. So let me open up the structure page in a new tab and I’ll pop it to the right and I’ll pop this article content form on the left. And if we go to content types and then go to manage form display on the article row, and we can reorder these widgets here on the left by simply reordering the fields on the manage form display page. So let’s go ahead and move body above title and then click on save. And when I refresh the browser on the left, the body field should be above title. There you go.

Trainer (08:10):
And we can do that the same thing to formatters. So let’s view the article and let’s move the comment form above the body field. And to do that, click on manage form display and simply click and drag the comments field above body, and then scroll down and click on save. And then on the left, if we refresh, we should see the add new comment form above body.

Trainer (08:36):
Now, you may have noticed that on the manage display page, we can’t reorder the title field, but if we go to manage form display, we have it right here. Why is that? This title field here on the front end is hard coded at the theme layer and it cannot be reordered like these custom fields. So just be aware of that. So that’s a quick overview of the field system. You will learn how to create and manage them as well as configuring field widgets throughout the rest of this course.

Scroll to Top