You are here

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

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

Responds to entity GET requests.

Parameters

string $pool_id: The ID of the selected flow

Return value

\Symfony\Component\HttpFoundation\Response A list of entities of the given type and bundle

Throws

\Exception

File

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

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

public function get(string $pool_id) {
  $cache_build = [
    '#cache' => [
      'max-age' => 0,
    ],
  ];

  // Loaded directly from the Sync Core, so we just need to provide the status for them.
  if (!empty($_GET['entities'])) {
    $items = [];
    $types = explode(';', $_GET['entities']);
    foreach ($types as $type) {
      list($type, $ids) = explode(':', $type);
      $ids = explode(',', $ids);
      foreach ($ids as $id) {
        list(, , $entity_type_name, $bundle_name) = explode('-', $type);
        $items[] = array_merge([
          'entity_type_id' => $type,
          'id' => $id,
        ], $this
          ->getPreviewItemData($entity_type_name, $bundle_name, $id));
      }
    }
    $resource_response = new ResourceResponse([
      'items' => $items,
    ]);
    $resource_response
      ->addCacheableDependency($cache_build);
    return $resource_response;
  }
  $pool = Pool::getAll()[$pool_id];
  if (!$pool) {
    $resource_response = new ResourceResponse([
      'message' => "Unknown pool {$pool_id}.",
    ], self::CODE_NOT_FOUND);
    $resource_response
      ->addCacheableDependency($cache_build);
    return $resource_response;
  }
  $entity_type_ids = [];
  $entity_type_name = isset($_GET['entity_type_name']) ? $_GET['entity_type_name'] : null;
  $bundle_name = isset($_GET['bundle_name']) ? $_GET['bundle_name'] : null;
  $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  $sync_core_settings = $pool
    ->getClient()
    ->getSyndicationService()
    ->configurePullDashboard();
  if (!$sync_core_settings) {
    throw new \Exception('Invalid pull usage. Please refresh the page.');
  }

  /**
   * @var \Drupal\Core\Entity\EntityFieldManager $entityFieldManager
   */
  $entityFieldManager = \Drupal::service('entity_field.manager');
  foreach (Flow::getAll() as $flow) {
    foreach ($flow
      ->getEntityTypeConfig() as $definition) {
      if (!$flow
        ->canPullEntity($definition['entity_type_name'], $definition['bundle_name'], PullIntent::PULL_MANUALLY)) {
        continue;
      }
      if ($entity_type_name && $definition['entity_type_name'] != $entity_type_name) {
        continue;
      }
      if ($bundle_name && $definition['bundle_name'] != $bundle_name) {
        continue;
      }
      if (empty($definition['import_pools'][$pool->id]) || Pool::POOL_USAGE_ALLOW != $definition['import_pools'][$pool->id]) {
        continue;
      }
      if (isset($entity_type_ids[$pool_id][$definition['entity_type_name']][$definition['bundle_name']])) {
        continue;
      }
      if (EntityHandlerPluginManager::isEntityTypeFieldable($definition['entity_type_name'])) {

        /**
         * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
         */
        $fields = $entityFieldManager
          ->getFieldDefinitions($definition['entity_type_name'], $definition['bundle_name']);
        foreach ($fields as $key => $field) {
          $field_config = $flow
            ->getFieldHandlerConfig($definition['entity_type_name'], $definition['bundle_name'], $key);
          if (empty($field_config)) {
            continue;
          }
          if (empty($field_config['handler_settings']['subscribe_only_to'])) {
            continue;
          }
          $allowed = [];
          foreach ($field_config['handler_settings']['subscribe_only_to'] as $ref) {
            $allowed[] = $ref['uuid'];
          }
          $sync_core_settings
            ->ifTaggedWith($pool_id, $definition['entity_type_name'], $definition['bundle_name'], $key, $allowed);
        }
      }
      $sync_core_settings
        ->forEntityType($pool_id, $definition['entity_type_name'], $definition['bundle_name']);
      $entity_type_ids[$pool_id][$definition['entity_type_name']][$definition['bundle_name']] = true;
    }
  }
  if (empty($entity_type_ids)) {
    $resource_response = new ResourceResponse([
      'message' => 'No previews available.',
    ], self::CODE_NOT_FOUND);
    $resource_response
      ->addCacheableDependency($cache_build);
    return $resource_response;
  }
  if (!empty($_GET['filter_title'])) {
    $sync_core_settings
      ->searchInTitle($_GET['filter_title']);
  }
  if (!empty($_GET['filter_preview'])) {
    $sync_core_settings
      ->searchInPreview($_GET['filter_preview']);
  }
  $sync_core_settings
    ->publishedBetween(empty($_GET['filter_published_from']) ? null : (int) $_GET['filter_published_from'], empty($_GET['filter_published_to']) ? null : (int) $_GET['filter_published_to'] + 24 * 60 * 60);
  try {
    $order_by_title = isset($_GET['order_by']) && 'title' == $_GET['order_by'];
    $order_ascending = isset($_GET['order_direction']) && 'asc' == $_GET['order_direction'];
    $response = $sync_core_settings
      ->runSearch($order_by_title, $order_ascending, $page);
  } catch (SyncCoreException $e) {
    $body = $e
      ->getResponseBody();
    $code = $e
      ->getStatusCode();
    $resource_response = new ResourceResponse($body ? $body : json_encode([
      'message' => $e
        ->getMessage(),
    ]), $code ? $code : 500);
    $resource_response
      ->addCacheableDependency($cache_build);
    return $resource_response;
  }
  foreach ($response
    ->getItems() as $item) {
    $item
      ->extend($this
      ->getPreviewItemData($item
      ->getType(), $item
      ->getBundle(), $item
      ->getId()));
  }
  $resource_response = new ResourceResponse($response
    ->toArray());
  $resource_response
    ->addCacheableDependency($cache_build);
  return $resource_response;
}