You are here

public function ImportEntityManager::importRemoteEntity in Acquia Content Hub 8

Saves a Content Hub Entity into a Drupal Entity, given its UUID.

This method accepts a parameter if we want to save all its dependencies. Note that dependencies could be of 2 different types:

  • pre-dependency or Entity Independent: Has to be created before the host-entity and referenced from it.
  • post-dependency or Entity Dependent: Has to be created after the host-entity and referenced from it.

This is a recursive method, and will also create dependencies of the dependencies.

Parameters

string $uuid: The UUID of the Entity to save.

bool $include_dependencies: TRUE if we want to save all its dependencies, FALSE otherwise.

string $author: The UUID of the author (user) that will own the entity.

int $status: The publishing status of the entity (Applies to nodes).

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON Response.

2 calls to ImportEntityManager::importRemoteEntity()
ImportEntityManager::entityUpdate in src/ImportEntityManager.php
Act on the entity's update action.
ImportEntityManager::import in src/ImportEntityManager.php
Import an entity.

File

src/ImportEntityManager.php, line 442

Class

ImportEntityManager
Provides a service for managing imported entities' actions.

Namespace

Drupal\acquia_contenthub

Code

public function importRemoteEntity($uuid, $include_dependencies = TRUE, $author = NULL, $status = 0) {

  // Checking that the parameter given is a UUID.
  if (!Uuid::isValid($uuid)) {

    // We will just show a standard "access denied" page in this case.
    throw new AccessDeniedHttpException();
  }

  // If the Entity is not found in Content Hub then return a 404 Not Found.
  $contenthub_entity = $this
    ->loadRemoteEntity($uuid);
  if (!$contenthub_entity) {
    $message = $this
      ->t('Entity with UUID = @uuid not found.', [
      '@uuid' => $uuid,
    ]);
    return $this
      ->jsonErrorResponseMessage($message, FALSE, 404);
  }
  $origin = $contenthub_entity
    ->getRawEntity()
    ->getOrigin();
  $site_origin = $this->contentHubEntitiesTracking
    ->getSiteOrigin();

  // Checking that the entity origin is different than this site's origin.
  if ($origin === $site_origin) {
    $args = [
      '@type' => $contenthub_entity
        ->getRawEntity()
        ->getType(),
      '@uuid' => $contenthub_entity
        ->getRawEntity()
        ->getUuid(),
      '@origin' => $origin,
    ];
    $message = $this
      ->t('Cannot save "@type" entity with uuid="@uuid". It has the same origin as this site: "@origin"', $args);
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->warning($message);
    $result = FALSE;
    return $this
      ->jsonErrorResponseMessage($message, $result, 403);
  }

  // Checking if bundle exists.
  $allowed_entity_types = $this->entityManager
    ->getAllowedEntityTypes();
  $contenthub_entity_attribute = $contenthub_entity
    ->getRawEntity()
    ->getAttribute('type')['value'] ?? NULL;
  $contenthub_entity_bundle = $contenthub_entity_attribute ? reset($contenthub_entity_attribute) : NULL;
  if ($contenthub_entity_bundle && !array_key_exists($contenthub_entity_bundle, $allowed_entity_types[$contenthub_entity
    ->getRawEntity()
    ->getType()])) {
    $args = [
      '@type' => $contenthub_entity
        ->getRawEntity()
        ->getType(),
      '@uuid' => $contenthub_entity
        ->getRawEntity()
        ->getUuid(),
      '@bundle' => $contenthub_entity_bundle,
    ];
    $message = $this
      ->t('Cannot save "@type" entity with uuid="@uuid". Missing "@type" entity with bundle "@bundle"', $args);
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->warning($message);
    $result = FALSE;
    return $this
      ->jsonErrorResponseMessage($message, $result, 403);
  }

  // Checking that the entity has a language that is supported by this site.
  if (!$this
    ->verifyLanguageSupportability($contenthub_entity)) {
    $args = [
      '@type' => $contenthub_entity
        ->getRawEntity()
        ->getType(),
      '@uuid' => $contenthub_entity
        ->getRawEntity()
        ->getUuid(),
    ];
    $message = $this
      ->t('Cannot save "@type" entity with uuid="@uuid". The site does not support any of the languages available for this entity.', $args);
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->warning($message);
    return $this
      ->jsonErrorResponseMessage($message, FALSE, 403);
  }

  // Collect and flat out all dependencies.
  $dependencies = [];
  if ($include_dependencies) {
    $dependencies = $this
      ->getAllRemoteDependencies($contenthub_entity, $dependencies, TRUE);
  }

  // Obtaining the Status of the parent entity, if it is a node and
  // setting the publishing status of that entity.
  $contenthub_entity
    ->setStatus($status);

  // Assigning author to this entity and dependencies.
  $contenthub_entity
    ->setAuthor($author);
  foreach ($dependencies as $uuid => $dependency) {
    $dependencies[$uuid]
      ->setAuthor($author);

    // Only change the Node status of dependent entities if they are nodes,
    // if the status flag is set and if they haven't been imported before.
    $entity_type = $dependency
      ->getEntityType();
    if (isset($status) && $entity_type === 'node' && !$this->contentHubEntitiesTracking
      ->loadImportedByUuid($uuid)) {
      $dependencies[$uuid]
        ->setStatus($status);
    }
  }

  // Save this entity and all its dependencies.
  return $this
    ->importRemoteEntityDependencies($contenthub_entity, $dependencies);
}