You are here

public function ConfigEntityRevisionsRevisionStorageHandler::resetDefaultRevision in Config Entity Revisions 8.2

Make default the most recently published or most recent revision.

This is needed because content_moderation has a concept of a default revision, which this module doesn't really care about, but which will cause problems if we attempt to delete a revision that's marked as the default.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $content_entity: The content (revisions) entity.

int $deleting: An optional revision ID that is about to be deleted.

Overrides ConfigEntityRevisionsRevisionStorageHandlerInterface::resetDefaultRevision

1 call to ConfigEntityRevisionsRevisionStorageHandler::resetDefaultRevision()
ConfigEntityRevisionsRevisionStorageHandler::createUpdateRevision in src/Entity/Handler/ConfigEntityRevisionsRevisionStorageHandler.php
Create revision when a new config entity version is saved.

File

src/Entity/Handler/ConfigEntityRevisionsRevisionStorageHandler.php, line 427

Class

ConfigEntityRevisionsRevisionStorageHandler
Class ConfigEntityRevisionsRevisionStorageHandler.

Namespace

Drupal\config_entity_revisions\Entity\Handler

Code

public function resetDefaultRevision(ContentEntityInterface $content_entity, $deleting = NULL) {
  $content_entity_id = $content_entity
    ->id();

  // Ensure our data is up to date after a save.
  $this->revisionStates = NULL;
  $this
    ->ensureRevisionStatesLoaded();
  $first_published = NULL;
  $first_revision = NULL;
  $remove_default = [];
  foreach ($this->revisionStates as $revision) {
    if (!$first_revision && $revision->revision != $deleting) {
      $first_revision = $revision;
    }
    if ($revision->published && !$first_published && $revision->revision != $deleting) {
      $first_published = $revision;
    }
    if ($revision->revision_default) {
      $remove_default[$revision->revision] = 1;
    }
  }
  $default_revision = $first_published ?: $first_revision;
  if ($default_revision) {
    unset($remove_default[$default_revision->revision]);
  }
  if (!empty($remove_default)) {
    $this->database
      ->update("config_entity_revisions_revision")
      ->condition('revision', array_keys($remove_default), 'IN')
      ->fields([
      'revision_default' => 0,
    ])
      ->execute();
  }
  if ($default_revision) {
    $this->database
      ->update("config_entity_revisions")
      ->condition('id', $content_entity_id)
      ->fields([
      'revision' => $default_revision->revision,
    ])
      ->execute();
    if ($this->database
      ->schema()
      ->tableExists('content_moderation_state_field_revision')) {
      $content_moderation_rev_ids = $this->database
        ->select('content_moderation_state_field_revision', 'c')
        ->condition('content_entity_type_id', 'config_entity_revisions')
        ->condition('content_entity_id', $content_entity_id)
        ->condition('content_entity_revision_id', $default_revision->revision)
        ->fields('c', [
        'id',
        'revision_id',
      ])
        ->execute()
        ->fetchAssoc();
      $this->database
        ->update('content_moderation_state')
        ->condition('id', $content_moderation_rev_ids['id'])
        ->fields([
        'revision_id' => $content_moderation_rev_ids['revision_id'],
      ])
        ->execute();
    }
  }
}