API page: https://api.drupal.org/api/drupal/modules%21block%21block.api.php/functi...

Enter a descriptive title (above) relating to function hook_block_view, then describe the problem you have found:

Blocks are sometimes empty depending on circumstances. A good example in core is the user login block, in https://api.drupal.org/api/drupal/modules!user!user.module/function/user...

    case 'login':
      // For usability's sake, avoid showing two login forms on one page.
      if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {

        $block['subject'] = t('User login');
        $block['content'] = drupal_get_form('user_login_block');
      }
      return $block;

This clearly shows it's ok to return an empty array.

However, hook_block_view() doesn't document this possibility.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Good catch! Maybe it should also mention that if $block['content'] is empty, the block is not shown at all (even the title) -- at least I think that is the case?

alexandre.todorov’s picture

Assigned: Unassigned » alexandre.todorov
Priority: Normal » Minor
Status: Active » Needs review
FileSize
649 bytes

Here is a patch about :

  • May be an empty array so as the block is not shown at all.
  • If The content is empty, the block is not shown at all.
DuaelFr’s picture

Assigned: alexandre.todorov » Unassigned

Removing forgotten assignation

jhodgdon’s picture

Status: Needs review » Needs work

Thanks! This needs a little bit of adjustment though:

...
  *     (preferable) or a string containing rendered HTML content.
+ *     If The content is empty, the block is not shown at all.
+ *   May be an empty array so as the block is not shown at all.

Can we put the last sentence before the list instead of after? Also, for formatting: The new text needs to be wrapped with the previous sentence into lines that go as close to 80 characters as possible without going over. And "The" should not be capitalized.

skipyT’s picture

Status: Needs work » Needs review
FileSize
971 bytes

Modified the documentation, please check again.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks!

The first line of the @return still needs some work:

+ *   May be an empty array so as the block is not shown at all or an array
+ *   containing the following elements:

It really should just start with "Either an empty array..." not "May be an empty array...". And please change "so as the block is not shown at all" to "(the block will not be shown),".

skipyT’s picture

Status: Needs work » Needs review
FileSize
964 bytes

I rephrased the first line and also the last one with the empty content.

skipyT’s picture

The patch file's name was incorrect. Renamed it.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thank you! That is very clear, and I think this is ready to go.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all! Committed to 7.x.

Automatically closed -- issue fixed for 2 weeks with no activity.