You are here

public function ConfigEntityRevisionsConfigEntityTrait::save in Config Entity Revisions 8.2

Save an updated version of the entity.

In this case, we only save the entity if this new revision is/will be the default revision. In other cases, we're just updating the content revision.

File

src/ConfigEntityRevisionsConfigEntityTrait.php, line 469

Class

ConfigEntityRevisionsConfigEntityTrait
Trait ConfigEntityRevisionsConfigEntityTrait.

Namespace

Drupal\config_entity_revisions

Code

public function save() {

  // Configuration entity IDs are strings, and '0' is a valid ID.
  $id = $this
    ->id();
  if ($id === NULL || $id === '') {
    throw new EntityMalformedException('The entity does not have an ID.');
  }
  $contentEntityStorage = $this
    ->contentEntityStorage();
  $contentEntityStorage
    ->setConfigEntity($this);
  $next_choice_id = $contentEntityStorage
    ->getLatestPublishedRevisionOrLatestId($this
    ->getRevisionId());
  $new_moderation_state = $this
    ->get('moderation_state');
  $new_moderation_state = $new_moderation_state ? $new_moderation_state->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' || $this
    ->getRevisionId() == $next_choice_id;
  if ($new_default) {
    $result = parent::save($this);
  }
  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 = $next_choice
      ->save();
  }

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