I have this code which I am using to get the body text from a node rewrite it with a regular expression and replace it, (I am replacing all you tube video links with embed codes)
function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
switch ($entity->bundle()) {
case 'article':
$node_body = $entity->get('body')->value;
Drupal::logger('origin-body')->notice($node_body);
$node_body_replaced = preg_replace('/(?:https:\/\/)?(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/', '<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>', $node_body);
Drupal::logger('replaced-body')->notice($node_body_replaced);
$body_encoded = htmlspecialchars($node_body_replaced, ENT_QUOTES, 'UTF-8');
//$entity->body = $node_body_replaced;
$entity->set( 'body',$body_encoded ); // This appears to be where special characters are getting escaped
break;
}
}
Both my logger messages show normal text output in the drupal log so I guess it happens somwhere when the node gets saved or $entity->set(). I have tried running through htmlspecialchars() and it does nothing.
For example, I am migrating content from WordPress and after running through this function my normal HTML becomes
<p>lorem ipsum dolar lorem ipsum dolar lorem ipsum dolar lorem ipsum dolar lorem ipsum dolar lorem ipsum dolar .</p> <!-- /wp:paragraph -->
I am using Drupal 9