Module Description
Meta Entity Description
Meta Entity module allows to add metadata about an entity, stored in a dedicated entity (meta_entity). This is useful when you want to avoid storing this information as a content entity field and clutter the content entity with data that is not part of the content but is metadata (data about other data).
Requirements
* Dynamic Entity Reference module.
How to use it?
Even though the module provides a user interface, it's mainly intended to be used programmatically. Meta data is represented by entities, of type meta_entity that are linking content entities. Meta entity types can be defined as bundle configuration entities (of type meta_entity_type) in order to define types of metadata (e.g. visits count, download count, etc). Each meta_type entity is linked to a content entity (such as node, taxonomy term, etc) via a Dynamic Entity Reference field.
A content entity can be referred by a single meta entity of that type. When defining a meta entity type, the site builder can choose the bundles of content entities that can be linked by that specific type of metadata.
Reverse references
On each bundle selected for a given meta entity bundle, the site builder may configure a field name. The target entity will automatically expose an entity_reference computed field with that name, linking back to the meta entity.
API usage
Create a meta entity type to be used on 'article' nodes:
MetaEntityType::create([ 'id' => 'visits', 'label' => 'Visits', 'mapping' => [ // Node entity type ID. 'node' => [ // The array is keyed by bundle ID. The values are configuration arrays // containing: // - field_name: (optional) A field identifier string may be configured // as value. In this example the 'article' nodes will automatically // expose a field named `visits` which is a reference to the node's // meta entity. // - auto_create: (optional) A boolean value indicating whether to // automatically create a 'visits' meta entity when an 'article' node // is created. 'article' => [ 'field_name' => 'visits', 'auto_create' => TRUE, ], // If the configuration array is empty, no computed field is created and // the no meta entity is created when a 'page' node is created but it's // still allowed to create meta entities for 'page' nodes. 'page' => [], ], // Entity types without bundles, such as 'user', should use the entity type // ID as bundle ID. 'user' => [ 'user' => [ ... ], ], ], ])->save(); Add all needed fields to this meta entity type, either as config entities or using the UI. For instance, in the above example you could add a configurable field field_count of type integer.
Create an 'article' node.
$node = Node::create([ 'type' => 'article', 'title' => 'News From The World', ... ]); $node->save(); Create and link the meta entity:
$visits = MetaEntity::create([ 'type' => 'visits', 'target' => $node, 'field_count' => 10, ]); $visits->save(); Now the node $node has metadata, represented by a meta entity of type visits, associated without the node to be touched. The meta entity can update (e.g. increment the field_count) without needing to re-save the $node. On meta entity update, also the $node cache tags are invalidated.
The content entity can refer directly the meta entity, via the computed reverse reference:
$message = t('@count visits', [ '@count' => $node->visits->entity->field_count->value; ]); For this reason, creating an entity with its meta entity could be achieved in a single step:
Node::create([ 'type' => 'article', 'title' => 'News From The World', 'visits' => MetaEntity::create([ 'type' => 'visits', 'field_count' => 10, ]), ])->save(); Supposing a new meta entity type downloads is added and a new metadata entity of this type is linked to $node:
MetaEntityType::create([ 'id' => 'downloads', 'label' => 'Downloads', 'mapping' => [ 'node' => [ 'article' => [ 'field_name' => 'download_count', ], ], ], ])->save(); $downloads = MetaEntity::create([ 'type' => 'downloads', 'target' => $node, 'field_count' => 100, ]); $downloads->save(); Now there are two reverse reference fields exposed on the node:
// Will return $downloads. $node->download_count->entity; // Will return $visits. $node->visits->entity;
Meta Entity module allows to add metadata about an entity, stored in a dedicated entity (meta_entity). This is useful when you want to avoid storing this information as a content entity field and clutter the content entity with data that is not part of the content but is metadata (data about other data).
Requirements
* Dynamic Entity Reference module.
How to use it?
Even though the module provides a user interface, it's mainly intended to be used programmatically. Meta data is represented by entities, of type meta_entity that are linking content entities. Meta entity types can be defined as bundle configuration entities (of type meta_entity_type) in order to define types of metadata (e.g. visits count, download count, etc). Each meta_type entity is linked to a content entity (such as node, taxonomy term, etc) via a Dynamic Entity Reference field.
A content entity can be referred by a single meta entity of that type. When defining a meta entity type, the site builder can choose the bundles of content entities that can be linked by that specific type of metadata.
Reverse references
On each bundle selected for a given meta entity bundle, the site builder may configure a field name. The target entity will automatically expose an entity_reference computed field with that name, linking back to the meta entity.
API usage
Create a meta entity type to be used on 'article' nodes:
MetaEntityType::create([ 'id' => 'visits', 'label' => 'Visits', 'mapping' => [ // Node entity type ID. 'node' => [ // The array is keyed by bundle ID. The values are configuration arrays // containing: // - field_name: (optional) A field identifier string may be configured // as value. In this example the 'article' nodes will automatically // expose a field named `visits` which is a reference to the node's // meta entity. // - auto_create: (optional) A boolean value indicating whether to // automatically create a 'visits' meta entity when an 'article' node // is created. 'article' => [ 'field_name' => 'visits', 'auto_create' => TRUE, ], // If the configuration array is empty, no computed field is created and // the no meta entity is created when a 'page' node is created but it's // still allowed to create meta entities for 'page' nodes. 'page' => [], ], // Entity types without bundles, such as 'user', should use the entity type // ID as bundle ID. 'user' => [ 'user' => [ ... ], ], ], ])->save(); Add all needed fields to this meta entity type, either as config entities or using the UI. For instance, in the above example you could add a configurable field field_count of type integer.
Create an 'article' node.
$node = Node::create([ 'type' => 'article', 'title' => 'News From The World', ... ]); $node->save(); Create and link the meta entity:
$visits = MetaEntity::create([ 'type' => 'visits', 'target' => $node, 'field_count' => 10, ]); $visits->save(); Now the node $node has metadata, represented by a meta entity of type visits, associated without the node to be touched. The meta entity can update (e.g. increment the field_count) without needing to re-save the $node. On meta entity update, also the $node cache tags are invalidated.
The content entity can refer directly the meta entity, via the computed reverse reference:
$message = t('@count visits', [ '@count' => $node->visits->entity->field_count->value; ]); For this reason, creating an entity with its meta entity could be achieved in a single step:
Node::create([ 'type' => 'article', 'title' => 'News From The World', 'visits' => MetaEntity::create([ 'type' => 'visits', 'field_count' => 10, ]), ])->save(); Supposing a new meta entity type downloads is added and a new metadata entity of this type is linked to $node:
MetaEntityType::create([ 'id' => 'downloads', 'label' => 'Downloads', 'mapping' => [ 'node' => [ 'article' => [ 'field_name' => 'download_count', ], ], ], ])->save(); $downloads = MetaEntity::create([ 'type' => 'downloads', 'target' => $node, 'field_count' => 100, ]); $downloads->save(); Now there are two reverse reference fields exposed on the node:
// Will return $downloads. $node->download_count->entity; // Will return $visits. $node->visits->entity;
Module Link
Project Usage
110
Security Covered
Covered By Security Advisory
Version Available
Production
Module Summary
The Meta Entity module aims to solve the issue of cluttering content entities with metadata by allowing the addition of metadata about an entity in a dedicated entity (meta_entity) through dynamic entity references.
Data Name
meta_entity