Show node and user objects as json data: endpoints

Public

Exposes the $node or $user object as json at three different endpoints. This is the super simple example of how to return pure json for node or user data.

Get raw version
php
  1. function nytech_jsonapi_menu() {
  2. $items = array();
  3.  
  4. // shows the same thing as print_r($node);
  5. $items['api/node/%node'] = array(
  6. 'page callback' => 'api_node_json',
  7. 'access callback' => TRUE,
  8. 'page arguments' => array(2),
  9. 'delivery callback' => 'drupal_json_output' ,
  10. );
  11. // shows the same thing as print_r($user);
  12. $items['api/user/%'] = array(
  13. 'page callback' => 'api_user_json',
  14. 'access callback' => TRUE,
  15. 'page arguments' => array(2),
  16. 'delivery callback' => 'drupal_json_output' ,
  17. );
  18. // shows the same thing as print_r($user) but for whoever is logged in.
  19. $items['api/user'] = array(
  20. 'page callback' => 'api_this_user_json',
  21. 'access callback' => TRUE,
  22. 'page arguments' => array(2),
  23. 'delivery callback' => 'drupal_json_output' ,
  24. );
  25.  
  26.  
  27. return $items;
  28. }
  29.  
  30. function api_node_json($node) {
  31. return $node;
  32. }
  33.  
  34. function api_user_json($uid) {
  35. $user = user_load($uid);
  36. return $user;
  37. }
  38.  
  39. function api_this_user_json() {
  40. global $user;
  41. $uid = $user->uid;
  42. $user = user_load($uid);
  43. return $user;
  44. }