D8: Entities (content)
Entity...
is high-level multipurpose data object
it's like a model from MVC, but with 💊💊💊
Entity - is a data object of a complex data type that is loadable.
Usually entities grouped by logical puprose
Entity features vary:
-
revisionable
-
translatable
-
fieldable
-
bundleable
Entity storage could be anything (with a "file"), database based by default
example: https://www.drupal.org/project/external_entities
Facts

ContentEntityBase
Examples
Interfaces + traits
(features)
class MySuperContentEntity implements ContentEntityInterface,
EntityChangedInterface,
EntityOwnerInterface,
RevisionLogInterface,
EntityPublishedInterface
{
use EntityChangedTrait;
use EntityPublishedTrait;
use RevisionLogEntityTrait;
}Extending
virtual fields (display fields)
calculated fields
Entity, fields definitions: properties, fields, view modes
Operations
Access
CRUD events, R - view + load
function devel_entity_test_entity_view_mode_info_alter() {}
function path_entity_base_field_info() {}
function field_entity_bundle_field_info() {}
function hook_entity_view_mode_alter() {}
...function comment_entity_insert() {}
function content_moderation_entity_update() {}
function hook_entity_create() {}
function hook_entity_load() {}
function rules_entity_view() {}
function rules_entity_delete() {}
...function commerce_payment_entity_operation() {}
...function content_moderation_entity_access() {}
...Cases
core/modules/path/src/Plugin/Field/FieldType/PathItem.php
Computed fields
/**
* Implements hook_entity_base_field_info().
*/
function path_entity_base_field_info(EntityTypeInterface $entity_type) {
if (in_array($entity_type->id(), ['taxonomy_term', 'node', 'media'], TRUE)) {
$fields['path'] = BaseFieldDefinition::create('path')
->setLabel(t('URL alias'))
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'path',
'weight' => 30,
])
->setDisplayConfigurable('form', TRUE)
->setComputed(TRUE);
return $fields;
}
}
/**
* Defines the 'path' entity field type.
*
* @FieldType(
* id = "path",
* label = @Translation("Path"),
* description = @Translation("An entity field containing a path alias and related data."),
* no_ui = TRUE,
* default_widget = "path",
* list_class = "\Drupal\path\Plugin\Field\FieldType\PathFieldItemList",
* constraints = {"PathAlias" = {}},
* )
*/
class PathItem extends FieldItemBase {}
Custom Entity...
Updates
/**
* Implements hook_install().
*/
function mymodule_install() {
// Different approaches for this update, see https://www.drupal.org/node/2078241.
// Create field storage for the 'completeness' base field.
$entity_manager = \Drupal::entityManager();
$definition = $entity_manager->getFieldStorageDefinitions('profile')['completeness'];
$entity_manager->onFieldStorageDefinitionCreate($definition);
}
/**
* Implements hook_uninstall().
*/
function mymodule_uninstall() {
$entity_manager = \Drupal::entityManager();
$definition = $entity_manager->getLastInstalledFieldStorageDefinitions('profile')['completeness'];
$entity_manager->onFieldStorageDefinitionDelete($definition);
}

Founder @

Co- Founder @
Vladyslav Moyseenko, a.k.a vlad.dancer
&
D8 School: Entities
By Vlad Moyseenko
D8 School: Entities
- 614