how to set date field programmatically drupal 8 / 9

4
(1)

Example how to set date field programmatically drupal 8 / 9

function mymodule_entity_presave(EntityInterface $entity)
{
    if ($entity->getEntityTypeId() == 'node') {
           $dateTime = DateTime::createFromFormat('Y-m-d',date('Y-m-d', time()));
           $publicationDate = $dateTime->format('Y-m-d');
           $entity->set('field_publication_date', $publicationDate);
    }
}

Similar Posts:

3,120

How useful was this post?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Scroll to Top