You are here

public function ConfigEntityRevisionsControllerBase::resetDefaultRevision in Config Entity Revisions 8

Same name and namespace in other branches
  1. 1.x src/ConfigEntityRevisionsControllerBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsControllerBase::resetDefaultRevision()

Make default the most recently published revision or the 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

ContentEntityInterface $content_entity: The content (revisions) entity.

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

File

src/ConfigEntityRevisionsControllerBase.php, line 284

Class

ConfigEntityRevisionsControllerBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function resetDefaultRevision(ContentEntityInterface $content_entity) {
  $content_entity_id = $content_entity
    ->id();
  $revisions = $this->connection
    ->select("config_entity_revisions_revision", 'c')
    ->fields('c', [
    'revision',
    'revision_default',
    'published',
  ])
    ->condition('id', $content_entity_id)
    ->orderBy('revision', 'DESC')
    ->execute()
    ->fetchAllAssoc('revision');
  $first_published = NULL;
  $first_revision = NULL;
  $remove_default = [];
  foreach ($revisions as $revision) {
    if (!$first_revision) {
      $first_revision = $revision;
    }
    if ($revision->published && !$first_published) {
      $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->connection
      ->update("config_entity_revisions_revision")
      ->condition('revision', array_keys($remove_default), 'IN')
      ->fields([
      'revision_default' => 0,
    ])
      ->execute();
  }
  if ($default_revision) {
    if (!$default_revision->revision_default) {
      $this->connection
        ->update("config_entity_revisions_revision")
        ->condition('revision', $default_revision->revision)
        ->fields([
        'revision_default' => 1,
      ])
        ->execute();
    }
    $this->connection
      ->update("config_entity_revisions")
      ->condition('id', $content_entity_id)
      ->fields([
      'revision' => $default_revision->revision,
    ])
      ->execute();
  }
}