Learning Drupal 8
上QQ阅读APP看书,第一时间看更新

Entities, nodes, and fields

When working with Drupal, one of the most important concepts to understand is that you are not just building pages, you are building page containers that consist of components. These components are made up of things called entities.

Entities

Everything you create in Drupal is referred to as an entity.

Nodes

Most viewable content you create will be of a particular type of entity known as a node entity.

Node types

You may often hear the terms "node entity", "node type", and "content type" interchanged routinely, so it's a good idea to think of these terms as synonymous.

There can be many different types of node entities in any given site, but in the standard installation, there are only two content types (node types) defined and they are: Basic page and Article.

Fields

All node entities contain a Title property and one or more fields, an example of which is the Body field. When creating an Article node, we see the following properties, fields, and their types exposed to us ready to populate:

Adding new fields

One of the most useful things about Drupal is the ease with which you can add new fields to existing content types. You might, for example, wish to add a date to an article if that article relates to an event. This can be done in literally moments as you will see shortly.

Field types

In the standard profile we installed earlier, the available field types are as follows:

General

Number

Reference

Text

As we saw earlier, the Article content type that is defined by the standard install profile without any additional configuration contains the following fields:

  • Body: Text (formatted, long, with summary)
  • Comment: (Comments)
  • Image: (Image)
  • Tags: (Taxonomy term)

You can customize any content type, delete any of the existing fields, or add new ones to suit your design. For example, if we want to create a field that allows us to add PDF files as attachments to Articles, we would simply add a new Reference | File type field.

This has been no more than a brief glance at some isolated screenshots for now, because the topic of field editing will be discussed in detail in Chapter 7, Advanced Content.

Field settings

Fields can be set to have a single value, a fixed maximum number of values, or an unlimited number of values. For example, we can set the Article content type to allow a single PDF file to be uploaded, or we can set up the field to accept any number of file attachments.

Field widgets

Widgets provide configuration options for how the field is presented to the user when creating and editing content. For example, a Text (long) field may be presented with a WYSIWYG (What You See Is What You Get) editor window or as a simple plain text field. If, for example, we were using a Date-type field, then we might prefer to present the content editing team with a drop-down menu for each element: Day, Month, and Year in a standard format such as yyyy/mm/dd in place of the default simple text entry of a date.

Form display

Whenever you are editing an item of content through the Drupal user interface (UI), we refer to the UI as the "Edit form". Without any doubt, you will want to arrange the edit form with the fields in the best possible order to help your content editors, so Drupal allows a high degree of control over this for, via the following:

Managing the form display

Strictly speaking, the Title is not a field, it is a property of the node entity but along with all the other fields it is still configurable here:

Managing field display

Similarly, all the fields rendered in the final content view have display-related settings too. In the example below we are looking at the settings for the Image field:

Drupal 8 core entity types

We established earlier that all of the actual items of "content" on a Drupal site (Articles, pages, and so on) are collectively referred to as nodes. Put another way, they are Node entities.

Others types of entity in the standard Drupal installation are: Comment, Custom block, Taxonomy term, and User.

Each of these other four entity types has its own set of specialized properties and fields. For example, the User entity, unlike the node entity, does not have the "published" property, but it does have a property relating to user registration settings. Similarly, it also has a specialized field entitled Picture for holding the user's image.

You can extend existing entities by adding more fields any time you like.

You can also define completely new entity types. For example, the Drupal commerce solution provided by the Commerce suite of modules defines, among others, entities to represent Products and Orders. These custom commerce entity types have properties and fields, which are custom-built precisely for selling online; Product entities, for example, have a SKU property and an intelligent Price field.

This approach allows you to create a structured data model to suit the needs of your particular website.