0

I am trying to create a block using hook_block, in Drupal 7. I have the following code in my module file (module.module its not actually called that but, this is an example).

function module_race_results_block_info($op = 'list'){
  $block = array();

  if ($op == 'list') {
    $block[0]['info'] = t('Race Results Block');
  }

  return $block;
}

function module_race_results_block_view($delta = 'raceresults') {
  $block = array();

  switch ($delta) {
    case 'raceresults':
      $block['content'] = module_race_results();
      break;
  }

  return $block;
}

function module_race_results() {
  $content = "Testing Race Results Block";

  return $content;
}

How can I test this? The block is not showing up in the blocks list (/admin/structure/block).

1 Answer 1

2

Quickly, it's $block['content']=module_race_results(); in your hook_block_view(), and $blocks['raceresults']... in your hook_block_info() so it matches your hook_block_view().

More precisely, this is probably more of what you are trying to accomplish:

function module_race_results_block_info() {
  $blocks = array();

  $blocks['raceresults']['info'] = t('Race Results Block');
  // any more blocks here

  return $blocks;

}


function module_race_results_block_view($delta) {
  $blocks = array();

  switch($delta) {
    case 'raceresults':
      $blocks['subject'] = t('Race Results');
      $blocks['content'] = module_race_results();
      break;

    // any more blocks here

    }
  }

  return $blocks;
}

function module_race_results() {
  $content = "Testing Race Results Block";  // eventually this should become a renderable array instead of plain text or html
  return $content;
}
10
  • 1
    for what its worth, your code as-is shows up on the block list when I run it (refer to Jima for how it should work). I thought a simple reminder to provide a .info file, and enable the module on admin/modules would help. Good luck!
    – Robbase
    Aug 23, 2012 at 15:36
  • thanks for fixing up the code. but its still not showing up in the /admin/structure/block page.
    – Mandeep
    Aug 23, 2012 at 16:57
  • and your module is installed/enabled per @gamengineers comment above?
    – Jimajamma
    Aug 23, 2012 at 17:00
  • (note that given your hooks were somewhat invalid earlier, you may need to disable/re-enable the module for drupal's caches to get back in sync)
    – Jimajamma
    Aug 23, 2012 at 17:01
  • 1
    Okay, it finally showed up. I had the ending php tag in the file. Thanks for your help!
    – Mandeep
    Aug 23, 2012 at 19:51

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.