doPreSave example


  abstract protected function doDelete($entities);

  /** * {@inheritdoc} */
  public function save(EntityInterface $entity) {
    // Track if this entity is new.     $is_new = $entity->isNew();

    // Execute presave logic and invoke the related hooks.     $id = $this->doPreSave($entity);

    // Perform the save and reset the static cache for the changed entity.     $return = $this->doSave($id$entity);

    // Execute post save logic and invoke the related hooks.     $this->doPostSave($entity, !$is_new);

    return $return;
  }

  /** * Performs presave entity processing. * * @param \Drupal\Core\Entity\EntityInterface $entity * The saved entity. * * @return int|string|null * The processed entity identifier, or null for new entities. * * @throws \Drupal\Core\Entity\EntityStorageException * If the entity identifier is invalid. */
// Sync the changes made in the fields array to the internal values array.     $entity->updateOriginalValues();

    if ($entity->getEntityType()->isRevisionable() && !$entity->isNew() && empty($entity->getLoadedRevisionId())) {
      // Update the loaded revision id for rare special cases when no loaded       // revision is given when updating an existing entity. This for example       // happens when calling save() in hook_entity_insert().       $entity->updateLoadedRevisionId();
    }

    $id = parent::doPreSave($entity);

    if (!$entity->isNew()) {
      // If the ID changed then original can't be loaded, throw an exception       // in that case.       if (empty($entity->original) || $entity->id() != $entity->original->id()) {
        throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity while changing the ID is not supported.");
      }
      // Do not allow changing the revision ID when resaving the current       // revision.       if (!$entity->isNewRevision() && $entity->getRevisionId() != $entity->getLoadedRevisionId()) {
        throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity revision while changing the revision ID is not supported.");
      }
Home | Imprint | This part of the site doesn't use cookies.