function hook_block_view_alter

You are here

7 block.api.php hook_block_view_alter(&$data, $block)
8 block.api.php hook_block_view_alter(array &$build, \Drupal\block\BlockPluginInterface $block)

Perform alterations to the content of a block.

This hook allows you to modify any data returned by hook_block_view().

Note that instead of hook_block_view_alter(), which is called for all blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a specific block.

Parameters

$data: The data as returned from the hook_block_view() implementation of the module that defined the block. This could be an empty array or NULL value (if the block is empty) or an array containing:

  • subject: The default localized title of the block.
  • content: Either a string or a renderable array representing the content of the block. You should check that the content is an array before trying to modify parts of the renderable structure.

$block: The block object, as loaded from the database, having the main properties:

  • module: The name of the module that defined the block.
  • delta: The unique identifier for the block within that module, as defined in hook_block_info().

See also

hook_block_view_MODULE_DELTA_alter()

hook_block_view()

Related topics

1 function implements hook_block_view_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

menu_block_view_alter in modules/menu/menu.module
Implements hook_block_view_alter().
1 invocation of hook_block_view_alter()
_block_render_blocks in modules/block/block.module
Render the content and subject for a set of blocks.

File

modules/block/block.api.php, line 274
Hooks provided by the Block module.

Code

function hook_block_view_alter(&$data, $block) {
  // Remove the contextual links on all blocks that provide them.
  if (is_array($data['content']) && isset($data['content']['#contextual_links'])) {
    unset($data['content']['#contextual_links']);
  }
  // Add a theme wrapper function defined by the current module to all blocks
  // provided by the "somemodule" module.
  if (is_array($data['content']) && $block->module == 'somemodule') {
    $data['content']['#theme_wrappers'][] = 'mymodule_special_block';
  }
}

Comments

Hello I am using Drupal Commerce and as you all know it generates a view block in the cart(http://example.com/cart) page. I want to add new values to the block content.

Debug:
array (
  'subject' => NULL,
  'content' =>
  array (
    'main' =>
    array (
      '#markup' => '<div class="view view-commerce-cart-form view-id-commerce_cart_form view-display-id-default view-dom-id-ee8be3a1006ea4af9538bbcded51217e contextual-links-region">
      <div class="contextual-links-wrapper"><ul class="contextual-links"><li><a href="/admin/structure/views/view/commerce_cart_form/edit/default?destination=cart">Edit view</a></li></ul></div> 
 
 
      <div class="view-content">
      <div class="commerce-line-item-views-form"><form action="/cart" method="post" id="views-form-commerce-cart-form-default" accept-charset="UTF-8"><div><input type="hidden" name="form_build_id" value="form-pbNaVXhrlMKSRJFz1haf1iurpeWkCUCmLqPUlTboLX8" />
<input type="hidden" name="form_token" value="4zyKsMvF3bYYxgA_n4sWww5nFfggrrV_exCdyYPdA1o" />
<input type="hidden" name="form_id" value="views_form_commerce_cart_form_default" />
<table class="views-table cols-6" >
        <thead>
      <tr>
                  <th class="views-field views-field-line-item-title" >
            Product          </th>
                  <th class="views-field views-field-commerce-unit-price" >
            Price          </th>
                  <th >
            Quantity          </th>
                  <th class="views-field views-field-edit-delete" >
            Remove          </th>
                  <th class="views-field views-field-commerce-total" >
            Total          </th>
                  <th class="views-field views-field-php" >
            Product Image          </th>
              </tr>
    </thead>
    <tbody>
          <tr class="odd views-row-first views-row-last">
                  <td class="views-field views-field-line-item-title" >
            <a href="/gift-cards">Gift Card</a>          </td>
                  <td class="views-field views-field-commerce-unit-price price" >
            $25.00          </td>
                  <td >
            <div class="form-item form-type-textfield form-item-edit-quantity-0">
<input type="text" id="edit-edit-quantity-0" name="edit_quantity[0]" value="1" size="4" maxlength="4" class="form-text" />
</div>
          </td>
                  <td class="views-field views-field-edit-delete" >
            <input class="delete-line-item form-submit" type="submit" id="edit-edit-delete-0" name="delete-line-item-0" value="Remove" />          </td>
                  <td class="views-field views-field-commerce-total price" >
            $25.00          </td>
                  <td class="views-field views-field-php" >
            <div id="place-image" class="image-split-0"></div>          </td>
              </tr>
      </tbody>
</table><div class="line-item-summary">
      <div class="line-item-total">
    <span class="line-item-total-label">Total:</span> <span class="line-item-total-raw">$25.00</span>
  </div>
    </div>
<div class="form-actions commerce-line-item-actions form-wrapper" id="edit-actions"><input type="submit" id="edit-submit" name="op" value="Update cart" class="form-submit" /><input type="submit" id="edit-checkout" name="op" value="Checkout" class="form-submit" /></div></div></form></div>    </div>

</div>',
    ),
  ),
)

I want to modify the string stored in "#markup" array key. Can this be done with this hook. As I am using it to get this value so far. Basically I have to add a new table data in two table rows.

Is there anyway to know if this can be used in a template.php file? I've tried and it doesn't work.

At least for my theme. I was able to call:

my_theme_block_view_alter(&data, $block){}

But you have to clear your cache every time you make a change.

  $blocks['menu']['menu-name'] ...

Since there is no $blocks array available in this hook, I think this is intended for hook_block_info_alter().

Be careful when using switch($block->delta) if the delta is an integer.
PHP uses loose comparisions http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose so when comparing 0 with a string the result is TRUE.

So in the following switch, the subject will be altered for any block with a delta of 0:

function mymodule_block_view_alter(&$data, $block) {
  switch ($block->delta) {
    case 'foo':
       $data['subject'] = 'bar';
       break;
  }
}