Entity In Drupal 7/8

  • Reading time:3 min(s) read
  • Post author:
  • Post category:Drupal

Drupal 7

This topic (Entity) is already documented in Drupal.org, but here I like to simplify it for future use or discussion. Generally we always need a single line definition that can define the whole scenario.

An entity is an instance of a particular entity type (such as a comment, taxonomy term or user profile) or of a bundle (such as a blog post, article or product).

In terms of Object-Oriented Design/Programming –

An entity is an object or instance of a base or extended class.

Where 

– An entity type is a base class. 

– A bundle is an extended class

– A field is a class member, property, variable or field instance.

So what are Entity types?

A group of fields are called entity types, example Node(content), Comments, Taxonomy and Users are the entity types from Drupal core.

A Field is a reusable piece of content. And it can be of any data type. And the important thing is Fields can be added to any of the bundles (or entity types) to help organize their data.

So what are Bundles?

Bundles are collection of Entity Types and Fields. Basic Pages and Articles are the best examples of bundles.

While doing code you can load any entity of your application using function entity_load(), but if you are looking to do more like CURD operation, than there is a contrib module called Entity API.

Drupal 8

Continuing the above, Drupal 8 added changes and stated, there are two types of “Entity types”.

Configuration Entity

Used by the Configuration System. Supports translations and can provide custom defaults for installations. Configuration entities are stored within the common config database table as rows.

Content Entity

Consist of configurable and base fields, and can have revisions and support translations. Content entities are stored within a custom database table as rows. The table name is the same as the content entity “id”, and the columns are defined by the entity’s “baseFieldDefinitions” method.

Bundles in Drupal 8

Bundles are combination of both Content Entity and Configuration entity. Content Entity that will manage the content’s details and operations, and a Configuration Entity that will handle the differences between the content entity types.

Annotations have strong presence in Drupal 8, while doing custom coding you may create your own Block, Modules or Bundles, you need to define ID, name, description etc. so here annotations block is the only place you have use. This should be added above the class on your page.

Drupal 8 uses the Doctrine annotation parser, which turns it into an object that PHP can use.

Example:

Handlers

Handlers are defined in the entity annotation as an array. They support the entity by mapping certain parts of its execution to other PHP classes. Those classes will “handle” the assigned parts of the entity’s execution.

Example:

Links

Links are defined in the entity annotation with the array syntax. Links have a specific set of keys whose value are URIs where the entity type or single entities of that type can be managed. Both Content and Configuration Entities can have these links defined.

Creating these links does not automatically create the routes for those URIs. To make these links accessible, your module will need to implement its own routing.yml file, or use a “route_provider” handler in the entity annotation.