【问题标题】:Drupal 8: How to customize the block content on specific pageDrupal 8:如何自定义特定页面上的块内容
【发布时间】:2021-03-14 22:15:18
【问题描述】:

我可以使用下面的钩子来更改块内容。但是,我想更改块内容(例如: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;
}

【问题讨论】:

    标签: drupal-8 alter


    【解决方案1】:

    您可以像这样检查路线名称:

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

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 2016-12-15
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      相关资源
      最近更新 更多