Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Preview:

Citation preview

Good evening!

Thanks for dinner!

My name is Bart

Among other things, I am a Drupal developer.

I have worked on Drupal 7 & 8.

I have developed for Drupal 5, 6, 7, & 8.

I have organized regional, national, and international Drupal events since 2008.

Tonight is your first date with Drupal 8’s entity API.

Tonight’s schedule• What are entities?

• Major changes since Drupal 7

• Entities in Drupal 8

• Content & config entities

• Creating your own entity type

• Q&A

What are entities?

Entities are self-contained units of complex data

Examples of entity types

• Users

• Content (nodes)

• Taxonomy terms

• Payments

• Orders

• Uploaded files

• Payment methods

• User roles

• Custom blocks

• Views

• Menus

• Image styles

What happened since Drupal 7?

Drupal 7 only loaded entities

The Contributed Entity module did everything else.

Drupal 8 supports full CRUD

It supports revsions and translations too

Entities are interfacedThey have class too.

Interfaces define what your class should do.

Not how it’s supposed to do it.

Entity handlersDefine how an entity type interacts with Entity API or

modules.

Entity handlers• Storage

• Access control (no more user_access() for entities)

• Forms

• Views

• Listing

• Viewing

• Translation

Entity queryWorks with any entity storage. If you don’t know the entity

type(’s storage), don’t use DB queries.

Content entities• Fieldable

• Revisionable by default

• Stored in the database by default

• Do not define how a site or application works

• Class properties are now base fields (typed data)

Config entities

• Not fieldable

• Not revisionable by default

• Stored on file (and serialized in the database for performance)

• Define how a site or application works

Creating your own entity type

Why? But, node types?

An entity type with its own interface can be

endlessly refactored.

Choosing a base class

• \Drupal\Core\Entity\ContentEntityBase

• \Drupal\Core\Config\Entity\ConfigEntityBase

Choosing base handlers

• \Drupal\Core\Entity\Sql\SqlContentEntityStorage

• \Drupal\Core\Config\Entity\ConfigEntityStorage

• \Drupal\Core\Entity\EntityAccessControlHandler

• \Drupal\Core\Entity\(Content)EntityForm

• \Drupal\Core\Entity\EntityListBuilder

Writing the annotation/** * Defines a payment entity. * * @ContentEntityType( * field_ui_base_route = “payment.payment_type”, * handlers = { * "access" = "Drupal\payment\Entity\Payment\PaymentAccessControlHandler", * "form" = { * "edit" = "Drupal\payment\Entity\Payment\PaymentEditForm", * }, * }, * id = "payment", * label = @Translation("Payment"), * ) */

Entity storage

• Database schema handler

• Config schema file

Routes can use handlers

payment.payment.edit: path: '/payment/{payment}/edit' defaults: _entity_form: 'payment.edit' requirements: _entity_access: 'payment.update'

Plugins attached to entities

Evaluation

What have you learned?

• The differences between the entity APIs in Drupal 7 and 8.

• The differences between content and config entities.

• How to create a new entity type in Drupal 8 and why this is a good thing.

Q&A

Recommended