Very interesting video to understand what is an entity and when to choose a content type or another entity type !
Drupal 7 Entity Construction Kit
The Drupal 7 Entity Construction Kit Module (ECK) can be used to build your own entities on a Drupal 7 website. In this long episode of the Daily Dose of Drupal, I attempt to go through what I know about the Entity Construction Kit module and how it can benefit a site builder in order to build their own custom entities.
In this episode you will learn:
- An overview of Drupal 7 entities and how the Entity Construction Kit can fit into the mix
- How to decide whether to use a Drupal content type or a Drupal entity to store data
- How to build out an entity type with the Entity Construction Kit
- How to add properties to an entity type with the Entity Construction Kit
- How to add bundles to an entity type with the Entity Construction Kit
- How to add fields to an entity type bundle
- How to create a Drupal View that displays a list of your custom created entities
Hello everyone and welcome to another Daily Dose of Drupal, today we’re on Episode Number 86. I’m Shane Thomas and you can follow me on Twitter at smthomas3, you can also go to codekarate.com and sign up for the newsletter on the left and check out the other Daily Dose of Drupal videos and blog posts.
Today I’m going to attempt to go over the Entity Construction Kit Module and attempt because I’m not going to claim to be the absolute expert on when this should be used in all cases and how it should be used but I’m going to go through it to the best of my ability and if you have any suggestions or anyways that you’ve used it I would be anxious to hear about it.
So there’s two different ways you can use it; you can use it from a site builder’s perspective or a developer’s perspective. I think the goal would be if you can do it through the developer’s perspective if you have that capability I would suggest maybe doing it that way it’s probably going to be a lot easier to maintain.
However; I’m going to go through this site builder’s perspective in this case because it’s just a little bit easier to get started with and you can get started right away. So first of all; what is the Entity Construction Kit? Well Entities are new in Drupal 7 and basically what this Entity Construction can allows you to do is create your own entities and basically you can think of it as it’s almost like a way to model your data using a little bit more like object oriented approach.
If you’re familiar with programming terminology basically it’s going to allow you to model your data in a specific way. For instance there are entities on your Drupal 7 site when you get it installed. The Node Entity, the User Entity, the Comment Entity and each of these entities has what’s known as the Bundle. The Node Entity has the Content Bundle and what that’s use for is displaying content across your site.
Many times in the past I know I have and many of you have used especially if you’re from Drupal 6 use CCK or content types in Drupal to build out different types of data that you’re going to store. In many cases it may not be suited to actually be what is considered content. If it doesn’t need a URL or commenting or any of the extra features that the Node Module allows you to have then it may need to be or maybe best to be structured as it’ s own entity and the Entity Construction Kit allows you to rapidly build those different entities out.
So there’s a lot of confusion in where you should use content types versus when you should use Entities using the Entity Construction Kit or building your own Entities through code and although there’s no hard and fast rules since there’s a lot of grey area where it could work both ways. You can kind of think of it as if it’s displaying content that you want users to see on a full page on your Drupal site then you probably are best using a content type.
If you’re just storing the data for use in other things and it’s more of just a Data Storage tool and you want to model the data in a certain way then the entity Construction Kit can often help you with that and maybe a better solution if you don’t need all the extras after Node Module gives you.
So we’re going to go ahead and get started; the first thing I would recommend you to do is look through the documentation. This isn’t a simple module that you’re going to understand all the concepts on how it works in just a few short minutes. So you may want to read through the different documentation and we’re going to go through the example that they provide here in our example so we’re going to follow along this or for the most part at least a portion of it.
So you can see what building an Entity is like. So it this example it uses Vehicles. You can think of vehicles as a vehicle may have specific types of information, in this case they mention make model and year but they also mentioned that there’s different types of vehicles. You may have a car, you may have a truck and those are two different types of vehicles and a car and a truck may not have all things that are the same.
So the things that are the same, this information here is for both cars and trucks but for instance a truck may have a truck bed. In this case it’s a different piece of information that the car would not have. So when you look at it in Entity terminology because a lot of different terminology that you’re going to hear … we can look at it this way; a vehicle is considered the entity type.
This entity type can have different properties and this … all these properties are available in all the different bundles in this case that are going to be created. All the different implementations or objects that are going to have these specific properties. So in this case the properties of the vehicle entity type are going to be make, model and year. You can notice that there’s bundles and fields. Well each entity is going to have at least one bundle generally.
In this case we’re going to have two bundles; we’re going to have bundle for cars and a bundle for trucks. And then example of node in Drupal 7 the Node Entity has a bundle of content which is used and you notice that when you build a content type you can add fields. Well when we build out the car bundle or the truck bundle we’re going to be able to add fields to either of those bundles and you’re going to build the manager just like you would on normal content type.
So in this case we’re going to need a field for Bed Type that we’re going to add only to trucks because we want the trucks to have it but we don’t want the car to have it. And entities or the individual entity itself is going to be the actual implementation of this data. So if I enter in a 2004 Ford Ranger with an open bed that is going to be a specific entity.
So it’s still maybe a little hazy or a little confusing on how this works so I’m going to go through a quick example. I already have the module installed, this is a pretty much a fresh Drupal site, you’ll see it requires C Tools and the Entity API, I have the Entity Construction kit enabled, there’s also an ECK example Module which I would recommend if you’re looking to do some of the or use Entity Construction Kit from code.
So I’m going to go ahead and go to Configure, you can also go to Structure and click on Entity Types to get you the same place and we’re going to add our Entity Type, I’m going to add the Entity Type for Vehicle. Here I’m going to specify our bundle, I’m going to start with car, we’re going to need to specify both though.
These properties are what … the properties are going to be shared across all of the different actual entities that we create. So the entity type is going to have these properties that are going to be shared across all cars and trucks. So I’m going to select Title and I could select Author, Created and Changed and it would create these, I’ll go or add these properties, I’ll go ahead and select Created and click Save.
Now it has created this entity type. So as you can see I have vehicle here; if I click on Vehicle it brings me to the different bundles. I have a bundle for car. I’m going to make sure I add my bundle for truck as well. I can go into manage properties for here and you can see that I have the title and the created.
It allows you to add new properties so you can select the different type of data you want to store and in this case I want to store text and I want this to be the make. Now these behaviors is a little confusing and it’s still be a little confusing to me but basically the behaviors are how the functionality of this property is going to work. So if I were to select change author or created, that’s going to have a different behavior necessarily on the title.
You can define your own behavior and functionality for how you want this property to react in code and there’s some documentation on drupal.org for how to do that. Because I don’t want to use any of this but I guess I’ll go ahead and select title just so the field is actually displayed but you’ll notice that this isn’t exactly ideal either but I’ll go ahead and use this and I’ll show you why in a second.
After you had add Property you’re going to want to make sure you select this box and check it so it actually gets turned on. I’m also going to say model, I’ll select the same thing, edit, check it and then save it and then I am going to select year which is going to be an integer and I’m going to select the same one; add Property and then save it.
So now all of these properties are going to be shared across both of the bundles. In this case the bundles that we have are Car and Truck. So I’m going to go ahead and click on Truck and you’ll notice this is going to look a little bit more familiar. There’s Manage Fields and Manage Display. Similar to how you build a content type. We’re going to put in Truck Bed as a Field and we’ll call this a Text Field but we can use any different type of field here as you can see because it just uses the traditional field system.
So after we get this we’ll save it, we’ll go through the defaults here and now we have a field and it’s only on this Truck bundle. You can of course manage the display for how that’s going to be displayed but you’ll notice if I go into vehicle, go into car in manage fields there is no field here. So in this entity list you can actually add data, I’m going to go ahead and do that.
You can see I have title, make and model. Looks like I didn’t actually get the year added … I forgot to check that so let me go ahead and add that. It’s a two-step process for getting this properties added. I actually have to do that and then make sure you actually check it there and now I’ll go into Car and I’ll add a Car so we’ll do 2002 Chevrolet Cavalier.
So the make is a Chevrolet, the model is a Cavalier and the year is 2002. So I save this and you’ll notice this behavior that we selected which was title. You’ll notice that the behavior requires the field to be selected, it also does some other things that makes it less than ideal so that’s why I’m not exactly sure the best way to select or have this common properties across and still use this interface but we’ll go through it and you’ll notice that the name here says 2002.
Well this is because it’s the last title behavior that’s selected so it’s going to show this field. If I didn’t have these three fields it would show this one. It’s a little confusing and like I said I’m not exactly sure why this is and I’m not sure that this is even the exact ideal way to use it but you can see that you have the various different fields and it does have the various lists of the different cars that you have.
So I’m going to add another car and we’ll go 2001 say Chevrolet Corvette. Now I’m going to go ahead and head over to Truck and I’m going to just add one truck, you’ll notice that there’s a truck bed. It’s not required because I didn’t make the field required but I’ll say this is 2002 Ford Ranger. It’s a Ford model is Ranger 2002, let’s say it’s an open bed Ford Ranger.
So we’ll click Save, you can see it’s still the same little issue here. Okay so what this allows you to do now that we have our Entity created and we have the vehicle entity and the two different bundles. Now you can of course view one of these. When you view it it says it has the different listings here; this is the title, the model, the year. If I come in to Truck and select this one it’s going to also show me the truck bed field which I can change that from the Manage Display.
As I mentioned before you may want to look into writing your own custom behavior for this property so you don’t have the same issue but one of the cool things you can do with this now is if you go ahead and download the Views Module and enable that, you can do some cool things because it integrates with Views right out of the box. So we come back in here, the Views module is now downloaded, we’re going to add a new view and this time I’m going to show Vehicle.
So let’s Vehicles and you could select if you will only show Cars or Trucks so let’s say Vehicles, I’m going to give it a path because I’m just going to create a simple page, click continue and edit. Now we can add the different fields, you can see I can add the make, the model, the title and we’ll also add the year.
I’ll go ahead and add and Edit and Delete link, let’s leave all these at the default to keep it quick and I will change this to a table so it’s just a little bit easier to get looking nice and I will rearrange this a little bit to have the ID, Title, Make, model, Year then Edit and Delete link at the end and we will save this and check out the page and see how it looks.
Now you can see I have created my entities, I have created the bundles and I have a listing of all the different data that I’ve stored, I probably want to get rid of this comma. I can do that through Views of course. And now it works with views just like any other content type would. You can display the data, you can use that in any other type of data storage and data model that you need on your Drupal site and it just allows you a little bit extra flexibility. It kind of adds that extra layer.
You’re not only just have a content type which is one layer. If you’re going to do the same thing you would have to have a Car Content Type and you’d have to have a Truck Content Type and then none of those properties would be able to shared, you’d have to have fields on each or you’d have to have one content type called Vehicle and have some type of conditional fields where you’d select what type of vehicle is this and then depending on the vehicle you’d see different fields there.
As you can see it gets confusing but using entities that adds that extra layer so you can kind of abstract some of the commonalities which in this case the make, model and year away from the bundle and put it into the actual entity itself. So it kind of adds that extra flexibility, it also doesn’t have all the extra overhead that the node system has. So if you don’t need to display it, in this case I’m just storing the data, I don’t need a specific page for each car, I don’t need comments for the car, I don’t need any of those extras so the Entity Construction Kit would work out pretty well in this example.
And as I’ve said before if you have any suggestions for how you use the Entity Construction Kit or any ways that I could be using it better or more efficiently please let me know. I’m just getting started using it and I would appreciate any feedback. So thanks for watching this episode of the Daily Dose of Drupal. Go ahead and follow me on Twitter if you’re not already and I’ll be back again next time with another episode. Thanks for watching.
I agree. Very interesting, helpfull, and very well produced. It explains entities very well so even beginners, like me, can understand it easily... Great work Shane :)
Interesting, seems a more complete version of the 'Model Entities' module: http://drupal.org/project/model
I used the above to help create stand alone 'spotlights' on a site I worked on and judging from the video and a quick play about it appears to be less complex than this module. There's no doubt that this is very promising but I think it requires more work out of the box to perfect it. That being said I would now use this over model entities simply due to the maintenance status of the projects.
Thanks for another great video.
Nice and interesting idea, an abstract concept... I still can't imagine a real good application for this, or why not use only content types.
Thanks for the video, the examples are good to understand how to use the module.
He explains why you would or would not at around 3:00 min in. Otherwise this tool is not for the novice who is looking to build Wordpress like sites with little data interaction with other entities.
Very interesting video to understand what is an entity and when to choose a content type or another entity type !
first of all... many thanks for your great efforts and time to produce all these videos and tips!
Regarding entities, i am still not sure about pro and con between content- and entity-types. Is it possible to describe what exactly is different, in other words: What allows one or the other? Maybe in regards of fronted display and backend features?
I got the idea about common and un-common properties... but what is the benefit creating this difference in one "type".
Sample usecase and idea in my mind: technical data-sheets (dynamic tables). i.e. a product catalog with similar machines, that share (almost) common technical specifications and numbers. Is this something for ECK ?
following your channel and RSS ;-)
This is a very nice introduction to ECK, thank you.
Regarding behaviors. A behavior attaches functionality to a property, but it also allows us to define “widgets” and “formatters” for the properties. As you notice the title property give you some functionality (make whatever is in that property the title of the entity), but it also give your a widget and formatter, so you can edit and display the property. Bundling functionality and display into one concept (behaviors) was a mistake, and I am hoping to fix it soon. Just like with fields, ECK should allow the user to choose a widget or formatter independently of a piece of functionality.
Regarding use cases, I believe that nodes and content types can do everything and in some cases more than what you can do with an entity type created with ECK, because there is so much functionality out of the box, and also because there are so many modules that enhance what nodes can do. This is great, but piling more and more functionality on top of nodes, just because there are no other options, does not seem like the right approach to me. I like to have well define elements in my site, with specific functionality that relates just to them, and that is how I have used ECK. I create all my data models with ECK from the UI, and then create their display and functionality in code.
The idea of behaviors came up so developers could share the pieces of interesting functionality they were writing with others, but as I mentioned before, the concept of behaviors needs to be refined.
So I would say that if you are a developer and you want a cleaner data model slate to work from, use ECK. But if you are mainly I site builder, and you do not want to do any code, Nodes might still be the better option.
Hopefully, soon, ECK will be FULLY integrated with modules like Panels, Display Suite, and Rules so anyone, not only developers, can enjoy a more flexible data model.
Been crawling all over your daily dose videos the last few weeks - thanks for all the great info! One thing I noticed (or maybe overlooked) on this video. If you don't have the Field UI module enabled and click on a bundle name, you only get the 'Entity List' and 'Delete' tabs - once the Field UI module is enabled the "Manage Fields" and "Manage Display" tabs appear.
good job , thank you so much
Thanks for showing us the module. I use entitys when i dont need comments and body fields as a nodetype would have... its overkill for some usecases.
Glad to learn different lessons about Drupal various pros and cons! I'm about to build my first website and looking forward to use Drupal for my site on page constructions and these types of instructions is very effective to improve my constructions knowledge. Thanks.
I played with ECK, and it looks great, you can create little data records that are not nodes, and reference them with Entity Reference in nodes. That is pretty neat.
However, I needed these entities created from a node, when editing the node, and then render the node based on the newly created entities. The node then becomes a collection of non-node entities. Now, my problem was that I couldn't find a way to create the entities on the fly from a node.
We can create the entities first, from the ECK admin interface, and then reference those entities in nodes, but that is too geeky for most content writers.