With my current module I have a simple form where a user inputs some information, that information is then sent to an external API, and that API sends some data back.

How do I display the data that is returned from the external API?


 * Implements hook_menu();
function find_rep_menu(){
    $items = array();
    $items['find-representatives/%'] = array(
        'title' => 'My Form',
        'page callback' => 'find_representative_value_callback',
        'page arguments' => array(2),
        'access callback' => TRUE,
    return $items;

 * My form
function find_rep_form($form, &$form_state) {
    $form['address'] = array(
        '#type' => 'textfield',
        '#title' => t('Your Address'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Find Rep'),
    return $form;

 * Submit handler for the find_rep_form.
function find_rep_form_submit($form, &$form_state) {
    $address = $form_state['values']['address'];
    $fetchGeocode = fetchGeocode($address);
    fetchStateInfo($fetchGeocode['returnedLat'], $fetchGeocode['returnedLong']);
    $form_state['redirect'] = 'find-representatives/' . $form_state['values']['address'];

function find_representative_value_callback($address) {
    return t('Value: @value', array('@value' => $address));
    //Once I was able to get this working, I was going to push my returned data through here
     * The fetch code
    function fetchGeocode($userAddress) {
        $userAddress = urlencode($userAddress);
        $fetchUrl = "https://api.mapbox.com/geocoding/v5/mapbox.places/" . $userAddress . ".json?country=us&proximity=38.8977%2C%2077.0365&types=address&autocomplete=true&access_token=APIKEY";
        $result = drupal_http_request($fetchUrl);
        if (in_array($result->code, array(200, 304))) {
            $geoResponse = json_decode($result->data);
            $long = $geoResponse->features[0]->geometry->coordinates[0];
            $lat = $geoResponse->features[0]->geometry->coordinates[1];
            return array('returnedLat' => $lat, 'returnedLong' => $long);
        } else {
            drupal_set_message("Geocode returned: " . $result->code); //For testing purposes

    function fetchStateInfo($userLat, $userLong) {
        $userLat = urlencode($userLat);
        $userLong = urlencode($userLong);
        $fetchUrl = 'http://openstates.org/api/v1//legislators/geo/?lat=' . $userLat . '&long=' . $userLong . '&apikey=APIKEY';
        $result = drupal_http_request($fetchUrl);
        if (in_array($result->code, array(200, 304))) {
            $stateInfo = json_decode($result->data, true);
        } else {
            drupal_set_message("State API Returned Error: " . $result->code);

I was taking some examples from example two on this comment here

  • What version of Drupal are you using? And what have you already tried? – acrosman Jul 19 '16 at 20:34
  • Drupal 7. I have tried displaying data through a menu callback. – Schaffer Jul 19 '16 at 20:38
  • Using a menu callback is potentially a good approach. You'll get better help if you post the code and explain where you got lost. – acrosman Jul 19 '16 at 20:39
  • Please post what you have tried. – Jimmy Ko Jul 20 '16 at 9:09
  • I'm out of the office today. Of post tomorrow when I'm back in. Thanks for the help and patience – Schaffer Jul 20 '16 at 16:04

If you don't need to save the data returned by the external source in your drupal database, then you can go through below mentioned points,

  • In hook_form_form_id_alter declare a custom submit function

    $form['#submit'][] = "my_module_form_submit_handler";

  • in the custom submit function redirect after submission.(are you using cURL to send and receive data to and from external api.)

$value1 = 'value returned from external api'; $value2 = 'value returned from external api'; $form_state['redirect'] = 'myurl/' . $value1.'/'.$value2 ;

  • Create a menu item with hook_menu and pass the values as argument

    $menu['myurl/%/%'] = array ( 'title' => 'some title', 'page callback' => 'my_page_callback', 'page arguments' => array(1, 2), // 1 and 2 are the first and second indexes of the URL - the location of the wildcards 'access callback' => TRUE, );

  • in page callback function pass the variables

    function my_page_callback($val1,$val2) { return theme('custom_data_page', array('val1' => $val1,'val2' => $val2));


  • implement hook_theme

function mymodule_theme($existing, $type, $theme, $path) { return array( 'custom_data_page' => array( 'variables' => array( 'val1' => val1, 'val2' => val2 ), 'template' => 'your_template_filename', // do not include .tpl.php 'path' => 'path-to-your-template-file' ) ); }

  • Create the template file in templates folder

  • Clear cache. Let me know if you face any issues

|improve this answer|||||

You can complete this by two way:

1. You can render the output on same page where the form is:

After submitting the form, simply redirect to the same page with a query param and display the result based on the query.

2. You can render the output on another page:

On submit the form, you can get the response data and store it in session, then redirect to another page and render the data.

To render data you have to define an theme function like

function my_module_theme() {
  $themes = array(
    'response_data' => array(
      'template' => 'template/response_data', // path of template file
      'variables' => array('data' => NULL), // pass args 
  return $themes;

for more info theme_function, theme_function

Now create an tpl file in the module directory template/responce-data.tpl.php and render all the data there.

Note: Do not forget to clear cache

|improve this answer|||||

My solution here would be to store the responses in a variable from your function:

if (in_array($result->code, array(200, 304))) {
        $stateInfo = json_decode($result->data, true);
        // drupal_set_message($stateInfo[0]['full_name']);
        return $stateInfo;

Then create a hook_theme function that sets the theme vars and tpls etc. and use that in you callback, eg something like this:

return theme('your_theme_var', array('data' => $data));
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.