You are here

protected function DeployLogger::entityAlreadyExistsInDeployment in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x src/DeployLogger.php \Drupal\build_hooks\DeployLogger::entityAlreadyExistsInDeployment()

Checks if an entity is already part of a deployment.

Parameters

\Drupal\build_hooks\Entity\DeploymentInterface $deployment: Deployment.

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity.

Return value

bool TRUE if the entity already exists in the deployment.

1 call to DeployLogger::entityAlreadyExistsInDeployment()
DeployLogger::logEntityCreated in src/DeployLogger.php
Logs the creation of an entity.

File

src/DeployLogger.php, line 206

Class

DeployLogger
Class DeployLogger.

Namespace

Drupal\build_hooks

Code

protected function entityAlreadyExistsInDeployment(DeploymentInterface $deployment, ContentEntityInterface $entity) : bool {
  if ($deployment
    ->get('contents')
    ->isEmpty()) {
    return FALSE;
  }
  $existing_items = array_column(array_filter($deployment
    ->get('contents')
    ->getValue(), function (array $item) use ($entity) {
    return $item['target_type'] === $entity
      ->getEntityTypeId();
  }), 'target_id');
  $string_ids = array_map(function ($id) {
    return (string) $id;
  }, $existing_items);

  // Item IDs can be strings or integers, so we convert them all to string
  // so we can make a type-safe in-array comparison.
  if (in_array((string) $entity
    ->id(), $string_ids, TRUE)) {

    // This item is already logged.
    return TRUE;
  }
  return FALSE;
}