0

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;
}
  • Note: I do not have any error checking in it yet – Vietyank Jan 7 '16 at 23:26
1

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' && is_numeric(arg(1))) 
$nodeid = arg(1);
$node = node_load($nodeid);
$field_leg_id = $node->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->field_name_field_leg_id['und'][0]['value'];
  • If the nid id not in the URL, how do I get it? There could be an alias. The leg_id is a field of the node. It is not in the URL – Vietyank Jan 7 '16 at 21:12
  • Okay. This works even if there is an alias. I am still wondering if there is a way to retrieve the nid without going to the URL – Vietyank Jan 7 '16 at 21:47
  • Check solution below //Edited – Manoj Bisht Jan 8 '16 at 4:12
  • Hi, can u marked it as fixed. – Manoj Bisht Jan 10 '16 at 15:39

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.