You are here

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

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

File

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

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)) {
    if (IApplicationInterface::FLOW_NONE === $flow_id) {
      $flow = new Flow([
        'id' => $flow_id,
        'name' => 'Virtual',
        'type' => Flow::TYPE_PUSH,
        'variant' => Flow::VARIANT_SIMPLE,
        'simple_settings' => [
          'poolAssignment' => 'force',
          'mode' => 'automatically',
          'deletions' => false,
          'updateBehavior' => 'ignore',
          'ignoreUnpublishedChanges' => false,
          'allowExplicitUnpublishing' => false,
          'pushMenuItems' => false,
          'pushPreviews' => false,
          'mergeLocalChanges' => false,
          'resolveUserReferences' => 'name',
          'poolSelectionWidget' => 'checkboxes',
          'entityTypeSettings' => [
            $entity_type => [
              'perBundle' => [
                $entity_bundle => [
                  'mode' => 'default',
                ],
              ],
            ],
          ],
        ],
      ], 'cms_content_sync_flow');
      $flow
        ->getController()
        ->isVirtual(true);
    }
    else {
      $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);
}