Skip to main content
Category:

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;
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook