Entities are a great way to organize your data in Drupal. If you are familiar with nodes, taxonomy terms, comments or users, you should also know that since Drupal 7, these have been entities. Another important aspect about them is that they are fieldable via the Field API.
In this tutorial I will show you how you can define your own custom entity type and get started working with it. Why would one want to do this instead of using nodes? Because although they are great, nodes can sometimes be overkill. There is a lot of functionality you may not need such as revisions or commenting.
For the purpose of this tutorial, we will define our own custom entity type called project to represent simple information we have about our projects (title, description and deadline). Then we will look at a few things about working with the entities of this type.
For following along, I assume you know how to write a basic custom module (mine will be named demo). You can find a great tutorial on this subject here. Go ahead and write the .info file and create the empty .module and .install files. I also set up a Git repository where you can get all the source code for this tutorial (one branch per part, two parts will be published).
Additionally, you need the Entity API contrib module enabled on your site and set as a dependency to your custom module. The Entity API module is very powerful when working with entities as it provides a lot of functionality that the Drupal core lacks.
Defining our own entity type
The first thing we need to do to create a new entity type is to declare its schema definition. That is, write the code that will generate the database table for the entity data. In my demo.install file I have the following code:
Continue reading %Build Your Own Custom Entities in Drupal – Setup%
more
{ 0 comments... » Build Your Own Custom Entities in Drupal – Setup read them below or add one }
Post a Comment