Drupal 8 Entities

Making programming fun again

DrupalCampLA 2016 - Ashok Modi

About Presenter

  • Drupal.org username is BTMash
  • Engineer at CARD.com
  • Using Drupal for 10 years
    • Webfont Loader
    • @fontyourface
  • Talk may not live up to the slogan


  • In MVC talk, these are content models
    • Rails
    • Laravel
    • Django
  • Types of Drupal Entities:
    • Node
    • User
    • Taxonomy


  • Drupal has advantages out of the box
    • Entities can be bundled (or, content models can have sub-types)
    • Integrates with many core Drupal-isms
      • Fields
      • Views
      • Etc.


  • Not everything needs to be a Drupal node
    • Not mixing content models in a given view
      • Gain performance
    • Not dealing with 'content' in the traditional sense
      • Fonts via @font-your-face
      • User badges
      • Rewards

Why not use a module?

Because it is easy!

  • You can always use ECK
  • But its a lot of magic
  • Full control of your entity right from beginning
  • Learning is fun

Drupal Console

  • Like Drush but with all the PHP5 goodness
  • Super well maintained
  • Get Jesus M. Olivas a beer for his hard work
    • At the camp!

Why Drupal Console?

  • The best, the very best, the best of all
  • Nice way to make a skeleton entity
    • Useful out of the box
    • Add fields
    • Add views
    • Wham!




We are a news organization. We have millions of articles over the years. We now want to also display local events.


  • Create a new entity
    • Events
  • Its a demo, so my rules!
  • When people search for content, they are not searching for events
    • Unless in event section
  • Make event section faster by using separate set of tables.

drupal generate:module

drupal generate:entity:content

You are now ready!

Use your new entity

  • Create Fields
  • Create Views
    • Override admin interface
  • Create Content!
  • Export it all to your module
    • Copypasta time

Entity without bundles

Entity with Bundles

Where is the programming?

You just ran a command, dummy

That's the point!

Stop writing boilerplate

  • Its over 2000 lines of code
  • Focus on what you want to do
  • Scaffolding will let you then just add your real properties and behaviors

Looking for revisions?



  • Walk through files and what they mean
    • src/Entity/LaDrupalEvent.php
    • src/Entity/LaDrupalEventInterface.php
    • src/Entity/LaDrupalEventViewsData.php
    • src/Form/LaDrupalEventForm.php

Adding new columns to entity

  • Sometimes, you don't need it to be a field with its own set of tables
  • Faster
  • Picked up automatically by views, search indexing
    • Kind of, anyways (simple fields work well)

Adding columns to entity table

  • Open src/Entity/<EntityInterface>.php
  • Open src/Entity/<Entity>.php
  • Add your new getters/setters
  • Add start/end dates

drupal update:entities

Configuration Entities

  • Drupal specific
  • Not fieldable
  • No integration with views
  • Import/Export configuration
    • Image Styles
    • Breakpoints
    • Font Displays
    • Custom Fonts


  • Fast
  • Simple
  • Flexible
  • Great in small number
  • Easy to export/import between sites

drupal generate:entity:config


  • Create config entity
  • config/schema/<ConfigEntity>.schema.yml
  • src/Entity/<ConfigEntity>.php
  • src/Entity/<ConfigEntity>Interface.php
  • src/Form/<ConfigEntity>Form.php


  • Define your config variables for the entity
    • Strings
    • Booleans
    • Integers
    • Lists


  • Define your getters/setters
    • Not necessary but it helps define your data


  • Build out using Form API
  • Element names must match what is in config schema


Thank you

Drupal 8 Entities

By btmash

Drupal 8 Entities

  • 769
Loading comments...

More from btmash