Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Drupal 7 hook_block_info() and hook_block_view() display information in the block

17/04/2025, by Ivan

In the previous lesson, we created a module for Drupal 7. In this lesson, we will continue to enhance our module. We’ll add a block output using hook_block_info() and hook_block_view(). This block will display the latest users on the site, each with a link to their profile page.

Let's start with a description of hook_block_info():

Defines all the blocks created by the module.

This hook tells Drupal which blocks the module will display and can also describe block configuration options.

In hook_block_info(), each block is assigned a unique identifier called "delta" (the keys in the returned array). The delta value must be unique within your module and is used for the following:

  1. Passed to other block hooks as an argument to identify the block to be configured or rendered.
  2. Used to create a block template file block-MODULE-DELTA, which Drupal applies for styling and scripting.
  3. Used to define theming functions block__MODULE__DELTA for advanced theming.
  4. Used to identify your block in hook_block_info_alter() and other related hooks.

Delta can be a string or integer, and its maximum length is 32 bytes.

Return Values

An associative array where each key is a delta, and its value is a description array. Each description array can contain the following key-value pairs:

  • 'info' (required): A human-readable name of the block, shown in the admin interface.
  • 'cache' (optional): Describes block caching behavior. Options include:
    • DRUPAL_CACHE_PER_ROLE (default): Cache based on user role.
    • DRUPAL_CACHE_PER_USER: Cache based on user ID.
    • DRUPAL_CACHE_PER_PAGE: Cache based on page.
    • DRUPAL_CACHE_GLOBAL: Same cache for all users and pages.
    • DRUPAL_NO_CACHE: Block will not be cached.
  • 'properties' (optional): Additional metadata array. Includes:
    • 'administrative': Boolean indicating if the block is for admin purposes.
    • 'weight': Block weight value.
    • 'status': Initial status (1 = enabled, 0 = disabled).
    • 'region': Initial region placement.
    • 'visibility': Block visibility rule (e.g., listed pages, not listed pages, or PHP code).
    • 'pages': A list of paths relevant to the visibility setting.

Let’s add a block with this hook:


The block should now appear in the admin interface. Let's add it to the left sidebar:

drupal block

Now that we have added the block, let's define its content using the hook_block_view() hook.

hook_block_view($delta = ''):

Parameters

$delta — Identifies which block is being rendered. This matches the delta returned by hook_block_info().

Return Value

An array with the following elements:

  • subject: The block title. If no title is needed, return NULL.
  • content: The body of the block, either as a renderable array (preferred) or raw HTML string.

Let’s implement this hook:

fields('u', array('uid', 'name')) // select uid and name
                ->orderBy('u.uid', 'DESC') // order by uid descending
                ->range(0, 5) // limit to 5 users
                ->execute(); // execute the query
            $users = $query->fetchAll(PDO::FETCH_ASSOC); // fetch results
            foreach ($users as $user) {
                $block['content'] .= '

'; // base_path() returns the base path of the Drupal site } break; } return $block; } ?>

This will output the 5 most recently registered users on the site, each as a link to their profile. Of course, it's better to use the l() function for creating links, but that deserves a separate article.

Drupal list users