I'm trying to alter the value of entity text fields upon node save, even the custom-made fields
so far I've managed to do that with the title and default body field but not with a custom field, I'm trying to target any field that might be a text field, here's my code:
function MODULENAME_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
$replacement_pattern = array("/a/", "/b/");
$replacement = array("c", "d");
/* Check whether it's a node */
if ($entity instanceof \Drupal\node\NodeInterface) {
// Node title
$nodeTitle = $entity->getTitle();
$entity->setTitle(preg_replace($replacement_pattern, $replacement , $nodeTitle));
// Body main
if(!empty($entity->body->value)) {
$entity->body->value = preg_replace($replacement_pattern, $replacement , $entity->body->value);
}
// Body Summary
if(!empty($entity->body->summary)) {
$entity->body->summary = preg_replace($replacement_pattern, $replacement , $entity->body->summary);
}
}
}