Skip to main content
removed tags from title
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Drupal 8: hook Hook to replace custom tags in block showing related articles

Source Link

Drupal 8: 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(&$variables)
    {
        $rows = $variables['rows'];
        foreach ($rows as $id => $row)
        {
            $variables['rows'][$id]['content']['#row']->_entity->get('title')->value
                = replace_tags_in_content($variables['rows'][$id]['content']['#row']->_entity->get('title')->value);
        }
    }

Also tried following code:

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

            foreach ($results as $key => $result)
            {
                $title = $variables['content']['#view']->result[$key]->_entity->get('title');
                $variables['content']['#view']->result[$key]->_entity->get('title')->value = replace_tags_in_content($title->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: enter image description here