LatestRevisionUpdateRunner.php in Scheduled Updates 8
File
src/Plugin/UpdateRunner/LatestRevisionUpdateRunner.php
View source
<?php
namespace Drupal\scheduled_updates\Plugin\UpdateRunner;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\scheduled_updates\ScheduledUpdateTypeInterface;
class LatestRevisionUpdateRunner extends EmbeddedUpdateRunner {
protected function getEntityIdsReferencingReadyUpdates() {
$entity_ids = [];
if ($field_ids = $this
->getReferencingFieldIds()) {
$entity_storage = $this->entityTypeManager
->getStorage($this
->updateEntityType());
$all_ready_update_ids = $this
->getReadyUpdateIds();
if ($all_ready_update_ids) {
foreach ($field_ids as $field_id) {
$query = $entity_storage
->getQuery('AND');
$query
->condition("{$field_id}.target_id", $all_ready_update_ids, 'IN');
$query
->allRevisions();
$entity_ids += $query
->execute();
}
}
}
return $entity_ids;
}
protected function loadEntitiesToUpdate($entity_ids) {
$revision_ids = array_keys($entity_ids);
$entity_ids = array_unique($entity_ids);
$revisions = [];
foreach ($entity_ids as $entity_id) {
$latest_revision = $this->updateUtils
->getLatestRevision($this
->updateEntityType(), $entity_id);
if (in_array($latest_revision
->getRevisionId(), $revision_ids)) {
$revisions[$entity_id] = $latest_revision;
}
}
return $revisions;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$scheduled_update_type = $form_state
->get('scheduled_update_type');
if (!$this->updateUtils
->supportsRevisionUpdates($scheduled_update_type)) {
$form_state
->setError($form['update_entity_type'], $this
->t('The latest revision runner cannot be used with an entity type that does not support revisions.'));
}
}
public function getDescription() {
return $this
->t('Runs updates always against the latest revision of revisionable entities content.') . ' ' . t('This is useful for modules that allow forward revisioning such as Workbench Moderation.') . ' ' . t('This Update Runner can only be used with revisionable entity types.');
}
}