Programming Drupal 7 Entities

Cover image for this book
Sub-title: 
Expose local or remote data as Drupal 7 entities and build custom solutions.
Authors: 
Publisher: 
Packt Publishing
Publication date: 
2013-06
Page count: 
134
ISBN-13: 
9781782166528

What you will learn from this book

  • Manipulate and utilize comment, file, field collection, node, term, user, and vocabulary entities
  • Attach, manipulate, and utilize date, file, image, link, number, text, and term reference fields
  • Write type-agnostic code dealing with more types of Drupal data than ever before
  • Upgrade a legacy Drupal node module to expose and utilize entities
  • Expose your legacy database tables as fully-fledged Drupal entities
  • Batch import remote data and expose them as entities
  • Glean good coding practices for dealing with entities

In Detail

Writing code for manipulating Drupal data has never been easier! Learn to dice and serve your data as you slowly peel back the layers of the Drupal entity onion. Next, expose your legacy local and remote data to take full advantage of Drupal's vast solution space.

Programming Drupal 7 Entities is a practical, hands-on guide that provides you with a thorough knowledge of Drupal's entity paradigm and a number of clear step-by-step exercises, which will help you take advantage of the real power that is available when developing using entities.

This book looks at the Drupal 7 entity paradigm, and breaks down the mystery and confusion that developers face when building custom solutions using entities. It will take you through a number of clear, practical recipes that will help you to take full advantage of Drupal entities in your web solutions without much coding.

You will also learn how to expose local and remote data to give your legacy data new life as fully-fledged Drupal entities that can be used by other modules such as views, rules, and so on. In addition to this, you'll learn to create, retrieve, update, and delete (CRUD) entities, their properties and fields, along with some programmatic wizardry to manipulate complex entities such as field collections. If you want to develop quickly and easily using Drupal entities, then this is the book for you.

You will learn everything you need to know to develop code and expose data using entities in Programming Drupal 7 Entities.

Approach

The book follows a standard tutorial-based approach to create, retrieve, update, and delete Drupal 7 entities, their properties and fields.

Who this book is for

Programming Drupal 7 Entities is perfect for intermediate or advanced developers new to Drupal entity development who are looking to get a good grounding in how to code using the new paradigm. It’s assumed that you will have some experience in PHP development already, and being vaguely familiar with Drupal, GIT, and Drush will also help.

Entity List Field Developer Documentation

If you want to use your own class for the Entity List field formatter, write a module with a class that extends EntityListFieldList.

2.x Roadmap

The goal of the 2.x branch is

Case Tracker 7.x-2.x - a new approach, with entities

You can see more about why this branch is being created on this link

Roadmap

Project and Case management for simple use cases
In many use cases, being able to simply add Cases/Tasks(without commenting, gantt charts, etc) and relate them to a Project is the ultimate goal. This is the goal of the first version of Case Tracker 2.x, that will solve many teams simple tasks management.

Entity JS

Entity JS allows for Javascript layer access to some commonly used entity-related functions such as EntityFieldQuery.

This is achieved through Drupal menu callbacks and jQuery $.ajax calls.

Note: jQuery's jqXHR Object uses the .done() method that requires later versions of jQuery (1.5+) than what currently ships with Drupal 7. In order to use these examples, we recommend installing jQuery Update. For more on .done(), review the .promise() method jQuery API docs.

entity_create

Create an entity by providing a type and an array of arguments. Shown printing response below.

values = {
    "name": "username", 
    "mail": "username@example.com",
}
entity_create('user', values).done(function(data) { console.log(data); });

Uses a $.post callback URL: /entity_create/[entity_type]

entity_render_view

Return a rendered entity using a view mode. Shown printing HTML to console below.

entity_render_view('node', 1, 'default').done(function(data) { console.log(data); });
Uses a $.get callback URL: /entity_js_drupal_render_entity_view/[entity_type]/[entity_id]/[view_mode]

Naming conventions

When it comes to entity labels, there are no worries. However, take special care when choosing your ECK entity's 'machine name'. ECK will check to make sure no other entities with the same 'machine name' conflict before allowing you to create it; however, there are aspects that cannot be checked for.

There have been several reported cases of other conflicts happening. See #1345264: Protect against reserved entity type names. In this example an entity of type category was defined using ECK; however, the user module has a function called user_category_load(). So, when the entity API tries to call the load hook for the category entity, it checks for all modules essentially calling a hook_category_load which conflicts with the user modules completely separate use of that function name.

Another example is if I were to define an entity called user_profile, it would seem a valid entity name and, thus, would get created. However, again the user module has a template file user-profile.tpl.php that requires certain variables and gets triggered on the path /user-profile. The same path is used by default for the user_profile entity I created using ECK. This again results in a conflict and it all breaks down.

Renaming entity properties

Yes, you can change the label of any property. This is how you do it:

  1. Go to "Manage Properties"
  2. Under "Add new Properties" fill the values to match the title property (type = text, name = Title, behavior = title)
  3. Click "Edit" on machine-name
  4. Now change "Name" (the label of the property) to whatever you like
  5. Click "Add Property"
  6. Click "Save"

This interface will be improved at some point. Consider helping out with a patch.

Pages

Subscribe with RSS Subscribe to RSS - entity