You are here

protected function SyncCoreEntityListResource::getItem in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 2.0.x src/Plugin/rest/resource/SyncCoreEntityListResource.php \Drupal\cms_content_sync\Plugin\rest\resource\SyncCoreEntityListResource::getItem()
1 call to SyncCoreEntityListResource::getItem()
SyncCoreEntityListResource::get in src/Plugin/rest/resource/SyncCoreEntityListResource.php

File

src/Plugin/rest/resource/SyncCoreEntityListResource.php, line 313

Class

SyncCoreEntityListResource
Provides entity interfaces for Content Sync, allowing Sync Core v2 to list entities.

Namespace

Drupal\cms_content_sync\Plugin\rest\resource

Code

protected function getItem(?Flow $flow, ?object $entity, array $statuses) {
  $item = new RemoteEntitySummary();
  $pools = [];
  $last_push = null;
  foreach ($statuses as $status) {
    $pools[] = $status
      ->getPool()
      ->id();
    if (!$last_push || $status
      ->getLastPush() > $last_push
      ->getLastPush()) {
      $last_push = $status;
    }
  }
  $item
    ->setPoolMachineNames($pools);
  $item
    ->setIsSource(empty($statuses) || (bool) $last_push);
  if ($entity) {
    $item
      ->setEntityTypeNamespaceMachineName($entity
      ->getEntityTypeId());
    $item
      ->setEntityTypeMachineName($entity
      ->bundle());
    $item
      ->setEntityTypeVersion(Flow::getEntityTypeVersion($entity
      ->getEntityTypeId(), $entity
      ->bundle()));
    $item
      ->setRemoteUuid($entity
      ->uuid());
    if (EntityHandlerPluginManager::isEntityTypeConfiguration($entity
      ->getEntityTypeId())) {
      $item
        ->setRemoteUniqueId($entity
        ->id());
    }
    $item
      ->setLanguage($entity
      ->language()
      ->getId());
    $item
      ->setName($entity
      ->label());
    $item
      ->setIsDeleted(false);
    if ($flow) {
      $config = $flow
        ->getController()
        ->getEntityTypeConfig($entity
        ->getEntityTypeId(), $entity
        ->bundle());
      $handler = $flow
        ->getController()
        ->getEntityTypeHandler($entity
        ->getEntityTypeId(), $entity
        ->bundle(), $config);
    }
    else {
      $entity_plugin_manager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
      $entity_handlers = $entity_plugin_manager
        ->getHandlerOptions($entity
        ->getEntityTypeId(), $entity
        ->bundle(), true);
      $entity_handler_names = array_keys($entity_handlers);
      $handler_id = reset($entity_handler_names);
      $handler = $entity_plugin_manager
        ->createInstance($handler_id, [
        'entity_type_name' => $entity
          ->getEntityTypeId(),
        'bundle_name' => $entity
          ->bundle(),
        'settings' => [],
        'sync' => null,
      ]);
    }
    $item
      ->setViewUrl($handler
      ->getViewUrl($entity));

    //$item->setReferenceDetails();
  }
  else {
    $item
      ->setEntityTypeNamespaceMachineName($last_push
      ->getEntityTypeName());
    $item
      ->setEntityTypeVersion($last_push
      ->getEntityTypeVersion());
    $item
      ->setRemoteUuid($last_push
      ->getUuid());
    $item
      ->setIsDeleted($last_push
      ->isDeleted());
  }
  return $item;
}