D8 Entity API (Old) - pierregermain/MyDrupal GitHub Wiki

Drupal 8 Entity API

Las entidades se programan a través de la Entity API, y los campos a través de la Field API. Nuestro objetivo en Drupal 8 será programar más tipos de entidades y menos tipos de contenido.


(this is a stub)

---> Link to cheat sheat <---


Introduction

2 types of methods (available extending Interfaces):

  • Generic: $entity->id()
  • Entity type: $node->getTitle()

2 types of Entity Types:

About Bundles:

  • They are Configuration Entities to enhance a Content Entity.
  • Example: Article and Basic Page are Node Bundles.
  • More info

About Annotations:

  • You will need to use them when creating a Entity Type.
  • At runtime D8 uses the doctrine annotation parser to turn the Entity into an PHP Object.

About Handlers:

  • Defined in the Entity Annotation
  • Common Handlers:
    • Storage Handler "storage" = "Drupal\node\NodeStorage",
    • Form Handler
    "form" = {
    "add" = "Drupal\block\BlockForm",
    "edit" = "Drupal\block\BlockForm",
    "delete" = "Drupal\block\Form\BlockDeleteForm",
    }
    
    • View builder "view_builder" = "Drupal\node\NodeViewBuilder",
    • List builder
    • Route provider
    • Access
    • Views data
    • Storage schema
    • Translation

About Links

  • Defined in the Entity Annotation (Content and Configuration)
  • To make these links accessible:
    • your module will need to implement its own routing.yml file
    • or use a route_provider handler in the entity annotation.

Entity Types

  • They are typed objects defining a class that can be instanciated.

  • Namespace: \Drupal\[module_name]\Entity

  • Directory: src/Entity

  • Annotations to be used: (\Drupal\Core\Entity\Annotation\EntityType)[https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Annotation%21EntityType.php/class/EntityType/8]

  • Naming: Entity type names should be prefixed with the module name if the entity type and module name aren't the same.

  • Interfaces: type hintfunctions and methods with Interfaces instead of classes.

    • Use descriptive method names starting with get/set/is only where needed. For example to access $node->status you will use $node->isPublished()
  • Discoverability: Don't forget the id annotation key.

Have a look at theNode Entity

Have a look at Drupal 8 Entities Diagram

Working with the Entity API

  • Generic methods:

    Entity::create()
    Entity::load()
    Entity::save()
    Entity::id()
    Entity::bundle()
    Entity::isNew()
    Entity::label()
    
  • More methods:

    $entity->toUrl()->toString();
    $entity->access();
    

More info


Your own custom entity and plugin type

TODO: Insert data from the book


Info from the Book "Drupal 8 Module Development"

Getting Data from Entities

We use the entity.query service (QueryFactory) to get our data.

Method 1 using entity.query service

$query = \Drupal::entityQuery('node');

where node is the entity type

Method 2 using the entity_type.manager service

\Drupal::entityTypeManager()->getStorage('node')->getQuery();

Build Queries

// Example 1
$query
 ->condition('type', 'article')
 ->range(0,10)
 ->sort('created', 'DESC');
$ids = $query->execute();

// Example 2
$query
 ->condition('type', ['article', 'page'], 'IN');
$or = $query->orConditionGroup()
 ->condition('title', 'Drupal', 'CONTAINS')
$query->condition($or);
$ids = $query->execute();

More info at the QueryInterface class (especially the condition() method. Important: Queries for the Node entity take access restrictions into account using the context.