Introduction to Drupal 7 Entities and Fields

 

First, some definitions:

Entity

An entity is a container for bundles. Entities can contain multiple bundles. Examples of an eitity are Node, User, File, Taxonomy term, Taxonomy vocabulary.

Bundle

A bundle is a set of fields that are treated as a group by the Field API. Buncles are simillar in concept to a node type in Drupal 6. You can have a bundle that describes an article or a photo or a story, for example.

Fields

Bundles contain fields. A field defines a particular type of data that can be attached to any entity and can be shared between entities. That's an important note; a field doesn't have to be defined by the module that's using it.

Widgets

A field can have widgets. A widget is the UI element that allows a user to enter data into the field. We're going to work with simple text widgets and image widgets to show you how to upload an image and display it in different view modes.

Formatters

A field can have formatters. A formatter defines how a field's data gets displayed in a view mode.

The Examples Module

In my mind, one of the best ways of learning is to just do it. This module used to live exclusively in CVS but is now maintained by rfay, Dave Reid, katbailey and ilo in contrib. Documentation can be found at api.drupal.org by following the Examples link.

D7 *.install

We still implement hook_install() and hook_uninstall(). hook_schema() no longer needs to be explicity called; most of this is done through the field API. The database structure generally resembles Drupal 6 CCK tables. Database entity revisions are provided automatically.

hook_install() will set up your content type. First, we'll get the content types Drupal is already aware of:

$types = node_type_get_types();
node_add_body_field($types['node_example']);

Next, we'll load the instance defintiion for our content type's body:

$body_instance = field_info_instance('node', 'body', 'node_example');

Then we'll add our example_node_list view mode to the body instance display by instructing the body to display as a summary:

$body_instance['type'] = 'text_summary_or_trimmed';

Now, save our changes to the body fielld instance:

field_update_instance($body_instance);

Now we'll create all the fields we are adding to our content type

foreach(_node_example_installed_fields() as $field) {
field_create_field($field);
}

We do a lot of stuff with Drush, so if you want to get a reference to the t() function before Drupal has been bootstrapped, use:

$t = get_t();

The Module File

hook_node_info() just goes through and tells Drupal what our content type is. hook_menu() works similar to Drupal 6 as well.

The rest of the code came too fast to take notes no. Sorry!

Q & A

Q: Can you speak to the extra advantages of the entities model?

A: When you go through and do an entity load on something like a taxonomy field, in d6 you'd have to get your taxonomy id and then query the database again, but in d7 you just do one single entity load that can be easily cached.

Q: Can you show where you defined the formatter?

A: The actual definition of it is in node_example_entity_info_alter(). The definition is simple, and the extra stuff you get out of it is at the field level and the display level.

Q: What happens when you have a field shared by multiple content types? What happens with purges?

A: The purge will only delete the information if it's not referenced elsewhere, so if another module is referring to that field it won't be cleared out. We do a lot of work with Mongo; the nice part about Drupal 7 is that it has the catches in place to make sure you don't delete anything that's being referenced. You can absolutely use fields across different content types.

Q: What's the shortest possible example for how to overwrite the widget part of the field?

A: That's not a short answer. What we do have are the core ones that I showed you; it would take additional time to talk about how to define those, but I think a lot of people will benefit from that.

Did you enjoy this post? Please spread the word.