You are here

public function FlowPush::push 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::push()
  2. 2.0.x src/Controller/FlowPush.php \Drupal\cms_content_sync\Controller\FlowPush::push()

Push all entities of the flow.

Parameters

string $cms_content_sync_flow:

string $push_mode:

1 string reference to 'FlowPush::push'
cms_content_sync.routing.yml in ./cms_content_sync.routing.yml
cms_content_sync.routing.yml

File

src/Controller/FlowPush.php, line 30

Class

FlowPush
Pull controller.

Namespace

Drupal\cms_content_sync\Controller

Code

public function push($cms_content_sync_flow, $push_mode) {

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

  /**
   * @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $operations = [];
  foreach ($flow
    ->getEntityTypeConfig(null, null, true) as $config) {
    if ('automatic_manual' == $push_mode || 'automatic_manual_force' == $push_mode) {
      if (PushIntent::PUSH_AUTOMATICALLY != $config['export'] && PushIntent::PUSH_MANUALLY != $config['export']) {
        continue;
      }
    }
    else {
      if (PushIntent::PUSH_AUTOMATICALLY != $config['export']) {
        continue;
      }
    }
    $storage = $entity_type_manager
      ->getStorage($config['entity_type_name']);
    $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'), $config['bundle_name']);
    }
    $query = $query
      ->count();
    $count = $query
      ->execute();
    if (!$count) {
      continue;
    }

    // @todo A better way would be to have one batch operation that does all
    //   of that and then just dynamically updates the progress.
    //   {@see FlowPull} for an example.
    $pages = ceil($count / self::PREPARATION_BATCH_SIZE);
    for ($i = 0; $i < $pages; ++$i) {
      $operations[] = [
        '\\Drupal\\cms_content_sync\\Controller\\FlowPush::batch',
        [
          $cms_content_sync_flow,
          $config['entity_type_name'],
          $config['bundle_name'],
          $push_mode,
          $i,
          $count,
          false,
        ],
      ];
    }
  }
  $operations[count($operations) - 1][1][6] = true;
  $batch = [
    'title' => t('Push items...'),
    'operations' => $operations,
    'finished' => '\\Drupal\\cms_content_sync\\Controller\\FlowPush::batchFinished',
  ];
  batch_set($batch);
  return batch_process();
}