35
Good evening!

Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Embed Size (px)

DESCRIPTION

What are entities in Drupal 8 and how does that differ from Drupal 7. How can you leverage all the power of entities in Drupal 8 and why you should definitely use them!

Citation preview

Page 1: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Good evening!

Page 2: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Thanks for dinner!

Page 3: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

My name is Bart

Page 4: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Among other things, I am a Drupal developer.

Page 5: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

I have worked on Drupal 7 & 8.

Page 6: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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

Page 7: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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

Page 8: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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

Page 9: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

What are entities?

Page 11: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Entities are self-contained units of complex data

Page 12: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

What happened since Drupal 7?

Page 14: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Drupal 7 only loaded entities

The Contributed Entity module did everything else.

Page 15: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Drupal 8 supports full CRUD

Page 16: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

It supports revsions and translations too

Page 17: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Entities are interfacedThey have class too.

Page 18: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Interfaces define what your class should do.

Not how it’s supposed to do it.

Page 19: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Entity handlersDefine how an entity type interacts with Entity API or

modules.

Page 20: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Entity handlers• Storage

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

• Forms

• Views

• Listing

• Viewing

• Translation

Page 21: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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

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

Page 22: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Creating your own entity type

Page 25: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Why? But, node types?

Page 26: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

An entity type with its own interface can be

endlessly refactored.

Page 27: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Choosing a base class

• \Drupal\Core\Entity\ContentEntityBase

• \Drupal\Core\Config\Entity\ConfigEntityBase

Page 28: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Entity storage

• Database schema handler

• Config schema file

Page 31: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Routes can use handlers

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

Page 32: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Plugins attached to entities

Page 33: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Evaluation

Page 34: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

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: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra

Q&A