Render HTML from a JSON object in custom drupal module.

Recently I needed to build a custom module that would accept JSON from a resource URL, turn that into HTML and place it into a block using context. The module needed to be dynamic depending on the site you were viewing, and pull in a campus code based on the hostname of the site you were viewing. 

The heart of the code that pulls and decodes the json are in these 2 functions:

Now that you have that data the rest of the modle uses block hooks and a custom function to create the render array for the output of the block. 

Here's how I did it.

First I need to create a new module so I created the necessary .info file and the necessary .module file.

Here's my .info file.

Change My Module to your module name.

name = My Module
description = Pull in json and render it as html
package = JSON
core = 7.x

Here's my .module file.

Change my_module to the name of your module and _my_custom_function to suit your needs.



/**
 * @file
 * Example module.
 */

/**
 * Implements hook_block_info().
 */
function my_module_block_info() {
  // This example comes from node.module.
  $blocks['my_block'] = array(
    'info' => t('My Block Title'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function my_module_block_view($delta='') {

  $block = array();

  switch($delta) {
    case 'my_block' :
      $block['content'] = array('#markup'=>_my_custom_function());
      break;
  }

  return $block;
}

/**
 * Custom function to assemble renderable array for block content.
 * Returns a renderable array with the block content.
 * @return
 *   returns a renderable array of block content.
 */

function _my_custom_function() {
  $output = '';

  //Get hostname for the campus codes
  $host = explode ( '.', $_SERVER['HTTP_HOST']);
  $cwc = array_shift($host);

// shortend for blogging purposes.
  switch($cwc) {
    case "campus1":
      $cc = "CC1";
      break;
    case "campus2":
      $cc = "CC2";
      break;
  }

  //This is the json array from the "APIs".
  $json = "http://api.somesite.psu.edu/rest/someuri/v1?campus=";

  //append the campus code to the url so you can dynamically use the url.
  $url = $json.$cc;

  //retreive and parse the json from URL.
  $request = drupal_http_request($url);
  $decoded_json = drupal_json_decode($request->data);


  //get the array position needed for retrieve the 'data' element.
  $data = $decoded_json['recordSet']['DATA'];


  //loop through the json data and add it to the $output array. 
  foreach($data as $info) {

    //Format the output with the info array you just created.

    //First item in the json data output
    $output .= $info[0];
  
    //Second item in the json data output
    $output .= $info[1];

  }

  return $output;
}

You will actually need valid JSON for this to work obviously, and more formatting was done in our actual implementation. This post was intended to point anyone in the right direction for getting started with pulling in JSON objects into a website via a custom module.

Now you can assign this block via the context module.

  • json logo