Drupal's Entity system
Entities were introduced in Drupal 7 first time. In Drupal, entity is a general concept . So it may be anything like noun(name, person, palace or a thing).
In Drupal each entity has a purpose to represent/show a specific type of data.
Like the user entity, it displays user information. Content entities display content-related data.
entities can be customized by adding fields to them called bundle.
bundles is a subtypes of an entity type.
Out of the box, there are a number of different types of entities in Drupal,
entities types
- Content entities
- Configuration entities
Content entities
Primarly content entity is used to store contentThe data is in text format (html markup, images, and files). In content entities we have field support. Data is stored in an individual field that holds one type of data.
Example : Node, Taxonomy, Blocks, Comment, User, files etc.
Configuration entities
Primarly configuration entity is used to store configurationConfiguration entities use the Entity API to store sets of configuration in the database. Data can be managed either in a schema table or YAML files.
Example: Views, image style, content type , Menu, Role etc.
Core drupal Entities
- Nodes (content entity)
- Media(content entity)
- Comments(content entity)
- Users(content entity)
- Taxonomy(terms and vocabularies)(content entity)
- block(content entity)
- Files(content entity)
Entity in technical term
- An entity type is a base class
- A bundle is an extended class
- A field is a class member, property, variable or field instance
- An entity is an object or instance of a base or extended class.
For example, term Media Types are the equivalent to media entities of what Content Types are for nodes.
Why need custom entity in Drupal?
Benefit: We can export config from one env to another env.
Create custom Entity in Drupal
So let’s create a module for custom entity type.
Module Setup :
- create a [module_name].info.yml.
- create a [module_name].routing.yml
- create a [module_name].links.menu.yml
- create a [module_name].links.action.yml
- create a [module_name].links.task.yml
- create a Entity type classes inside src/[entity_type_name].php.
- create entity class inside src/Entity/[entity_name].php.
- create Controllers inside src/Entity/Controller/[controller_name].php
- create a form inside src/Form/[form_name].php
- Entity type creation is done.