You are here

public static function FlowPush::batch in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Controller/FlowPush.php \Drupal\cms_content_sync\Controller\FlowPush::batch()
  2. 2.0.x src/Controller/FlowPush.php \Drupal\cms_content_sync\Controller\FlowPush::batch()

Batch push callback for the push-all operation.

Parameters

string $flow_id:

string $entity_type_id:

string $bundle_name:

string $push_mode:

int $page:

int $count:

$last:

$context:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Controller/FlowPush.php, line 123

Class

FlowPush
Pull controller.

Namespace

Drupal\cms_content_sync\Controller

Code

public static function batch($flow_id, $entity_type_id, $bundle_name, $push_mode, $page, $count, $last, &$context) {
  $message = 'Checking ' . $entity_type_id . '.' . $bundle_name . ' page ' . ($page + 1) . '/' . ceil($count / self::PREPARATION_BATCH_SIZE) . '...';
  $results = [];
  if (isset($context['results'])) {
    $results = $context['results'];
  }

  /**
   * @var \Drupal\cms_content_sync\Entity\Flow $flow
   */
  $flow = Flow::getAll()[$flow_id];

  /**
   * @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $operations = new PushEntities();
  $storage = $entity_type_manager
    ->getStorage($entity_type_id);
  $query = $storage
    ->getQuery();

  // Files don't have bundles, so this would lead to a fatal error then.
  if ($storage
    ->getEntityType()
    ->getKey('bundle')) {
    $query = $query
      ->condition($storage
      ->getEntityType()
      ->getKey('bundle'), $bundle_name);
  }
  $query = $query
    ->range($page * self::PREPARATION_BATCH_SIZE, self::PREPARATION_BATCH_SIZE);
  $ids = $query
    ->execute();
  foreach ($ids as $id) {
    $entity = $storage
      ->load($id);

    /**
     * @var \Drupal\cms_content_sync\Entity\EntityStatus[] $entity_status
     */
    $entity_status = EntityStatus::getInfosForEntity($entity
      ->getEntityTypeId(), $entity
      ->uuid(), [
      'flow' => $flow
        ->id(),
    ]);
    $is_manual = PushIntent::PUSH_MANUALLY == $flow
      ->getEntityTypeConfig($entity
      ->getEntityTypeId(), $entity
      ->bundle())['export'];

    // If this is manual AND the export doesn't say FORCE, we skip this entity if it wasn't exported before.
    if ('automatic_manual' == $push_mode && $is_manual && (empty($entity_status) || is_null($entity_status[0]
      ->getLastPush()))) {
      continue;
    }
    $operations
      ->add($flow_id, $entity_type_id, $id);
  }
  $context['message'] = $message;
  $context['results'] = array_merge($results, $operations
    ->get());

  // can't do this in the finished callback unfortunately as Drupal errs out with "No active batch" then or goes into
  // an infinite batch loop.
  if ($last) {
    $pusher = new PushEntities($context['results']);
    $pusher
      ->start();
  }
}