You are here

protected function EmbeddedUpdateRunner::getEmbeddedUpdates in Scheduled Updates 8

Return all schedule updates that are referenced via Entity Reference fields.

Return value

ScheduledUpdate[]

1 call to EmbeddedUpdateRunner::getEmbeddedUpdates()
EmbeddedUpdateRunner::getAllUpdates in src/Plugin/UpdateRunner/EmbeddedUpdateRunner.php
Get all schedule updates for this types that should be added to queue.

File

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

Class

EmbeddedUpdateRunner
The default Embedded Update Runner.

Namespace

Drupal\scheduled_updates\Plugin\UpdateRunner

Code

protected function getEmbeddedUpdates() {
  $updates = [];

  /** @var String[] $fields */
  if ($entity_ids = $this
    ->getEntityIdsReferencingReadyUpdates()) {
    if ($entities = $this
      ->loadEntitiesToUpdate($entity_ids)) {
      $field_ids = $this
        ->getReferencingFieldIds();

      /** @var ContentEntityInterface $entity */
      foreach ($entities as $entity) {

        /** @var  $entity_update_ids - all update ids for this entity for our fields. */
        $entity_update_ids = [];

        /** @var  $field_update_ids - update ids keyed by field_id. */
        $field_update_ids = [];
        foreach ($field_ids as $field_id) {

          // Store with field id.
          $field_update_ids[$field_id] = $this
            ->getEntityReferenceTargetIds($entity, $field_id);

          // Add to all for entity.
          $entity_update_ids += $field_update_ids[$field_id];
        }

        // For all entity updates return only those ready to run.
        $ready_update_ids = $this
          ->getReadyUpdateIds($entity_update_ids);

        // Loop through updates attached to fields.
        foreach ($field_update_ids as $field_id => $update_ids) {

          // For updates attached to field get only those ready to run.
          $field_ready_update_ids = array_intersect($update_ids, $ready_update_ids);
          foreach ($field_ready_update_ids as $field_ready_update_id) {
            $updates[] = [
              'update_id' => $field_ready_update_id,
              // If this is revisionable entity use revision id as key for Runner Plugins that care about revisions.
              'entity_ids' => $entity
                ->getRevisionId() ? [
                $entity
                  ->getRevisionId() => $entity
                  ->id(),
              ] : [
                $entity
                  ->id(),
              ],
              'field_id' => $field_id,
              'entity_type' => $this
                ->updateEntityType(),
            ];
          }
        }
      }
    }
  }
  return $updates;
}