![drupal gear](/sites/default/files/styles/blog_teaser/public/2024-05/drupalci_gear.png?itok=A5-TEiBK)
En Drupal 10, un objeto entidad representa contenido o datos como nodos, usuarios, etc. Es personalizable y extensible. Cada tipo de entidad tiene paquetes, lo que permite configuraciones variadas. La API de Entidades facilita la interacción programática con las entidades, ofreciendo funciones para operaciones de CRUD y consultas.
Consultar tipo de entidad
// Make sure that an object is an entity.
if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
}
// Make sure it's a content entity.
if ($entity instanceof \Drupal\Core\Entity\ContentEntityInterface) {
}
// or:
if ($entity->getEntityType()->getGroup() == 'content') {
}
// Get the entity type or the entity type ID.
$entity->getEntityType();
$entity->getEntityTypeId();
// Make sure it's a node.
if ($entity instanceof \Drupal\node\NodeInterface) {
}
// Using entityType() works better when the needed entity type is dynamic.
$needed_type = 'node';
if ($entity->getEntityTypeId() == $needed_type) {
}
Obtener informacion de una entidad
// Get the ID.
$entity->id();
// Get the bundle.
$entity->bundle();
// Check if the entity is new.
$entity->isNew();
// Get the label of an entity. Replacement for entity_label().
$entity->label();
// Get the URL object for an entity.
$entity->toUrl();
// Get internal path, path alias if exists, for an entity.
$entity->toUrl()->toString();
// Create a duplicate that can be saved as a new entity.
$duplicate = $entity->createDuplicate();