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
<?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' => t('A listing of all of the committees for the member'),
'cache' => 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' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);
$block = array(
'subject' => t('Committee Membership'),
'content' => $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->roles;
foreach ($roles as $role) {
if (isset($role->committee))
$list[] = $role->committee;
}
return $list;
}
Working Module
<?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' => t('A listing of all of the committees for the member'),
'cache' => 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' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);
$block = array(
'subject' => t('Committee Membership'),
'content' => $content,
);
return $block;
}
}
/**
* Web service to list committees for the member
*/
function get_committees() {
if (arg(0) == 'node' && is_numeric(arg(1)))
$nodeid = arg(1);
$node = node_load($nodeid);
$field_leg_id = $node->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->roles;
foreach ($roles as $role) {
if (isset($role->committee))
$list[] = $role->committee;
}
return $list;
}