You are here

public function EntityOperations::entityStorageLoad in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/EntityOperations.php \Drupal\workbench_moderation\EntityOperations::entityStorageLoad()

Hook bridge.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entity objects that have just been loaded.

string $entity_type_id: The type of entity being loaded, such as "node" or "user".

See also

hook_entity_storage_load()

File

src/EntityOperations.php, line 99

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workbench_moderation

Code

public function entityStorageLoad(array $entities, $entity_type_id) {

  // Ensure that all moderatable entities always have a moderation_state field
  // with data, in all translations. That avoids us needing to have a thousand
  // NULL checks elsewhere in the code.
  // Quickly exclude any non-moderatable entities.
  $to_check = array_filter($entities, [
    $this->moderationInfo,
    'isModeratableEntity',
  ]);
  if (!$to_check) {
    return;
  }

  // @todo make this more functional, less iterative.
  foreach ($to_check as $entity) {
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $translation = $entity
        ->getTranslation($language
        ->getId());
      if ($translation->moderation_state->target_id == NULL) {
        $translation->moderation_state->target_id = $this
          ->getDefaultLoadStateId($translation);
      }
    }
  }
}