1 node.entity.inc protected NodeStorageController::invokeHook($hook, EntityInterface $node)

Overrides EntityDatabaseStorageController::invokeHook().

Parameters

string $hook: One of 'presave', 'insert', 'update', 'predelete', or 'delete'.

Node $node: The entity object, always a Node object in this case.

Overrides EntityDatabaseStorageController::invokeHook

File

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

Class

NodeStorageController
Controller class for nodes.

Code

protected function invokeHook($hook, EntityInterface $node) {
  if ($hook == 'insert' || $hook == 'update') {
    node_invoke($node, $hook);
  }
  elseif ($hook == 'predelete') {
    // 'delete' is triggered in 'predelete' is here to preserve hook ordering
    // from Drupal 7.
    node_invoke($node, 'delete');
  }

  parent::invokeHook($hook, $node);

  if ($hook == 'presave') {
    if ($node->isNew() || !empty($node->revision)) {
      // When inserting either a new node or a new node revision, $node->log
      // must be set because {node_revision}.log is a text column and therefore
      // cannot have a default value. However, it might not be set at this
      // point (for example, if the user submitting a node form does not have
      // permission to create revisions), so we ensure that it is at least an
      // empty string in that case.
      // @todo: Make the {node_revision}.log column nullable so that we can
      // remove this check.
      if (!isset($node->log)) {
        $node->log = '';
      }
    }
    elseif (!isset($node->log) || $node->log === '') {
      // If we are updating an existing node without adding a new revision, we
      // need to make sure $node->log is unset whenever it is empty. As long as
      // $node->log is unset, backdrop_write_record() will not attempt to update
      // the existing database column when re-saving the revision; therefore,
      // this code allows us to avoid clobbering an existing log entry with an
      // empty one.
      unset($node->log);
    }

    // When saving a new node revision, unset any existing $node->vid so as to
    // ensure that a new revision will actually be created, then store the old
    // revision ID in a separate property for use by node hook implementations.
    if (!$node->isNew() && !empty($node->revision) && $node->vid) {
      $node->old_vid = $node->vid;
      $node->vid = NULL;
    }
  }
}