You are here

public function EntityManager::isEligibleEntity in Acquia Content Hub 8

Checks whether the current entity should be transferred to Content Hub.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The Drupal entity.

Return value

bool True if it can be parsed, False if it not a suitable entity for sending to content hub.

3 calls to EntityManager::isEligibleEntity()
EntityManager::enqueueCandidateEntity in src/EntityManager.php
Enqueue an entity with an operation to be performed on Content Hub.
EntityManager::enqueueCandidateParagraph in src/EntityManager.php
Enqueue a paragraph with an operation to be performed on Content Hub.
EntityManager::isEligibleDependency in src/EntityManager.php
Checks whether the current dependency should be transferred to Content Hub.

File

src/EntityManager.php, line 514

Class

EntityManager
Provides a service for managing entity actions for Content Hub.

Namespace

Drupal\acquia_contenthub

Code

public function isEligibleEntity(EntityInterface $entity) {

  // Early return, if already sync'ing.
  if (!empty($entity->__contenthub_entity_syncing)) {
    return FALSE;
  }

  // Currently Content Hub does not support configuration entities to be
  // exported. Only content entities can be exported to Content Hub.
  if ($entity instanceof ConfigEntityInterface) {
    return FALSE;
  }

  // If access to the entity is not allowed, then it is not eligible.
  if ($entity instanceof FileInterface) {
    $account = new ContentHubUserSession(\Drupal::config('acquia_contenthub.entity_config')
      ->get('user_role'));
    $entity_access = $entity
      ->access('view', $account, TRUE);
    if (!$entity_access
      ->isAllowed()) {
      return FALSE;
    }
  }
  $entity_type_id = $entity
    ->getEntityTypeId();

  /** @var \Drupal\acquia_contenthub\ContentHubEntityTypeConfigInterface $entity_type_config */
  $entity_type_config = $this
    ->getContentHubEntityTypeConfigurationEntity($entity_type_id);
  $bundle_id = $entity
    ->bundle();
  if (empty($entity_type_config) || empty($entity_type_config
    ->isEnableIndex($bundle_id))) {
    return FALSE;
  }

  // If this is a file with status = 0 (TEMPORARY FILE) do not export it.
  // This is a check to avoid exporting temporary files.
  if ($entity instanceof FileInterface && $entity
    ->isTemporary()) {
    return FALSE;
  }
  $tracking_entity = $this->contentHubEntitiesTracking
    ->loadExportedByUuid($entity
    ->uuid());
  if ($tracking_entity) {
    $entity_origin = $tracking_entity
      ->getOrigin();
    $site_origin = $this->contentHubEntitiesTracking
      ->getSiteOrigin();
    if ($entity_origin !== $site_origin) {
      $this->loggerFactory
        ->get('acquia_contenthub')
        ->warning('Site origin id (@site) and origin id in the tracking table (@entity) does not match', [
        '@entity' => $entity_origin,
        '@site' => $site_origin,
      ]);
      return FALSE;
    }
  }

  // If the entity has been imported before, then it didn't originate from
  // this site and shouldn't be exported.
  $entity_id = $entity
    ->id();
  if ($this->contentHubEntitiesTracking
    ->loadImportedByDrupalEntity($entity_type_id, $entity_id) !== FALSE) {

    // Is this an entity that does not belong to this site? Has it been
    // previously imported from Content Hub?
    $uuid = $entity
      ->uuid();

    // We cannot bulk upload this entity because it does not belong to this
    // site. Add it to the pool of failed entities.
    if (isset($uuid)) {
      $this
        ->entityFailures(1);

      // We can use this pool of failed entities to display a message to the
      // user about the entities that failed to export.
      // $args = [
      // '@type' => $entity_type_id,
      // '@uuid' => $uuid,
      // ];
      // $message = new FormattableMarkup('Cannot export @type entity with
      // UUID = @uuid to Content Hub because it was previously imported
      // (did not originate from this site).', $args);
      // $this->loggerFactory->get('acquia_contenthub')->error($message);
    }
    return FALSE;
  }
  $status = $entity
    ->getEntityType()
    ->hasKey("status") ? $entity
    ->getEntityType()
    ->getKey("status") : NULL;
  $revision_col = $entity
    ->getEntityType()
    ->hasKey("revision") ? $entity
    ->getEntityType()
    ->getKey("revision") : NULL;
  if ($status && $revision_col && $entity instanceof RevisionableInterface) {
    $definition = $entity
      ->getFieldDefinition($status);
    $property = $definition
      ->getFieldStorageDefinition()
      ->getMainPropertyName();
    $value = $entity
      ->get($status)->{$property};
    if (!$value) {
      $table = $entity
        ->getEntityType()
        ->getBaseTable();
      $id_col = $entity
        ->getEntityType()
        ->getKey("id");
      $query = \Drupal::database()
        ->select($table)
        ->fields($table, [
        $revision_col,
      ]);
      $query
        ->condition("{$table}.{$id_col}", $entity
        ->id());
      $revision_id = $query
        ->execute()
        ->fetchField();
      if ($revision_id != $entity
        ->getRevisionId()) {
        return FALSE;
      }
    }
  }

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  $module_handler = \Drupal::getContainer()
    ->get("module_handler");
  $results = $module_handler
    ->invokeAll('acquia_contenthub_is_eligible_entity', [
    $entity,
  ]);
  foreach ($results as $result) {
    if ($result === FALSE) {
      return FALSE;
    }
  }
  return TRUE;
}