0

I am trying to display the data from a REST API (URL: 'http://api.kivaws.org/v1/loans/38239/lenders.json') to display in a block. Below is my code. It is not working, any hint on why will be sincerely appreciated.

/**
 * Implements hook_block_info().
 */
function restapiexample_block_info() {
  // This example comes from node.module.
  $blocks['kiva'] = array(
  'info' => t('Kiva Test Block'),
  'cache' => DRUPAL_NO_CACHE
);


return $blocks;
}

/**
 * Implements hook_block_view().
 */
function restapiexample_block_view($delta = '') {
// This example is adapted from node.module.
$block = array();

switch ($delta) {
  case 'kiva':
    $block['subject'] = t('Kiva Example');
    $block['content'] = array(
    '#theme'  =>  'page',
    '#markup' =>  kiva_list_loans(),
  break;
}
return $block;

}

function kiva_list_loans() {
  $request = drupal_http_request('http://api.kivaws.org/v1/loans/38239/lenders.json');
  return drupal_json_decode($request->data);
}

You can try the URL, good JSON data is being returned. Error I receive is several instances of the following text in the $messages area: "Warning: Cannot use a scalar value as an array in drupal_render() (line 5859 of /home/drupalpro/websites/example7.dev/includes/common.inc)"

UPDATE 1: With help from comments/answer below, I made some progress. from my function "kiva_list_loans()", I am able to see the response in my $page array. I changed the '#theme' in my "$block['content']" in hook_block_view to 'page' - but still I only see the word "Array". How to get this to render?

enter image description here

3
  • 1
    You shouldn't need to add "my code attempt inside" to title. That's what we expect to be usual behavior ;)
    – Mołot
    Commented Nov 28, 2013 at 9:08
  • Please don't change the meaning of your question. Questions like that are called chameleon questions and people really hate them. For example, you have just invalidated my answer. You can expect anybody who sees that to refrain from answering you in future. Consider reverting to older revision, and asking new question instead.
    – Mołot
    Commented Nov 28, 2013 at 10:39
  • Sorry i dont see how i am changing the meaning. My challenge was always how to display in a block, I am still not able to do this. I made some progress (with your help, thank you), so i posted the update. I dont think your answer is invalidated, and this comment should show that. Thank you for the help so far. I am still working on how to display the data in a block but i will mark you answer as the answer, and continue work myself or ask separate question.
    – drupalmav
    Commented Nov 28, 2013 at 19:56

1 Answer 1

1

$block['content'] should be an array, like in the example for hook_block_view:

$block['content'] = array(
    '#theme' => 'feed_icon',
    '#url' => 'rss.xml',
    '#title' => t('Syndicate'),
  );

For more examples, see Examples for Developers project, block_example module.

If you are putting array there, it must be a renderable array. If it's not, then either:

  1. Implode it into string. With nested arrays, it can get a bit complicated.
  2. Put it as an element in $block['content'] array, and set $block['content']['#theme'] element to something that can theme it. More complicated, but also more versatile and cleaner solution.
4
  • That's definitely true for best-practice - technically the content is passed through render(), so it can be a string. I suspect OP's current problem is that they're returning an array from kiva_list_loans(), and not formatting it into a render array, so render() throws a wobbly
    – Clive
    Commented Nov 28, 2013 at 9:13
  • @Clive updated. Old problem with "but I know what I mean" on my part.
    – Mołot
    Commented Nov 28, 2013 at 9:16
  • @Mołot What can I use for '#theme' here? I tried the #1-implode option and the output just is "ArrayArray". (The json output can be seen at this link: api.kivaws.org/v1/loans/38239/lenders.json) Sorry if I am asking for a bit of hand holding here, been stuck on this for entire day.
    – drupalmav
    Commented Nov 28, 2013 at 9:22
  • @drupalmav well it means your array has 2 sub-arrays and implode can't work on that directly. Imploding nested arrays was discussed on Stack Overflow already. For a '#theme', pretty good example is in examples project I linked. You would need to write own theming function, but that's not so scary. Try and feel free to ask about details when you will have something (probably copied from examples and only modified to your needs ;) ).
    – Mołot
    Commented Nov 28, 2013 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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