Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Create a Simple Custom Entity

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:05
    [Blake Hall] There are a few steps you need to follow to create your own custom entity.
  • 0:08
    In this lesson we'll implement hook_schema to define our entity properties in the database,
  • 0:13
    hook_entity_info to give Entity API a little bit of information about our custom entity,
  • 0:18
    and then we'll take a look at loading it using EntityFieldQuery that we've already seen in a previous lesson.
  • 0:24
    In this lesson we're going to start getting our hands dirty
  • 0:27
    and dive into some code that we need to create our custom video entity
  • 0:30
    to display some YouTube videos.
  • 0:33
    If you'd like to follow along, the code that we'll be working with will be available in the GitHub repository
  • 0:37
    you can see on the screen here.
  • 0:39
    To get started creating a custom video entity,

Create a Simple Custom Entity


This lesson demonstrates the bare minimum needed to create a custom entity type and to load an Entity from the database. We’ll look at implementing a minimum viable hook_entity_info, talk about the relationship between the Entity API and the Schema API and use entity_load to retrieve a single entity record from the database.

Note, although it would be considered best practices to name the entity with the name of the module, e.g. videoentity_video, we did not include the module name prefix here because it is tedious to type it all out and to say "videoentity_video" without confusing people.

In the next lesson we'll cover the various entity classes and how they work. However, if you just want to get straight to using your entity and doing things like $entity = entity_load(); you'll need to declare a controller for your new entity type. Simply add this 'controller class' => 'EntityAPIController' in hook_entity_info(). That will get you started, and well talk about what exactly that line does in the next lesson.

Note: the video doesn't mention the 'primary key' element in the schema array (although it is in the code). This bit is necessary for the schema to install properly and work with the Entity API so if you're following a long make sure you add that part as well.

Log in or sign up to download companion files.