user1829449
user1829449

Reputation: 69

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

I'm able to change the block content by using the hook as below. But, I would like to change the block content (for eg: system_main_block) only if it appears on specific page. But, I'm not sure how to get the page id or title in the hooks below. Appreciate help.

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

Upvotes: 1

Views: 1776

Answers (1)

Kien Nguyen
Kien Nguyen

Reputation: 2691

You can check the route name like this:

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

Upvotes: 2

Related Questions