Same name in other branches
  1. 2.x src/EntityClone/Content/ContentEntityCloneBase.php \Drupal\entity_clone\EntityClone\Content\ContentEntityCloneBase::setCreatedAndChangedDates()

Resets the created and changed dates on the cloned entity.

Since we don't want the cloned entity to have the old dates (as a new entity is being created), we need to reset the created and changed dates for those entities that support it.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The cloned entity.

bool $is_translation: Whether we are recursing over a translation.

1 call to ContentEntityCloneBase::setCreatedAndChangedDates()
ContentEntityCloneBase::cloneEntity in src/EntityClone/Content/ContentEntityCloneBase.php
Clone an entity.

File

src/EntityClone/Content/ContentEntityCloneBase.php, line 350

Class

ContentEntityCloneBase
Class Content Entity Clone Base.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

protected function setCreatedAndChangedDates(EntityInterface $entity, bool $is_translation = FALSE) {
    $created_time = $this->timeService
        ->getRequestTime();
    // For now, check that the cloned entity has a 'setCreatedTime' method, and
    // if so, try to call it. This condition can be replaced with a more-robust
    // check whether $cloned_entity is an instance of
    // Drupal\Core\Entity\EntityCreatedInterface once
    // https://www.drupal.org/project/drupal/issues/2833378 lands.
    if (method_exists($entity, 'setCreatedTime')) {
        $entity->setCreatedTime($created_time);
    }
    // If the entity has a changed time field, we should update it to the
    // created time we set above as it cannot possibly be before.
    if ($entity instanceof EntityChangedInterface) {
        $entity->setChangedTime($created_time);
    }
    if ($is_translation) {
        return;
    }
    if ($entity instanceof TranslatableInterface) {
        foreach ($entity->getTranslationLanguages(FALSE) as $language) {
            $translation = $entity->getTranslation($language->getId());
            $this->setCreatedAndChangedDates($translation, TRUE);
        }
    }
}