Fork me on GitHub

Alter a block's build

Here are a few examples of how to alter the builds for any block:

 * Implements hook_blocks_build_alter().
function my_module_blocks_build_alter(blocks) {

  // Add some custom css classes to the block.
  if (blocks.admin_menu) {
    blocks.admin_menu._attributes['class'].push('medium-6', 'columns');

  // Move the logo block to the footer region for authenticated users.
  if (dg.currentUser().isAuthenticated()) {
    blocks.logo._region = 'footer';

  // Add some content before and after the main menu block.
  blocks.main_menu._prefix = '<p>$19.95 + shipping</p>';
  blocks.main_menu._suffix = dg.render({
    _theme: 'item_list',
    _items: ['Foo', 'Bar']


Alter a blocks's view

Here are a few examples of how to alter the view (i.e. block content) for any block:

 * Implements hook_block_view_alter().
function my_module_block_view_alter(element, block) {

  // Inspect the element and block to reveal who and what to alter.

  switch (block.get('id')) {

    // Add a link to the main menu.
    case 'main_menu':
        dg.l('Map', 'map')

    // Make the powered by block's text red and add a custom class to it.
    case 'powered_by': = 'color: red;';

  // On the hello world page, add a prefix to the title block.
  if (dg.router.getRouteKey() == 'example.hello-world' && block.get('id') == 'title') {
    element._prefix = '<i class="fa fa-cog"></i>';