You are here

protected function LogStorage::doPostSave in Log entity 2.x

Performs post save entity processing.

Parameters

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

bool $update: Specifies whether the entity is being updated or created.

Overrides ContentEntityStorageBase::doPostSave

File

src/LogStorage.php, line 20

Class

LogStorage
Defines the controller class for logs.

Namespace

Drupal\log

Code

protected function doPostSave(EntityInterface $entity, $update) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  if ($update && $this->entityType
    ->isTranslatable()) {
    $this
      ->invokeTranslationHooks($entity);
  }

  // Get the log's current name.
  $current_name = $entity
    ->get('name')->value;

  // We will automatically set the log name under two conditions:
  // 1. Saving new/existing logs without a name.
  // 2. Updating existing logs that were saved using the naming pattern.
  $set_name = FALSE;
  if (empty($current_name)) {
    $set_name = TRUE;
  }
  elseif ($update && !empty($entity->original)) {

    // Generate a log name using the original entity.
    $original_generated_name = $this
      ->generateLogName($entity->original);

    // Compare the current log name to what would have been the original
    // auto-generated name, to determine if the name was auto-generated
    // previously. If it was, we will regenerate it.
    if ($current_name == $original_generated_name) {
      $set_name = TRUE;
    }
  }

  // We must run the parent method before we set the name, so that new logs
  // have an ID that can be used in token replacements.
  // Also, we must run the parent method after the logic above, because the
  // parent method unsets $entity->original.
  parent::doPostSave($entity, $update);

  // Set the log name, if necessary.
  if ($set_name) {

    // Generate a new name.
    $new_name = $this
      ->generateLogName($entity);

    // If the name has been changed, update the entity.
    if ($current_name != $new_name) {
      $entity
        ->set('name', $new_name);
      $entity
        ->save();
    }
  }
}