Drupal 8 Entity tài liệu hay đáng xem - Drupal's Entity API

Drupal 8 Entity tài liệu hay đáng xem - Drupal's Entity API

Share Everywhere

When learning Drupal development, it won't be long before you encounter the word "entity" and the Entity API. But what are entities in Drupal? How can you use them to build your site? When should you use the Entity API?

This tutorial will explain Drupal's Entity system from a high level. We'll look at:

  • The main problems the Entity system solves
  • Key terms you should know
  • Key concepts we'll explore as we dive into Drupal's Entity API

By the end of this tutorial you should be able to explain the problems that the Entity API solves, and when you should use it in your own code.

What are entities?

Entities are the basic building blocks of Drupal's data model. They make up, in one way or another, all of the visible content a user interacts with on a Drupal-powered site. There are several types of entities included in Drupal core that make up both the configuration and content of a default installation. It's important to understand the basic differences between these two types of entities before we really dig in further.

Configuration entities are objects that allow us to store information (and default values) for configurable settings on our site. Examples of configuration entities in core include image styles, user roles and displays in views. Configuration entities can be exported via core's configuration management system. They can also be used to provide default configuration used during the installation process or when a new module is enabled. Configuration entities support translation but they cannot have user-configured fields attached to them. The data structure of a configuration entity is limited to what is provided by module code.

Content entities are configurable, support translation and revisions, and allow additional fields to be attached for more complex data modeling. Content entities included in core include nodes, taxonomy terms, blocks and users.

Often you will find entity variants that come in pairs. The Block module, for example, provides configuration entities to define custom block types and content entities to provide the actual content of custom blocks.

Key terms in Drupal's Entity API

Now that we have a little better idea of what Drupal means by the word entity, let's look at some other key terms that we'll need to know in order to understand Drupal's Entity API.


Bundles are another generic noun, used to describe containers for a sub-type of a particular entity. For example, nodes are a type of entity. The node entity has a bundle for each content type (ie: article, page, blog post, etc). Taxonomy is another entity type, and each individual vocabulary is its own bundle. Bundles provide an organizational abstraction layer that allows for differences in field definitions and configurations between entity sub-types. For a particular entity type (i.e. node) all bundles (article, page, etc) will have the same base fields (title, author) but will have different bundle fields (articles have tags).


Fields consist of individual (or compound) data elements that make up the details of the data model. If you're trying to build a photo gallery, your node type will need some method of collecting images. An image field would be handy in this case. Drupal core provides several different types of fields including boolean, decimal, float, integer, entity reference, link, image, email, telephone, and several text fields. Fields, in turn, are built on top of the actual data primitives in Drupal, Typed Data. Fields can be added to content entities and field configuration will vary between bundles of the same entity type.


In short, plugins provide developers an API to encapsulate re-useable behavior. Plugins are used throughout Drupal core and you'll be exposed to several of them while working with the Entity API. We have several tutorials that cover plugins in more depth, starting with What Are Plugins? in our Drupal Module Development Guide.


Annotations are another element of Entity API used throughout Drupal core. Annotations are specially formatted code comments that are parsed to provide metadata information about particular PHP classes to Drupal. The Entity API uses annotations to discover which classes to load for a particular entity type (among other things). You can learn more about how they work in our annotations tutorial.


If you've worked with the Entity API in previous versions of Drupal you're probably already familiar with controllers. Handlers are the Drupal 8+ equivalent of Drupal 7's controllers.

Handlers are responsible for acting on and with entities. They help manage things like storage, access control, building lists and views of entities, and the forms required for creating, viewing, updating and deleting entities.

Differences from Drupal 7's Entity API

If you already have experience with the Entity API in previous versions of Drupal and you're familiar with the Entity contrib module much of this probably seems quite familiar. The Entity API was introduced late in the development cycle for Drupal 7. As such it wasn't completely functional on its own. In practice, most implementations either required this contributed module or were left recreating much of its functionality. Drupal 7's version of the Entity API had to account for differences in accessing and working with entity properties (things like the node title and published status) and fields (things like images, reference fields, etc). This interface is unified in Drupal 8+ because everything is a field. Developers interact with fields using the same techniques regardless of whether they are base fields or bundle fields. Also the method used for querying entity information, the EntityFieldQuery class, had limited functionality in core in Drupal 7. The method for querying entities has been simplified in Drupal 8+ yet is simultaneously more powerful through chaining.

Bạn thấy tài liệu phát triển bản thân này như thế nào?

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Enter the characters shown in the image.

Bài viết liên quan

Sách hay Drupal 7 to 9 - Time to Make the Move

Launched in June 2020, Drupal 9 is the latest version of the open source web content management system.

GUU Chiết khấu thêm 5% trên mức chiết khấu thông thường dành cho agency

GUU The number 1 social platform for young Vietnamese women about fashion, beauty & lifestyle.

Slide tư duy dùng GSA tháng 10 năm 2020

Tối ưu theo chuẩn UX/UI và >1000 yếu tố onpage khắt khe do GPSC qui định, chuẩn người dùng và chuẩn google

SEO Tools tự động hóa, tối ưu chi phí doanh nghiệp tháng 10 năm 2020

Học Phí Tham Gia Khóa Học Là Bao Nhiêu?

Slide Auto Mention Dang Le Nam tháng 10 năm 2020

Bài 1: Tổng quan về khóa đào tạo SEO Growth Hacking