You are here

protected function EmbeddedUpdateRunner::deactivateUpdates in Scheduled Updates 8

Deactivate any Scheduled Updates that are previous revision but not on current.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity:

1 call to EmbeddedUpdateRunner::deactivateUpdates()
EmbeddedUpdateRunner::onEntityUpdate in src/Plugin/UpdateRunner/EmbeddedUpdateRunner.php
Fires when entity of type to be updated is changed.

File

src/Plugin/UpdateRunner/EmbeddedUpdateRunner.php, line 98
Contains \Drupal\scheduled_updates\Plugin\UpdateRunner\EmbeddedUpdateRunner.

Class

EmbeddedUpdateRunner
The default Embedded Update Runner.

Namespace

Drupal\scheduled_updates\Plugin\UpdateRunner

Code

protected function deactivateUpdates(ContentEntityInterface $entity) {
  $current_update_ids = $this
    ->getUpdateIdsOnEntity($entity);

  // Loop through all previous revisions and deactive updates not on current revision.
  $revisions = $this
    ->getPreviousRevisionsWithUpdates($entity);
  if (empty($revisions)) {
    return;
  }
  $all_revisions_update_ids = [];
  foreach ($revisions as $revision) {

    // array_merge so so elements with same key are not replaced.
    $all_revisions_update_ids = array_merge($all_revisions_update_ids, $this
      ->getUpdateIdsOnEntity($revision));
  }
  $all_revisions_update_ids = array_unique($all_revisions_update_ids);
  $updates_ids_not_on_current = array_diff($all_revisions_update_ids, $current_update_ids);
  if ($updates_ids_not_on_current) {
    $storage = $this->entityTypeManager
      ->getStorage('scheduled_update');
    foreach ($updates_ids_not_on_current as $update_id) {

      /** @var ScheduledUpdateInterface $update */
      $update = $storage
        ->load($update_id);
      $update->status = ScheduledUpdateInterface::STATUS_INACTIVE;
      $update
        ->save();
    }
  }
}