Hook to replace custom tags in block showing related articles I use a single drupal8 installation to serve content for multiple website's FAQ section. So I have put some custom tags like "##website_name##" in the html in article content and title.

Depending on the current domain, the tags get replaced. I have successfully replaced the tags in article content and title while displaying article page.

I have also created a block view to show related articles to the current article depending on the taxonomy of the article.

Problem is the tags I had placed in article title is not getting replaced when the related article block is replaced.

    function my_module_preprocess_views_view_unformatted(&amp;$variables)
    {
        $rows = $variables['rows'];
        foreach ($rows as $id =&gt; $row)
        {
            $variables['rows'][$id]['content']['#row']-&gt;_entity-&gt;get('title')-&gt;value
                = replace_tags_in_content($variables['rows'][$id]['content']['#row']-&gt;_entity-&gt;get('title')-&gt;value);
        }
    }


Also tried following code:

function my_module_preprocess_block(&amp;$variables)
{
        if ($variables['plugin_id'] == 'views_block:related_articles-block_1')
        {
            $results = $variables['content']['#view']-&gt;result;

            foreach ($results as $key =&gt; $result)
            {
                $title = $variables['content']['#view']-&gt;result[$key]-&gt;_entity-&gt;get('title');
                $variables['content']['#view']-&gt;result[$key]-&gt;_entity-&gt;get('title')-&gt;value = replace_tags_in_content($title-&gt;value);
            }
        }
}


When I inspect the title value in debugger, values are getting replaced but when the output is shown on the page the custom tags are still present.

Following the the screen shot for the view setting:
This is not easy to solve in preprocess, it is better to do this earlier when the view() of the node is executed, for example:

function mymodule_NODE_view_alter(&amp;$build) {
    if ( isset($build['body']['0']['#text']) ) {
      $build['body']['0']['#text'] = replace_tags_in_content($build['body']['0']['#text']);
    }
}


This works, no matter if the node is used as page or in views (as content).

You might run into caching issues, when you make changes, that are not coming from the database and drupal does not know, which cache tags have to be invalidated. This would be a version of this code to address this:

function mymodule_NODE_view_alter(&amp;$build) {
    $nocache = false;
    if ( isset($build['body']['0']['#text']) ) {
      $build['body']['0']['#text'] = replace_tags_in_content($build['body']['0']['#text'], $nocache);
    }
    if ( $nocache ) {
      $build['#cache']['max-age'] = 0;
    }
}


You have to set $nocache in the function replace_tags_in_content($string, &amp;$nocache) to true, if you change dynamic data from outside.