What's New In Drupal 8: Entity Field API

Video loading...

  • 0:01
    What's New in Drupal 8 Entity Field API
  • 0:02
    with Joe Shindelar
  • 0:03
    Welcome to the Entity Field API addition of our series
  • 0:06
    on What's New in Drupal 8.
  • 0:08
    In this presentation, we're going to get a brief history of the Entity
  • 0:11
    API and its use in Drupal 7, so that we can understand where the pain
  • 0:15
    points are and how Drupal has improved upon both
  • 0:18
    the Entity API and the Field API.
  • 0:21
    We'll cover some of these improvements,
  • 0:23
    and then we'll talk a little bit about how
  • 0:25
    this impacts the types of things that we can build in Drupal 8.
  • 0:28
    Finally, we'll wrap things up with some thoughts
  • 0:30
    about prerequisite knowledge required for using and fully
  • 0:34
    understanding the new APIs.
  • 0:36
    The entity system was introduced pretty late
  • 0:39
    in the Drupal 7 development cycle.
  • 0:41
    And it really only standardized how entities were loaded.
  • 0:44
    However, it laid the groundwork for an idea that has
  • 0:47
    continued to evolve in Drupal 8.
  • 0:49
    Let's look back at some of Drupal 7's entity API pain points,
  • 0:54
    so we can then talk about how they've been fixed.
  • 0:56
    Fields versus properties.
  • 0:58
    In Drupal 7, properties represent data
  • 1:00
    that is required for any entity of a given type,
  • 1:04
    like the ID and title of a node.
  • 1:06
    Fields, on the other hand, are user configurable
  • 1:09
    and could be added to an entity type on a per-use case basis.
  • 1:13
    The problem is that the system handles each differently.
  • 1:16
    And so the options available for configuring the body field
  • 1:19
    are different than those that are available for the title property,
  • 1:22
    and this causes a lot of confusion.
  • 1:24
    The recommended way to get a list of entities
  • 1:26
    is to use the EntityFieldQuery class rather than querying
  • 1:29
    the database directly.
  • 1:31
    The problem is, EntityFieldQuery is missing
  • 1:34
    a lot of advanced functionality, like, for example,
  • 1:36
    the ability to perform joins.
  • 1:38
    And it also suffers from trying to distinguish
  • 1:40
    between properties and fields.
  • 1:43
    The contributed Entity module tries to address
  • 1:45
    many of these shortcomings.
  • 1:47
    But without being able to rewrite the underlying architecture,
  • 1:50
    it's a bit limited in what it can do.
  • 1:52
    It has, however, served as a really great opportunity
  • 1:55
    to do some research and development and informed decisions regarding
  • 1:58
    the new Entity API in Drupal 8.
  • 2:01
    Enter Drupal 8 and the new Entity Field API.
  • 2:05
    Built on the groundwork laid by the Drupal 7 entity and field APIs
  • 2:10
    and the work done in the contributed Entity module,
  • 2:13
    Drupal 8 introduces a new unified Entity Field
  • 2:16
    API with all kinds of awesome.
  • 2:19
    First of all, the API has full support for creating, reading,
  • 2:22
    updating, and deleting entities of any type via a consistent workflow.
  • 2:27
    In fact, that API has gotten so good that in Drupal 8
  • 2:30
    virtually everything has been converted to an entity.
  • 2:33
    All content is stored as entities and not
  • 2:36
    just user-generated content.
  • 2:38
    Complex configuration data is stored as entities as well.
  • 2:41
    The recommended way to store your module's data in Drupal 8
  • 2:44
    is with the entity API.
  • 2:47
    So what do you get if you do that?
  • 2:49
    Well, one of my favorite things about the new entity API
  • 2:53
    is that all entities are now typed objects.
  • 2:56
    That means that when you're dealing with a $node variable
  • 2:59
    somewhere in your code, you're no longer using
  • 3:01
    a standard class object, which is really
  • 3:03
    just a glorified array anyways.
  • 3:05
    You're now working with a node object
  • 3:08
    that conforms to a known interface.
  • 3:10
    So as a developer, I now have a much better idea
  • 3:12
    of how I can use the object and the data it contains.
  • 3:16
    Because all entities implement the standard EntityInterface,
  • 3:20
    I can even do some pretty advanced operations
  • 3:22
    even if I don't know what kind of entity I'm dealing with.
  • 3:25
    This makes things like a universal EntityManager possible.
  • 3:29
    And there is no longer this properties versus fields issue,
  • 3:33
    as everything is a field now.
  • 3:34
    The ID, the title, the body, they're all handled the same way.
  • 3:39
    In order to allow the system to distinguish between fields that are
  • 3:41
    required for all entities of a given type, like the node title,
  • 3:45
    and those that are user-configurable,
  • 3:47
    like the node body, the new Entity Field API introduces the concept
  • 3:52
    of base fields and bundle fields.
  • 3:55
    But these new field objects all implement the same interface.
  • 3:59
    And thus, you've got a consistent API for dealing with field values,
  • 4:02
    regardless of the field type.
  • 4:04
    This also means widgets and formatters
  • 4:07
    work equally well for each piece of data an entity stores.
  • 4:11
    And to make it even more awesome, the API for accessing field values
  • 4:15
    is chainable.
  • 4:16
    So I can easily drill down from the node entity
  • 4:19
    through the author field to the user entity and get the author's name.
  • 4:23
    It's really cool.
  • 4:24
    This might be easier to visualize and get excited about
  • 4:26
    if you can see some code.
  • 4:27
    So here's a couple of examples comparing how you handle fields
  • 4:30
    and their values in Drupal 7 contrasted
  • 4:33
    with how you'll do it in Drupal 8.
  • 4:35
    Notice how there's no more complex arrays
  • 4:37
    with hard-to-decipher keys, just a predictable API
  • 4:40
    for accessing field values.
  • 4:42
    Since fields are all objects themselves,
  • 4:45
    you can also call methods, like getFieldDefinition,
  • 4:47
    for any field to get all the configuration
  • 4:50
    and metadata for that field.
  • 4:52
    And you can even easily set and save a field value
  • 4:55
    without having to call entity type-specific functions.
  • 4:59
    The new Entity Field API introduces the concept of handlers.
  • 5:03
    If you think of an entity as a unit of data,
  • 5:05
    then handlers are the things responsible for acting
  • 5:07
    on that data, similar to controllers from the contributed Entity
  • 5:11
    module for Drupal 7.
  • 5:13
    Handlers are declared per entity type.
  • 5:16
    So you can either use the default handler provided by core,
  • 5:19
    or you can extend it and customize it as needed
  • 5:21
    for your specific use case.
  • 5:23
    Handlers are used for things like storage, access control,
  • 5:27
    and rendering an entity into HTML, amongst other things.
  • 5:31
    There's a new entityQuery class, which
  • 5:33
    is the next generation of entityFieldQuery from Drupal 7.
  • 5:37
    Since everything is a field now, you no longer
  • 5:39
    need to worry about the difference between a property and a field
  • 5:42
    condition when querying.
  • 5:44
    And it's super smart about relationships and more complex
  • 5:47
    query options.
  • 5:48
    It's every bit as powerful as the database querying system.
  • 5:52
    With storage handlers for entities, you can and should
  • 5:55
    just extend the default storage handler, which uses entityQuery,
  • 5:59
    and add custom methods to it.
  • 6:01
    This helps to encapsulate your customizations
  • 6:04
    and makes discovery and maintenance a lot easier down the road.
  • 6:07
    Translation and localization is supported natively
  • 6:09
    by the new Entity Field API.
  • 6:11
    And again, because everything is a field instead
  • 6:13
    of this whole property versus field hassle,
  • 6:16
    the system is much more consistent.
  • 6:18
    Because entities are typed objects, there are also
  • 6:21
    methods for easily accessing translated
  • 6:23
    values for an entire entity or even for an individual field.
  • 6:28
    Underneath the Entity Field API is the new Typed Data API.
  • 6:33
    This introduces the ability to say that the value contained
  • 6:36
    in this field is of a specific type-- something PHP
  • 6:39
    doesn't do very well on its own.
  • 6:41
    Being able to perform introspection on a bit of data
  • 6:44
    that you've just been given and infer that it is a string or even
  • 6:47
    a more complex type, like a link, means you can create a lot of very
  • 6:51
    generic tools that are smart enough to work with any field
  • 6:54
    data they're provided, making things like creating a REST web service
  • 6:58
    and serializing objects or a token replacement
  • 7:01
    system that just works without extra handholding much easier.
  • 7:05
    This will also make it easier for you to integrate your custom entity
  • 7:08
    types with the existing tools, like Drupal
  • 7:11
    8's REST web service or Views, without having
  • 7:14
    to do additional work.
  • 7:16
    There's a lot to get excited about with the new Entity Field API.
  • 7:19
    And we've really only just scratched the surface here.
  • 7:22
    But we'll be digging into all of the details in future tutorials.
  • 7:25
    In order to prepare yourself to use the new system,
  • 7:27
    you want to make sure that you understand the basics
  • 7:29
    of object-oriented programming, like how to use methods on an object
  • 7:33
    and how both inheritance and interfaces work.
  • 7:36
    In order to create new entity types, you'll
  • 7:38
    need to know how to implement Drupal 8's plugin API.
  • 7:41
    So at least having a grasp of how that works will be good.
  • 7:45
    Related, you'll want to understand how annotations work,
  • 7:48
    since they're used to do things like,
  • 7:49
    say which storage or access handler your new entity type should use.
  • 7:55
    In this presentation, we got our first look at the new unified
  • 7:58
    Entity Field API in Drupal 8.
  • 8:00
    We talked a little bit about how almost all data in Drupal 8
  • 8:03
    is represented as an entity and how this
  • 8:06
    makes for a more consistent, more predictable
  • 8:09
    system for dealing with data.
  • 8:11
    Finally, we learned about how, by improving the way the entity system
  • 8:15
    works and standardizing things as much as possible,
  • 8:18
    we're able to create more advanced tools,
  • 8:20
    like the Drupal 8 Views module, which can just
  • 8:23
    work with any new entity type without us
  • 8:25
    having to also provide a ton of additional metadata
  • 8:28
    about our specific case.
  • 8:30
    There are a handful of these new tools already,
  • 8:32
    and I'm really excited to see what kind of additional and more generic
  • 8:35
    and more powerful tools our community is able to come up
  • 8:38
    with once we dig into this new API.
Loading ...

What's New in Drupal 8: Entity Field API

Loading...

This video was part of a series of presentations produced in anticipation of Drupal 8's official release. To learn about Drupal 8's new Entity API, take a look at our Entity API series.

In this presentation we're going to take a high-level look at the new Drupal 8 Entity Field API. This is an improved version of the Entity API and Field API that was started in Drupal 7. Entities are the new recommended way of dealing with data in Drupal and as such, familiarizing yourself with the workings of the Entity API will be important when it comes time to store or retrieve data from your custom modules. It's a big change from what we're used to in Drupal—directly accessing the database via the database abstraction layer—but the Entity Field API also has some distinct advantages.

This presentation covers:

  • Improvements to the Entity API
  • Improvements to the Field API
  • Handlers, Controllers, and ways to manipulate entities
  • Discussion of new types of things we can build with these changes
  • What you can do to start learning now

After watching this presentation you should be able to articulate the improvements made to the Entity Field API in Drupal 8 and start to understand how you might make use of it in your own code. We'll also cover some of the things you can start doing now in order to prepare yourself to use the Entity Field API in Drupal 8.

Downloads: 
Log in or sign up to download companion files.
Additional resources: