Working with Entity API
This cheat sheet provides an overview of commonly used methods, classes, and interfaces for content entities.
/sites/default/files/drupal-content-entity-8.0.pdf
Content entities inherit much of their behavior from entities. See Working with the Entity API for these shared features.
Managing Field Values
Create custom fields using the baseFieldDefinitions method for your content entity.
- Load a custom entity instance into a local variable $Custom_Entity
- Define a custom field named "custom_field"
- Store some data
Code Example
$custom_field_value = $Custom_Entity->custom_field->value; // Perform some kind of data manipulation $Custom_Entity->custom_field->value = $custom_field_value; $Custom_Entity->save();
Example content entities:
- node
- comment
- user
Configuration entities use the Entity API to store configuration in the database.
Differences from Content Entities
- Integrates with the CMI API for export capabilities
- No fields
- Schema file (Content Entities use hook_schema())
Tutorials
- Drupal example: Creating a configuration entity type in Drupal 8
- Change record: https://www.drupal.org/node/1818734
In Drupal 8, bundles are a type of container for information that holds field definitions or settings. They are sometimes referred to as "subtypes." Bundles are optional and sit below entity types in the hierarchy of information containers.
- Entity variants (content and configuration; many entities are paired, e.g., blocks)
- Entity Types
- Bundles or subtypes (optional)
Examples of these container types:
- Entity variants:
1) Content
Content entity types:
1. Node:
- Node bundles, also known as content types:
* Article
* Basic Page
2. Taxonomy
- Taxonomy bundles, also known as vocabularies:
* <Vocabulary A>
* <Vocabulary B>
* <Vocabulary Etc.>
3. Blocks
- Custom Block Bundles, also known as Block Types:
* <Block Type L>
* <Block Type M>
* <Block Type Etc.>
4. User (no child bundles)
5. <Custom content entity type X>
6. <Custom content entity type Y>
7. <Custom content entity type Etc.>
2) Configuration
Configuration entity types:
* Custom Block types (no child bundles)
* View (no child bundles)
* Menu (no child bundles)
* Role (no child bundles)
* <Custom config entity type I>
* <Custom config entity type II>
* <Custom config entity type Etc.>
This cheat sheet provides an overview of commonly used methods, classes, and interfaces for content entities.
/sites/default/files/drupal-content-entity-8.0.pdf
Content entities inherit much of their behavior from entities. See Working with the Entity API for these shared features.
Managing Field Values
Create custom fields using the baseFieldDefinitions method for your content entity.
- Load a custom entity instance into a local variable $Custom_Entity
- Define a custom field named "custom_field"
- Store some data
Code Example
$custom_field_value = $Custom_Entity->custom_field->value; // Perform some kind of data manipulation $Custom_Entity->custom_field->value = $custom_field_value; $Custom_Entity->save();
Example content entities:
- node
- comment
- user
Configuration entities use the Entity API to store configuration in the database.
Differences from Content Entities
- Integrates with the CMI API for export capabilities
- No fields
- Schema file (Content Entities use hook_schema())
Tutorials
- Drupal example: Creating a configuration entity type in Drupal 8
- Change record: https://www.drupal.org/node/1818734
In Drupal 8, bundles are a type of container for information that holds field definitions or settings. They are sometimes referred to as "subtypes." Bundles are optional and sit below entity types in the hierarchy of information containers.
- Entity variants (content and configuration; many entities are paired, e.g., blocks)
- Entity Types
- Bundles or subtypes (optional)
Examples of these container types:
- Entity variants:
1) Content
Content entity types:
1. Node:
- Node bundles, also known as content types:
* Article
* Basic Page
2. Taxonomy
- Taxonomy bundles, also known as vocabularies:
* <Vocabulary A>
* <Vocabulary B>
* <Vocabulary Etc.>
3. Blocks
- Custom Block Bundles, also known as Block Types:
* <Block Type L>
* <Block Type M>
* <Block Type Etc.>
4. User (no child bundles)
5. <Custom content entity type X>
6. <Custom content entity type Y>
7. <Custom content entity type Etc.>
2) Configuration
Configuration entity types:
* Custom Block types (no child bundles)
* View (no child bundles)
* Menu (no child bundles)
* Role (no child bundles)
* <Custom config entity type I>
* <Custom config entity type II>
* <Custom config entity type Etc.>
General Entity API Methods
- Entity::create()
- Entity::load()
- Entity::save()
- Entity::id()
- Entity::bundle()
- Entity::isNew()
- Entity::label()
More specific APIs will be discussed in the respective chapters.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.