Entities

From Core to Custom

Shawn Duncan, Technical Architect

Digital Pulp

The Idea of a Data Container

The Idea of a Data Container

  • An object that stores information.
    • Content
    • Configuration

The Idea of a Data Container

  • No actual Entity Class in D7 core.
  • PHP Standard Objects
    • Node
    • User
    • Taxonomy
    • Comments

The Idea of a Data Container

  • In Drupal 8, Entities are first class objects
  • Content
    \Drupal\Core\Entity\ContentEntityBase
    abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface
  • Configuration
    \Drupal\Core\Config\Entity\ConfigEntityBase
    abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
  • UML

Working with Core Entities

Working with Core Entities

  • If you've been working with Drupal for multiple versions, now is the time to stop thinking in nodes.
  • Use fields to model your real processes and relationships.

Working with Core Entities

  • QueryFactory: entity.query
  • You may see blog post refering to the EntityManager Service
  • Accessing fields with methods.
  • Setting field values. Check ::generateSampleValue on the field class to get structure.

Smarter Modules with Custom Entities

The Secret Sauce

  • Drupal Console
  • drupal generate:module
  • drupal generate:entity:content
  • drupal generate:entity:config

Classes, Files & Annotations
for Custom Content Entities

  • An entity interface in the /src directory.
    • Conform your entity to multiple types by extending core interfaces.
      • ContentEntityInterface
        For obvious reasons...
      • EntityChangedInterface
        • Defines an interface for entity change timestamp tracking.
        • Allows more precise cache invalidation.
        • Disallows concurrent editing
      • EntityOwnerInterface
        Allow reassigning entity ownership.

Classes, Files & Annotations
for Custom Content Entities

  • An entity class at /src/Entity/
    • extend ContentEntityBase
    • implement the custom interface just discussed.
    • @ContentEntityType Annotations
      • Refer to \Drupal\Core\Entity\EntityType for all possible properties
      • id: The unique identifier of this entityType.
      • label: Human readable name of the entity type
      • handlers: You can use handlers provided by parent or override with your own. Implement classes in /src for any overrides.
      • base_table: Define a unique name for the table used to store the data.

Classes, Files & Annotations
for Custom Content Entities

  • An entity class at /src/Entity/
    • @ContentEntityType Annotations continued...
      • fieldable: Can additional fields be added to the entity via the admin UI?
      • entity_keys: Map entity columns to standard keys.
      • links: Provide routes to do standard tasks.
      • admin_permission: The permission string. Must define in a permissions.yml file.
    • implement baseFieldDefinitions
      Base fields are fields that exist for every bundle. See: Drupal\Core\Entity\FieldableEntityInterface. Note that entities can have base fields without being designated fieldable.

Classes, Files & Annotations
for Custom Content Entities

  • Customize your entity list by extending EntityListBuilder. Add a list_builder annotation.
  • Handlers you may wish to implement:
    • views_data
    • forms for add/edit and delete
    • access (Pair with a permissions.yml file.)
    • route_provider
  • A routing.yml file is not needed if you implement a route provider. https://www.drupal.org/node/2593577
  • Yaml files for local tasks, local actions and menu defaults. https://www.drupal.org/node/2302893

Classes, Files & Annotations
for Custom Configuration Entities

  • Very Similar
    • Extending ConfigEntityInterface & ConfigEntityBase
    • Corresponding extensions of ConfigEntity classes for forms and list
    • Form controllers
    • Route provider
  • Need a config/schema/module_name.schema.yml file. This defines the schema for yaml files used to export the entity.
  • Each property on the defining class should have a mapping in the schema.yml

More Resources

More Resources

Open Q & A

“Bring him here.”

“Question him we will.”