I create a custom module which replaces some string on the front page. For nodes, it work correctly, but it doesn’t work on the front page. I wrote this code.
function my_module_preprocess_page(&$variables) {
if (Drupal::routeMatch()->getRouteName() == 'view.frontpage.page_1') {
$generateBlock = NULL;
$block_manager = Drupal::service('plugin.manager.block');
$block_config = [];
$block_plugin = $block_manager->createInstance('id_block', $block_config);
$block_build = $block_plugin->build();
$block_content = render($block_build);
$block = Drupalblock_contentEntityBlockContent::load(4);
$render = Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
$body = $render['body'][0]['#text'];
$newContent = str_replace('---*block*---', $block_content, $body);
// I don't know how render the new modified content from $newContent.
return $variables;
}
}
I need modify the content with a new string in $newContent
, but I cannot save this content to variables. My content is not modified.