You are here

class ViewRevisionsRevisionStorage in Config Entity Revisions 8.2

Class ViewRevisionsRevisionStorage.

@package Drupal\view_revisions

Hierarchy

  • class \Drupal\view_revisions\ViewRevisionsRevisionStorage extends \Drupal\config_entity_revisions\ConfigEntityRevisionsRevisionStorageHandler

Expanded class hierarchy of ViewRevisionsRevisionStorage

File

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

Namespace

Drupal\view_revisions
View source
class ViewRevisionsRevisionStorage extends ConfigEntityRevisionsRevisionStorageHandler {

  /**
   * {@inheritdoc}
   */
  public function loadRevision($revision_id) {
    $this
      ->ensure_revision_states_loaded();
    $content_entity_record = array_pop(array_filter($this->revision_states, function ($item) use ($revision_id) {
      return $item->revision == $revision_id;
    }));
    $content_entity_record = $content_entity_record->configuration;
    if (!$content_entity_record) {
      return NULL;
    }
    $entity = \Drupal::getContainer()
      ->get('serializer')
      ->deserialize($content_entity_record, $this
      ->getEntityType()
      ->getClass(), 'json');

    // The result of serialising and then deserialising is not an exact
    // copy of the original. This causes problems downstream if we don't fix
    // a few attributes here.
    $entity
      ->set('settingsOriginal', $entity
      ->get('settings'));
    $entity
      ->set('enforceIsNew', FALSE);

    // Record the revision ID in the config entity so we can quickly and
    // easily access the revision record if needed (eg for edit form revision
    // message).
    $entity
      ->updateLoadedRevisionId($revision_id);
    return $entity;
  }

  /**
   * Implements Drupal\Core\Entity\EntityStorageInterface::save().
   *
   * @throws EntityMalformedException
   *   When attempting to save a configuration entity that has no ID.
   */
  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;
  }

}

Members