Preprocess blocks using hook_preprocess_block

You, DrupalDrupal theme
Back

Preprocess is a very common function in Drupal to alter the content and other properties like Lable of a block. The Drupal.org URL itself also provides a glimpse of what it looks like to preprocess.

So here are the ways to preprocess blocks within your theme.

Drupal 8

function THEME_preprocess_block(&$vars) {
  if ($vars['plugin_id'] == 'system_branding_block') {

  }
}

When even the plugin_id is something like this "block_content:58fb8b64-dead-4c31-95b4-86adfe9113c9" you can stil use it.

If the plugin ID is a string you can use like below too

function THEME_preprocess_block__system_branding_block(&$vars) {

}

Drupal 7

In Drupal 7 this is fairly straightforward.

function THEME_preprocess_block(&$vars) {
  if ($vars['block']->bid === 'system_branding_block') {

  }
}
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.