| 7 block.api.php | hook_block_view_alter(&$data, $block) |
| 8 block.api.php | hook_block_view_alter(&$data, $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: An array of data, as returned from the hook_block_view() implementation of the module that defined the block:
- 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()
Related topics
1 function implements hook_block_view_alter()
1 invocation of hook_block_view_alter()
File
- modules/
block/ block.api.php, line 270 - 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';
}
}
Login or register to post comments
Comments
Modify string content in $data->content.
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.