You are here

public function SyncCoreEntityItemResource::get in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 2.1.x src/Plugin/rest/resource/SyncCoreEntityItemResource.php \Drupal\cms_content_sync\Plugin\rest\resource\SyncCoreEntityItemResource::get()

File

src/Plugin/rest/resource/SyncCoreEntityItemResource.php, line 139

Class

SyncCoreEntityItemResource
Provides entity interfaces for Content Sync, allowing Sync Core v2 to request and manipulate entities.

Namespace

Drupal\cms_content_sync\Plugin\rest\resource

Code

public function get($flow_id, $entity_type, $entity_bundle, $shared_entity_id) {
  $flow = Flow::getAll()[$flow_id];
  if (empty($flow)) {
    $message = t("The flow @flow_id doesn't exist.", [
      '@flow_id' => $flow_id,
    ])
      ->render();
    \Drupal::logger('cms_content_sync')
      ->notice('@not GET @shared_entity_id: @message', [
      '@shared_entity_id' => $shared_entity_id,
      '@not' => 'NO',
      '@message' => $message,
    ]);
    return $this
      ->respondWith([
      'message' => $message,
    ], self::CODE_NOT_FOUND);
  }
  $infos = EntityStatus::getInfosForEntity($entity_type, $shared_entity_id, [
    'flow' => $flow_id,
  ]);
  foreach ($infos as $info) {
    if ($info
      ->isDeleted()) {
      return $this
        ->respondWith([
        'message' => 'This entity has been deleted.',
      ], self::CODE_NOT_FOUND);
    }
  }
  $entity = \Drupal::service('entity.repository')
    ->loadEntityByUuid($entity_type, $shared_entity_id);
  if (!$entity) {
    return $this
      ->respondWith([
      'message' => 'This entity does not exist.',
    ], self::CODE_NOT_FOUND);
  }
  $always_v2 = Migration::alwaysUseV2();
  if (!$always_v2) {
    Migration::useV2(true);
  }

  /**
   * @var PushIntent $intent
   */
  $intent = PushIntent::pushEntity($entity, PushIntent::PUSH_ANY, SyncIntent::ACTION_CREATE, $flow, null, true);
  if (!$intent) {
    return $this
      ->respondWith([
      'message' => 'This entity is not configured to be pushed.',
    ], self::CODE_NOT_FOUND);
  }

  /**
   * @var PushSingle $operation
   */
  $operation = $intent
    ->getOperation();
  $body = $operation
    ->getData();
  $intent
    ->afterPush(SyncIntent::ACTION_CREATE, $entity);
  if (!$always_v2) {
    Migration::useV2(false);
  }
  return $this
    ->respondWith(json_decode(json_encode($body), true), 200);
}