1.20.x node.entity.inc public NodeStorageController::save(EntityInterface $entity)

Overrides EntityDatabaseStorageController::save().

Parameters

Node $entity: The node entity being saved.

Overrides EntityDatabaseStorageController::save

File

modules/node/node.entity.inc, line 463
Entity controller and class for nodes.

Class

NodeStorageController
Controller class for nodes.

Code

public function save(EntityInterface $entity) {
  $transaction = db_transaction();
  try {
    // Load the stored entity, if any.
    if (!$entity->isNew() && !isset($entity->original)) {
      $entity->original = entity_load_unchanged($this->entityType, $entity->id());
    }

    $this->preSave($entity);
    $this->invokeHook('presave', $entity);

    if ($entity->isNew()) {
      $op = 'insert';
      $return = backdrop_write_record($this->entityInfo['base table'], $entity);
      unset($entity->is_new);
    }
    else {
      $op = 'update';
      // Update the base node table, but only if this revision is marked as
      // the active revision.
      if ($entity->isActiveRevision()) {
        $return = backdrop_write_record($this->entityInfo['base table'], $entity, $this->idKey);
      }
      else {
        $return = SAVED_UPDATED;
      }
    }

    if ($this->revisionKey) {
      $this->saveRevision($entity);
    }

    // Reset general caches, but keep caches specific to certain entities.
    $this->resetCache($op == 'update' ? array($entity->{$this->idKey}) : array());

    $this->postSave($entity, $op == 'update');
    $this->invokeHook($op, $entity);

    // Ignore slave server temporarily.
    db_ignore_slave();
    unset($entity->original);

    return $return;
  }
  catch (Exception $e) {
    $transaction->rollback();
    watchdog_exception($this->entityType, $e);
    throw new EntityStorageException($e->getMessage(), (int) $e->getCode(), $e);
  }
}