You must execute composer install once you've added this module to your project before it will work. A fatal error will occur otherwise.

Summary

Provide a simple and consistent means to interact programatically with entity data, form submissions and other Drupal data across projects and major versions.

Requirements

Installation

  1. Install as usual, see http://drupal.org/node/70151 for further information.
  2. Then run composer install from inside the data_api module direcory; for more information on composer go here, e.g. cd sites/all/modules/contrib/data_api && composer install.

Configuration

  1. No configuration is provided.

Suggested Use

More code examples are available if you enable the Advanced Help module.

Entities

// Create a getter for node entities
$n = data_api('node');

// Load a node entity
$node = node_load(4503);

// Use the getter to pull the first name or default.
// Do not include the language key; language is determined automatically.
$vars['name'] = $n->get($node, 'field_first_name.0.value', '{first name}');

// You can resuse the node getter with a new node.  The node getter can be reused as long as the entity type doesn't change.
$node = node_load(345);
$vars['name'] = $n->get($node, 'field_first_name.0.value', '{first name}');

// But to pull data from a different entity type, you can either create a new getter for user entities...
$u = data_api('user');

//... or just reassign the entity type of the original getter to 'user'.
$n->setEntityType('user');
$vars['mail'] = $n->get($GLOBALS['user'], 'mail', '{missing email}');

Arrays and objects

This will also work on native arrays and objects, and offers a means of supplying defaults with minium code. For more information go here.

// Create a global getter that uses no entity type.
$g = data_api();

// Using a standard array...
$array = array('do' => array('re', 'mi'));

// Access it's elements.
print $g->get($array, 'do.0', 'none'); // === 're'
print $g->get($array, 'do.1', 'none'); // === 'mi'
print $g->get($array, 'do.2', 'none'); // === 'none'; the default

And form submissions...

$value = data_api()->get($form_state, 'values.summary', 'none');

Use callback to load an entity reference

The fourth argument is a callable that receives the value and the default, so you can post process the value, e.g.,

$related_node = data_api('node')->get($node, 'field_related_node.0.nid', null, function ($nid, $defaultValue) {
    return $nid ? node_load($nid) : $defaultValue;
});

Design Decisions/Rationale

Roadmap/Drupal 8

I'm planning a Drupal 8 version which will follow the same patterns so you do not have to relearn a new api.