Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra
description
Transcript of Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra
![Page 1: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/1.jpg)
Good evening!
![Page 2: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/2.jpg)
Thanks for dinner!
![Page 3: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/3.jpg)
My name is Bart
![Page 4: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/4.jpg)
Among other things, I am a Drupal developer.
![Page 5: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/5.jpg)
I have worked on Drupal 7 & 8.
![Page 6: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/6.jpg)
I have developed for Drupal 5, 6, 7, & 8.
![Page 7: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/7.jpg)
I have organized regional, national, and international Drupal events since 2008.
![Page 8: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/8.jpg)
Tonight is your first date with Drupal 8’s entity API.
![Page 9: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/10.jpg)
What are entities?
![Page 11: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/11.jpg)
Entities are self-contained units of complex data
![Page 12: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/13.jpg)
What happened since Drupal 7?
![Page 14: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/14.jpg)
Drupal 7 only loaded entities
The Contributed Entity module did everything else.
![Page 15: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/15.jpg)
Drupal 8 supports full CRUD
![Page 16: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/16.jpg)
It supports revsions and translations too
![Page 17: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/17.jpg)
Entities are interfacedThey have class too.
![Page 18: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/19.jpg)
Entity handlersDefine how an entity type interacts with Entity API or
modules.
![Page 20: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/24.jpg)
Creating your own entity type
![Page 25: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/25.jpg)
Why? But, node types?
![Page 26: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/26.jpg)
An entity type with its own interface can be
endlessly refactored.
![Page 27: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/27.jpg)
Choosing a base class
• \Drupal\Core\Entity\ContentEntityBase
• \Drupal\Core\Config\Entity\ConfigEntityBase
![Page 28: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/30.jpg)
Entity storage
• Database schema handler
• Config schema file
![Page 31: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/32.jpg)
Plugins attached to entities
![Page 33: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/33.jpg)
Evaluation
![Page 34: Entities in Drupal 8 - Drupal Tech Talk - Bart Feenstra](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052621/5581b08fd8b42a1d1a8b458a/html5/thumbnails/35.jpg)
Q&A