You are here

public function ViewRevisionsRevisionStorage::save in Config Entity Revisions 8.2

Implements Drupal\Core\Entity\EntityStorageInterface::save().

Throws

EntityMalformedException When attempting to save a configuration entity that has no ID.

File

modules/view_revisions/src/ViewRevisionsRevisionStorage.php, line 55

Class

ViewRevisionsRevisionStorage
Class ViewRevisionsRevisionStorage.

Namespace

Drupal\view_revisions

Code

public function save(EntityInterface $config_entity) {

  // Configuration entity IDs are strings, and '0' is a valid ID.
  $id = $config_entity
    ->id();
  if ($id === NULL || $id === '') {
    throw new EntityMalformedException('The entity does not have an ID.');
  }

  // Check the configuration entity ID length.
  // @see \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH
  // @todo Consider moving this to a protected method on the parent class, and
  //   abstracting it for all entity types.
  if (strlen($config_entity
    ->get($this->idKey)) > self::MAX_ID_LENGTH) {
    throw new ConfigEntityIdLengthException("Configuration entity ID {$config_entity->get($this->idKey)} exceeds maximum allowed length of " . self::MAX_ID_LENGTH . " characters.");
  }
  $contentEntityStorage = $config_entity
    ->contentEntityStorage();
  $contentEntityStorage
    ->setConfigEntity($config_entity);
  $next_choice_id = $contentEntityStorage
    ->getLatestPublishedRevisionOrLatestId($config_entity
    ->getRevisionId());
  $new_moderation_state = $config_entity
    ->get('moderation_state');
  $new_moderation_state = $new_moderation_state ? $new_moderation_state[0]['value'] : 'published';

  // Only save the config entity if this is going to be the default revision
  // (ie the last published revision or the last revision if none are
  // published).
  // Ensure the content entity is always added/updated.
  $new_default = $new_moderation_state == 'published' || $config_entity
    ->getRevisionId() == $next_choice_id;
  if ($new_default) {
    $result = parent::save($config_entity);
  }
  else {

    // We need to save $latest's entity into the config entity if it has
    // changed.
    $next_choice_revision = $contentEntityStorage
      ->loadRevision($next_choice_id);
    $next_choice = $contentEntityStorage
      ->getConfigEntity($next_choice_revision);
    $result = parent::save($next_choice);
  }

  // And then also update the content entity for our latest change.
  $contentEntityStorage
    ->createUpdateRevision($config_entity);
  return $result;
}