How to change entity before it is created or updated.
Example How to change entity before it is created or updated using hook_entity_presave().
function MODULE_entity_presave(EntityInterface $entity)
{
if ($entity->getEntityTypeId() == 'node') {
if ($entity->getType() == 'article') {
$entity->set('title', $title);
}
}
}
Example 2:
/**
* Implements hook_entity_presave().
*/
function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
switch ($entity->bundle()) {
// Here you modify only your day content type
case 'day':
// Setting the title with the value of field_date.
$entity->setTitle($entity->get('field_date')->value);
break;
}
}