Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am reading "Drupal 7 Module Development", and I followed the book's steps to create a block in my module.

/**
   * Implements hook_block_info().
   */
  function first_block_hook_info() {
    $block = array();

    $block['list_modules'] = array(
      'info' => t('A listing of all the enable modules'),
      'cache' => DRUPAL_NO_CACHE
    );

    return $block;
  }
  /**
  * Implements hook_block_view()
  */
  function first_block_view($block_name ='') {
    if ($block_name == 'list_modules') {
      $list = module_list();

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

      $block = array(
        'subject' => t('Enable modules'),
        'content' => $content
      );

      return $block;
    }
  }

first_block_view($block_name = '') is using if ($block_name == 'list_modules') to check which block should be rendered, but I think Drupal will only send list_modules' to my module.

Is there any need to check the block name in hook_block_view()?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

While your module defines only a single block, technically no, it's not necessary.

But, as soon as you want to add another block, you'll need to distinguish between that and the existing one, so you'll need to check the block delta.

It's a good practice to get into, even if it's not strictly necessary to test the delta in the block view function.

share|improve this answer
    
Many many thanks, I have understood :) –  Wooden Feb 17 at 10:41
    
One more question, I can not find the block( 'list_module') in Blocks page, How can I enable the block? thanks. –  Wooden Feb 17 at 11:24
    
The title is whatever comes under the info key in hook_block_info()...so it'll be called "A listing of all the enable modules" in your case. You can always change info to something shorter, and use the description key to describe it in more detail. You'll need to clear the caches after doing that for the change to get picked up –  Clive Feb 17 at 11:28
    
Can I disable caches when develop module? –  Wooden Feb 17 at 12:21
    
Certainly can: stackoverflow.com/questions/4892573/… –  Clive Feb 17 at 12:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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