是否仅适用于某些块的预处理变量?我创建了这样的功能: mytheme_preprocess_block__aggregator(&$vars) 但这行不通。

- 编辑 -

它似乎是在Drupal 8中固定的https://drupal.org/node/1751194

有帮助吗?

解决方案

不幸的是,没有办法这样做(类似于hook_form_alter())。

最好的方法是使用$变量['block'] - >仅将修改仅应用于所需的块:

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
  }
}

其他提示

只是为了确认,在Drupal 8中,您可以为特定块编写预处理功能。例如:

Drupal 8

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

但是您也可以使用hook_preprocess_block和插件ID:

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

如Alex所述,在Drupal 7中,您必须坚持使用Hook_preprocess_block,并进行ID检查:

Drupal 7

mytheme_preprocess_block(&$vars) {
  if ($vars['block']->bid === 'target_block_id') {
    // make changes to this block
  }
}
许可以下: CC-BY-SA归因
scroll top