首页 > 解决方案 > 在 Drupal 8 中更改节点主体

问题描述

我正在尝试更改我所有文章节点的内容以在第一段之后插入一个 div。

我目前正在使用钩子: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']);      
}

在 insertSlimcutDiv 函数中,我可以先插入一些东西

如果我添加普通文本,它会起作用,如果我添加一个 div,它会被 Drupal 删除。

知道我应该如何处理这个问题吗?

谢谢你的帮助!

标签: phpdrupal-8

解决方案


我设法通过改变正文的文本格式来解决这个问题,如下所示:

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

内部 hook_entity_view_alter


推荐阅读