12

Is it possible preprocess variables only for certain blocks? I created mytheme_preprocess_block__aggregator(&$vars), but it doesn't work.

1
  • The edit you did and the link you pasted there seems to only deal with theme hook suggestions, doesn't it?
    – leymannx
    Commented Nov 17, 2016 at 20:56

3 Answers 3

20

Unfortunately, there is no way to do it like that (similar to hook_form_alter()).

The best way to do this would be to use $variables['block']->bid to apply modifications only to the blocks you want:

function mytheme_preprocess_block(&$variables) {
  if ($variables['block']->bid === 'target_block_id') {
    // do something for this block
  } else if ($variables['block']->bid === 'other_target_block_id') {
    // do something else for this other block
  }
}
5
  • 4
    I looked at Drupal code, and Drupal doesn't looks for hook_preprocess_block_MODULE() or THEME_preprocess_block_MODULE(). It handle in a particular way the __ contained in theme function names, when theme() is invoked as theme('links__contextual__node', ...), for example.
    – avpaderno
    Commented Apr 7, 2011 at 15:37
  • Hmm, good to know!
    – Alex Weber
    Commented Apr 7, 2011 at 17:09
  • 'block_id' is usually the machine name of the block correct?
    – chrisjlee
    Commented Sep 26, 2011 at 14:46
  • 2
    Please make use of $variables['block']->bid and not $variables['block_id'] as 'block_id' is not unique to that block.
    – Duncanmoo
    Commented Sep 5, 2013 at 9:42
  • 3
    I think its a little friendlier to conditionally switch on $variables['block']->delta if $variables['block']->module == 'MODULE'
    – Pete
    Commented May 16, 2014 at 0:02
12

Just to confirm, in Drupal 8 you can write preprocess functions for specific blocks. For example:

Drupal 8

mytheme_preprocess_block__system_branding_block(&$vars) {
  // Make changes to the the system branding block
}

But you could also use hook_preprocess_block, and the plugin ID:

function mytheme_preprocess_block(&$vars) {
  if ($vars['plugin_id'] == 'system_branding_block') {
    // Make changes to the the system branding block
  }
}

As mentioned by Alex, in Drupal 7 you'll have to stick with HOOK_preprocess_block, and an id check:

Drupal 7

mytheme_preprocess_block(&$vars) {
  if ($vars['block']->bid === 'target_block_id') {
    // make changes to this block
  }
}
2
  • Tried it out right now on D8, neither MYTHEME_preprocess_block__system_branding_block(&$vars) works nor does MYTHEME_preprocess_block__page_title_block(&$variables).
    – leymannx
    Commented Nov 15, 2016 at 12:24
  • 3
    mytheme_preprocess_block__{my_block_machine_name}(&$variables) does work on D8.3
    – Tim
    Commented Mar 17, 2017 at 16:43
0

In Drupal 10 (presumably 8 & beyond), you can dig into the $vars array to isolate a particular block type/bundle:

function mytheme_preprocess_block(&$vars){
  $block_content = $vars['elements']['content']['#block_content'];
  if($block_content instanceof \Drupal\block_content\Entity\BlockContent) {
    $block_type = $block_content->bundle();
    // Logic here based on a (custom) block type/bundle.
  }
  else{
    /* A variety of system-generated blocks (logo, menu, etc.) 
       will have other kinds of info in $vars['elements']['content'] 
       you may be able to key off of.
       Views-generated blocks will also provide details 
       about the View and display in $vars['elements']['content'].
    */
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.