简体   繁体   中英

Alter node body in Drupal 8

I am trying to alter the content of all my articles node to insert a div after the first paragraph.

I am currently using the hook: hook_entity_view_alter

function bcom_advertising_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  $text = $build['body'][0]['#text'];
  $end_of_paragraph =  strpos($text, '</p>');

  insertSlimcutDiv($end_of_paragraph, $build['body'][0]['#text']);      
}

In the insertSlimcutDiv function i'm able to insert something after first

If I add normal text it works, if I add a div it is removed by Drupal.

Any idea how I'm supposed to deal with this?

Thank's for your help!

I managed to solve this problem by changing the text format of the body like this:

$build['body'][0]['#format'] = "full_html";

Inside hook_entity_view_alter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM