1

Simple thing. I have some page (node). At end it produces some content, i.e.

<h1>This is original content</h1>

I want to alter it with different content, i.e.

<h1>This is changed content</h1>

So I need a way to collect original content, parse it, replace some part of it and put it back.

hook_ENTITY_TYPE_view() should be the way to do that:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_view/8.2.x

And I tried example from that page (by Devendra Mishra) and it works, but it's adding additional content. I need a way to access original content and change it, not to just add new one.

To be clear, I don't want to change content on field level, but want to change final content, with all fields combined, ready to be rendered.

2

If you don't want to change the variables on field level you can parse the content in a #post_render callback after it is rendered:

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function mymodule_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { 
  $build['#post_render'][] = function ($html, array $elements) {
    return str_replace('original', 'changed', $html);
  };
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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