1

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

1 Answer 1

0

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(&$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(&$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, &$nocache) to true, if you change dynamic data from outside.

2
  • Thanks for the answer. As I mentioned, I had a problem in replacing content from the custom blocks or custom view (related articles). mymodule_NODE_view_alter works fine for page body but but does not work for custom blocks or custom view. For related articles I had use my_module_preprocess_views_view_unformatted and for a custom block I had to use my_module_preprocess_block Variables which hold data are also different for all of them. As a solution, I have moved the replacement logic outside drupal as an outgoing middleware using "auto_prepend_value" and varnish to cache the pages. Commented Nov 27, 2015 at 13:30
  • The view_alter hooks are much earlier than preprocess and easier to handle, because you only have the render array of the node itself to alter and the result can be used everywhere in drupal, you can put it in views or a block (as content not fields). If you use the preprocess of views or the whole block, there is maybe somewhere deep inside a node, but it is more difficult to reach and not all things, that are available, can be used, because they are not processed anymore and it is useless to change them at this stage.
    – 4uk4
    Commented Nov 27, 2015 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.