Created
          August 27, 2015 18:28 
        
      - 
      
 - 
        
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.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | name = Tattoo | |
| core = 7.x | |
| dependencies[] = restws | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?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