Score:-1

How can I programmatically replace a string on the front page?

la flag

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 = \Drupal\block_content\Entity\BlockContent::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.

Jaypan avatar
de flag
Can you tell us your use case? I doubt that doing this in preprocess_page is the best place to do it, but you haven't told us what your goal is.
apaderno avatar
us flag
Welcome to Drupal Answers! There is also no need to render a block in `hook_preprocess_page()`, which is probably the wrong place to do it, since Drupal core already renders blocks of its own. If you need to alter a block, there is a hook for that.
ridd avatar
la flag
My use case is - that user create block in administration. This block content is with special string ---*blok*--- - this string is saying where in block user wish generate content from another block 'id_block' So I need find this string on front page and replace it with my render block. It work correctly in preprocess_node, but not in preprocess_page because I cannot save content - this not work $variables['page']['content']['kontakt'] = $mynewcontent
Score:0
in flag

Have you considered using the Custom Token and Token Filter modules together?

There's also this brief tutorial on implementing your own custom tokens: https://codimth.com/blog/web/drupal/how-create-custom-token-drupal-8-9

And here is Token Filter's Readme: https://git.drupalcode.org/project/token_filter/-/blob/8.x-1.x/README.txt

ridd avatar
la flag
Thanks. Custom token was good solution. I created it, and now is much better than before :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.