Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created August 27, 2015 18:28
Show Gist options
  • Save bangpound/a5827ca2dd07a3587803 to your computer and use it in GitHub Desktop.
Save bangpound/a5827ca2dd07a3587803 to your computer and use it in GitHub Desktop.
Drupal 7 tattoo module adds to `Drupal.settings` object the whole JSON representation of all viewed entities.
name = Tattoo
core = 7.x
dependencies[] = restws
<?php
/**
* Implements hook_entity_view().
*/
function tattoo_entity_view($entity, $type, $view_mode, $langcode) {
$tattooed = &drupal_static(__FUNCTION__, array());
if (!isset($tattooed[$type])) {
$tattooed[$type] = array();
}
list($id) = entity_extract_ids($type, $entity);
// Don't tattoo an entity's JSON representation more than once.
if (in_array($id, $tattooed[$type])) {
return;
}
// Use RESTWS module to generate an array representation of the entity.
$resourceController = restws_resource_controller($type);
$format = restws_format('json');
$json = $format->viewResource($resourceController, $id);
// Embed that entity's JSON in Drupal's global JS settings object.
$data = array(
'entity' => array(
$type => array(
$id => json_decode($json, TRUE)
)
)
);
drupal_add_js($data, 'setting');
$tattooed[$type][] = $id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment