Drupal 8:如何自定义特定页面上的块内容

Drupal 8: How to customize the block content on specific page

我可以使用下面的挂钩来更改块内容。但是,我只想更改出现在特定页面上的块内容(例如:system_main_block)。但是,我不确定如何在下面的挂钩中获取页面 ID 或标题。感谢帮助。

function yourmodule_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) 

{
  if ($block->getBaseId() === 'system_powered_by_block') {
    $build['#pre_render'][] = '_yourmodule_block_poweredby_prerender';
  }
}

function _yourmodule_block_poweredby_prerender(array $build) {
    $build['content']['#markup'] = Markup::create('Your text');
    return $build;
}

您可以这样查看路线名称:

function yourmodule_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if ($block->getBaseId() === 'system_powered_by_block' && \Drupal::routeMatch()->getRouteName() === '<your_route_name>') {
    $build['#pre_render'][] = '_yourmodule_block_poweredby_prerender';
  }
}