How to capture value of a field in a node I am nearly a complete newbie when it comes to writing modules (Drupal 7). I have a module that successfully calls an OpenStates API and retrieves information about a member of the legislature. The key is a custom field called leg_id. In testing this, I hard coded the leg_id in the module that creates the block I want to display.

I now have a node that has a custom field for leg_id. When that node is displayed, I want to pass the value of the leg_id to the module that retrieves the information from OpenStates and display it in a block in the right sidebar.

I need a simple example of how to get the value of the leg_id field and pass it to the module that creates the block.

Here is an example of how the page should look: http://dev-thecivithing.pantheon.io/node/1

Block creation Module Code

&lt;?php
// $Id$


/**
 * Implements hook_help().
 */
function committees_help($path, $arg) {
  if ($path == 'admin/help#committees') {
    return t('A demonstration module.');
  }
}

/**
 * Implements hook_block_info().
 */
function committees_block_info() {
  $blocks = array();

  $blocks['list_committees'] = array(
    'info' =&gt; t('A listing of all of the committees for the member'),
    'cache' =&gt; DRUPAL_NO_CACHE,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function committees_block_view($block_name = '') {
  if ($block_name == 'list_committees') {
    $list = get_committees();
    sort($list, $sort_flags = SORT_STRING);

    $theme_args = array('items' =&gt; $list, 'type' =&gt; 'ol');
    $content = theme('item_list', $theme_args);

    $block = array(
      'subject' =&gt; t('Committee Membership'),
      'content' =&gt; $content,
    );

    return $block;

  }
}


/**
 * Web service to list committees for the member
 */


function get_committees() {

$leg_id='NCL000208';
$apikey='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$list = array();

$request_url = "http://openstates.org/api/v1/legislators/$leg_id/?apikey=$apikey";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$legislator = json_decode($result);
$roles = $legislator-&gt;roles;
    foreach ($roles as $role) {
        if (isset($role-&gt;committee))
            $list[] = $role-&gt;committee;

    }
    return $list;
}


Working Module

&lt;?php
// $Id$


#print $body . PHP_EOL;

/**
 * Implements hook_help().
 */
function committees_help($path, $arg) {
  if ($path == 'admin/help#committees') {
    return t('A demonstration module.');
  }
}

/**
 * Implements hook_block_info().
 */
function committees_block_info() {
  $blocks = array();

  $blocks['list_committees'] = array(
    'info' =&gt; t('A listing of all of the committees for the member'),
    'cache' =&gt; DRUPAL_NO_CACHE,
  );

  return $blocks;
}



/**
 * Implements hook_block_view().
 */
function committees_block_view($block_name = '') {
  if ($block_name == 'list_committees') {
    $list = get_committees();
    sort($list, $sort_flags = SORT_STRING);

    $theme_args = array('items' =&gt; $list, 'type' =&gt; 'ol');
    $content = theme('item_list', $theme_args);

    $block = array(
      'subject' =&gt; t('Committee Membership'),
      'content' =&gt; $content,
    );

    return $block;

  }
}


/**
 * Web service to list committees for the member
 */

function get_committees() {

if (arg(0) == 'node' &amp;&amp; is_numeric(arg(1))) 
$nodeid = arg(1);
$node = node_load($nodeid);
$field_leg_id = $node-&gt;field_leg_id['und'][0]['value'];



$apikey='a1e620771b1d463cb726eda33e8f3102';
$list = array();

$request_url = "http://openstates.org/api/v1/legislators/$field_leg_id/?apikey=$apikey";
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$legislator = json_decode($result);
$roles = $legislator-&gt;roles;
    foreach ($roles as $role) {

        if (isset($role-&gt;committee))
            $list[] = $role-&gt;committee;

    }
    return $list;
}
What u can do is, pass leg_id in the function definition, like:

function get_committees($leg_id = '') {
if(empty($leg_id)) {
$leg_id = 'NCL000208';
}
...


and in the content of block inside block_view, u can call this function, with leg_id, if u are sending leg_id through url, then u can get the leg_id from url using arg() function, and then passing it to function.

But since u want to get Leg_id from node, what u can do is:

if (arg(0) == 'node' &amp;&amp; is_numeric(arg(1))) 
$nodeid = arg(1);
$node = node_load($nodeid);
$field_leg_id = $node-&gt;field_name_field_leg_id['und'][0]['value'];


and sending $field_leg_id to function get_committees($field_leg_id)

// Edited

You can do the same by calling menu_get_object(), it will return you an array, what u can do is:

$menu_obj = menu_get_object();
$leg_id = $menu_obj-&gt;field_name_field_leg_id['und'][0]['value'];