You are here

protected function PullEntity::getPreviewItemData in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/rest/resource/PullEntity.php \Drupal\cms_content_sync\Plugin\rest\resource\PullEntity::getPreviewItemData()
  2. 2.0.x src/Plugin/rest/resource/PullEntity.php \Drupal\cms_content_sync\Plugin\rest\resource\PullEntity::getPreviewItemData()

Return value

array

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to PullEntity::getPreviewItemData()
PullEntity::get in src/Plugin/rest/resource/PullEntity.php
Responds to entity GET requests.
PullEntity::post in src/Plugin/rest/resource/PullEntity.php
Responds to entity POST requests.

File

src/Plugin/rest/resource/PullEntity.php, line 402

Class

PullEntity
Provides entity interfaces for Content Sync, allowing the manual pull dashboard to query preview entities and to pull them to the site.

Namespace

Drupal\cms_content_sync\Plugin\rest\resource

Code

protected function getPreviewItemData(string $entity_type_name, string $bundle_name, string $shared_entity_id) {
  $data = [];
  $data['entity_type_name'] = $entity_type_name;
  $data['bundle_name'] = $bundle_name;
  $data['entity_status'] = [];
  $data['last_import'] = null;
  $data['last_export'] = null;
  $data['deleted'] = false;
  $data['is_source'] = false;

  /**
   * @var \Drupal\Core\Entity\EntityInterface $entity
   */
  if (EntityHandlerPluginManager::isEntityTypeConfiguration($entity_type_name)) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type_name)
      ->load($shared_entity_id);
    $entity_uuid = null;
  }
  else {
    $entity = \Drupal::service('entity.repository')
      ->loadEntityByUuid($entity_type_name, $shared_entity_id);
    $entity_uuid = $shared_entity_id;
  }
  if ($entity) {
    if ($entity
      ->hasLinkTemplate('canonical')) {
      try {
        $url = $entity
          ->toUrl('canonical', [
          'absolute' => true,
        ])
          ->toString(true)
          ->getGeneratedUrl();
        $data['local_url'] = $url;
      } catch (\Exception $e) {
      }
    }
    $entity_uuid = $entity
      ->uuid();
  }
  if (!$entity_uuid) {
    return $data;
  }
  $entity_status = EntityStatus::getInfosForEntity($entity_type_name, $entity_uuid);
  foreach ($entity_status as $info) {
    $data['entity_status'][] = [
      'flow_id' => $info
        ->get('flow')->value,
      'pool_id' => $info
        ->get('pool')->value,
      'last_import' => $info
        ->getLastPull(),
      'last_export' => $info
        ->getLastPush(),
    ];
    if (!$data['last_import'] || $data['last_import'] < $info
      ->getLastPull()) {
      $data['last_import'] = $info
        ->getLastPull();
    }
    if (!$data['last_export'] || $data['last_export'] < $info
      ->getLastPush()) {
      $data['last_export'] = $info
        ->getLastPush();
    }
    if ($info
      ->isDeleted()) {
      $data['deleted'] = true;
    }
    if ($info
      ->isSourceEntity()) {
      $data['is_source'] = true;
    }
  }

  // We had a bug before where the "deleted" flag wasn't set correctly locally. So we're mitigating for that here.
  if ($data['last_import'] && !$entity) {
    $data['deleted'] = true;
  }
  return $data;
}