You are here

protected function SchedulerManager::loadEntities in Scheduler 2.x

Helper method to load latest revision of each entity.

Parameters

array $ids: Array of entity ids.

string $type: The type of entity.

Return value

array Array of loaded entity objects, keyed by id.

2 calls to SchedulerManager::loadEntities()
SchedulerManager::publish in src/SchedulerManager.php
Publish scheduled entities.
SchedulerManager::unpublish in src/SchedulerManager.php
Unpublish scheduled entities.

File

src/SchedulerManager.php, line 837

Class

SchedulerManager
Defines a scheduler manager.

Namespace

Drupal\scheduler

Code

protected function loadEntities(array $ids, string $type) {
  $storage = $this->entityTypeManager
    ->getStorage($type);
  $entities = [];
  foreach ($ids as $id) {

    // Avoid errors when an implementation of hook_scheduler_{type}_list has
    // added an id of the wrong type.
    if (!($entity = $storage
      ->load($id))) {
      $this->logger
        ->notice('Entity id @id is not a @type entity. Processing skipped.', [
        '@id' => $id,
        '@type' => $type,
      ]);
      continue;
    }

    // If the entity type is revisionable then load the latest revision. For
    // moderated entities this may be an unpublished draft update of a
    // currently published entity.
    if ($entity
      ->getEntityType()
      ->isRevisionable()) {
      $vid = $storage
        ->getLatestRevisionId($id);
      $entities[$id] = $storage
        ->loadRevision($vid);
    }
    else {
      $entities[$id] = $entity;
    }
  }
  return $entities;
}