Author: Aaron Klump sourcecode@intheloftstudios.com
Provide a simple and consistent means to interact programatically with entity data, form submissions and other Drupal data across projects and major versions.
You may also visit the project page on Drupal.org.
composer install
; for more information on composer go here.// Create a getter for node entities
$getter = 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'] = $getter->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'] = $getter->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...
$user_getter = data_api('user');
//... or just reassign the entity type of the original getter to 'user'.
$getter->setEntityType('user');
$vars['mail'] = $getter->get($GLOBALS['user'], 'mail', '{missing email}');
This will also work on native arrays and objects, and offers a means of supplying defaults with minium code. For more information go here.
$subject = array('do' => array('re', 'mi'));
print data_api()->get($subject, 'do.0', 'none'); // === 're'
print data_api()->get($subject, 'do.1', 'none'); // === 'mi'
print data_api()->get($subject, 'do.2', 'none'); // === 'none'
$value = data_api()->get($form_state, 'values.summary', 'none');