Drupal 7 Entities:What are they?

In Drupal 6, module developers often use Nodes as an all-purposemethod for having objects that can be saved into the database. Thishack avoids having to write a lot of boilerplate CRUD for allcustom data used in your modules. Nodes in D6 are a powerful toolfor this use case when combined with the flexibility of CCK'scustom fields, and the simplified integration with contrib modules.However, using Nodes for every piece of specialized content means alot of overhead; contributed modules often modify Nodes and addedhooks that operated on them. This is a major performance concern,not to mention a bad architecture for unrelated data.

In Drupal 7, we have entities. Entities not only provide allfunctionality available with Nodes in D6, but they offer much more.Many modules in core are now using Entities for their databaserecords, including node.module and user.module. In this post, we'lldiscuss what Entities are, some poignant examples of Entities, andwhy you would want to use them.

What is an Entity?

Entities can be thought of as the parent class of nodes. Theyare the abstraction of functionality common between Nodes, TaxonomyTerms, and Users (among others). You can see this very clearly bylooking at the Drupal 7 version of user_load_multiple() andnode_load_multiple().

<?php
function<wbr></wbr>user_load_multiple(<wbr></wbr>$uids<wbr></wbr>=<wbr>array(),<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>$conditions<wbr></wbr>=<wbr>array(),<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>$reset<wbr></wbr>=<wbr></wbr>FALSE)<wbr>{<br><wbr><wbr>return<wbr></wbr></wbr></wbr></wbr>entity_load('user',<wbr></wbr>$uids,<wbr></wbr>$conditions,<wbr></wbr>$reset);
}
function<wbr></wbr>
node_load_multiple(<wbr></wbr>$nids<wbr></wbr>=<wbr>array(),<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>$conditions<wbr></wbr>=<wbr>array(),<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>$reset<wbr></wbr>=<wbr></wbr>FALSE)<wbr>{<br><wbr><wbr>return<wbr></wbr></wbr></wbr></wbr>entity_load('node',<wbr></wbr>$nids,<wbr></wbr>$conditions,<wbr></wbr>$reset);
}
?>
 
 

As you can see, the loading behaviour of nodes and users isalmost identical. This makes a lot of code in Drupal 7 core moremaintainable and much cleaner. Building on this, the API defines aconsistent set of functions for getting information such as labeland uri for an entity, which was often annoying extra work in D6.One of the biggest benefits, however, is that you can define yourown custom entities, and they can also be fieldable. In short, youget all of the convenience of nodes without the overhead andunnecessary code.

Entities also have revision tracking support baked in, just likethe the Node module. This lets you have multiple versions of theentity stored in the database, but without having to define therevision-loading logic yourself. For example, if you made a “Wikipage” entity, you could track changes to pages and, with someingenuity, show a diff between two revisions. Setting up thesetables and writing boilerplate SQL and loading code would be timeconsuming and error-prone, but with entities, you can get Drupal tod the hard work for you.

Examples of entities

Some notable Entity types are Nodes, Users, and Taxonomy Terms.However, many contributed modules have moved to Entities for thiskind of functionality. Here are some notable modules that useEntities:

  • Wysiwyg: Uses entities for profiles.
  • Organic Groups: Uses entities for groups,membership types, and memberships.
  • Commerce: Uses entities for products,customers, payments, and line items.
  • Field Collection: Uses entities for“Field-collection items.” The Field collection module allows you toadd fields to the “Field-collection items,” which then appear to beadded to the entity that the field collection was originallyattached to.
  • Relation: Uses entities for — you guessed it —relations. These entities link two other entities together. But,since relations are entities, the relationships themselves can havefields.

Benefits of Entities

Why are entities so cool? Here’s a short list:

Avoiding Boilerplate loading logic and SQL

Writing loading functions like node_load and boilerplate SQL istedious and hard to maintain. With Entities, we can focus on theimportant functionality that is specific to our data type.

Consistent functionality between types

As I said before, entities have a standard interface. They haveURIs, labels, and you can use a standard function to load them.This means more flexible, reliable code.

Not having to work with Nodes

Not having to work with Nodes for every main chunk of data makesyour architecture better. Separating Node functionality from yourown module’s functionality leads to more stable code. Evencontributed modules that abuse nodes won’t get in your way. And themultitude of hooks running on Node operations can be trimmeddown.

The contrib Entity API Module

The confusingly named contrib Entity API module provides evenmore functionality than Drupal core. In addition to all thebenefits mentioned above, it provides saving, deleting, andupdating entities, along with a ton of extra functionality. We’lltalk about this in the next part of this series.

Related Links

Want to learn more about Entities? Here are some links to getyou started.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值