35
Good evening!

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

Embed Size (px)

Citation preview

Page 1: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Good evening!

Page 2: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Thanks for dinner!

Page 3: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

My name is Bart

Page 4: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Among other things, I am a Drupal developer.

Page 5: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

I have worked on Drupal 7 & 8.

Page 6: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

Page 7: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

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

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

Page 9: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

Page 10: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

What are entities?

Page 11: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Entities are self-contained units of complex data

Page 12: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Examples of entity types

• Users

• Content (nodes)

• Taxonomy terms

• Payments

• Orders

• Uploaded files

• Payment methods

• User roles

• Custom blocks

• Views

• Menus

• Image styles

Page 13: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

What happened since Drupal 7?

Page 14: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Drupal 7 only loaded entities

The Contributed Entity module did everything else.

Page 15: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Drupal 8 supports full CRUD

Page 16: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

It supports revsions and translations too

Page 17: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Entities are interfacedThey have class too.

Page 18: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Interfaces define what your class should do.

Not how it’s supposed to do it.

Page 19: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Entity handlersDefine how an entity type interacts with Entity API or

modules.

Page 20: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Entity handlers• Storage

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

• Forms

• Views

• Listing

• Viewing

• Translation

Page 21: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

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

Page 22: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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)

Page 23: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

Page 24: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Creating your own entity type

Page 25: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Why? But, node types?

Page 26: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

An entity type with its own interface can be

endlessly refactored.

Page 27: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Choosing a base class

• \Drupal\Core\Entity\ContentEntityBase

• \Drupal\Core\Config\Entity\ConfigEntityBase

Page 28: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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

Page 29: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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"), * ) */

Page 30: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Entity storage

• Database schema handler

• Config schema file

Page 31: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Routes can use handlers

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

Page 32: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Plugins attached to entities

Page 33: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Evaluation

Page 34: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

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.

Page 35: Entity API in Drupal 8 (Drupal Tech Talk October 2014)

Q&A