BatchBuilder example

/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
    $projects = [];
    foreach (['projects', 'disabled_projects'] as $type) {
      if (!$form_state->isValueEmpty($type)) {
        $projects = array_merge($projectsarray_keys(array_filter($form_state->getValue($type))));
      }
    }
    $batch_builder = (new BatchBuilder())
      ->setFile($this->moduleHandler->getModule('update')->getPath() . '/update.manager.inc')
      ->setTitle($this->t('Downloading updates'))
      ->setInitMessage($this->t('Preparing to download selected updates'))
      ->setFinishCallback('update_manager_download_batch_finished');
    foreach ($projects as $project) {
      $batch_builder->addOperation('update_manager_batch_project_get', [
        $project,
        $form_state->getValue(['project_downloads', $project]),
      ]);
    }
    batch_set($batch_builder->toArray());
  }
return;
    }

    /** @var \Drupal\Core\Config\ConfigImporter $config_importer */
    $config_importer = $form_state->get('config_importer');
    if ($config_importer->alreadyImporting()) {
      $this->messenger()->addError($this->t('Another request may be importing configuration already.'));
    }
    else {
      try {
        $sync_steps = $config_importer->initialize();
        $batch_builder = (new BatchBuilder())
          ->setTitle($this->t('Importing configuration'))
          ->setFinishCallback([ConfigImporterBatch::class, 'finish'])
          ->setInitMessage($this->t('Starting configuration import.'))
          ->setProgressMessage($this->t('Completed @current step of @total.'))
          ->setErrorMessage($this->t('Configuration import has encountered an error.'));
        foreach ($sync_steps as $sync_step) {
          $batch_builder->addOperation([ConfigImporterBatch::class, 'process'][$config_importer$sync_step]);
        }
        batch_set($batch_builder->toArray());
      }
      catch (ConfigImporterException $e) {
        
/** * Submits a form within a batch programmatically. * * @param int $value * Some value passed to a custom batch callback. * * @return \Symfony\Component\HttpFoundation\RedirectResponse|null * A redirect response if the batch is progressive. No return value otherwise. */
  public function testNestedDrupalFormSubmit($value = 1) {
    // Set the batch and process it.     $batch_builder = (new BatchBuilder())
      ->addOperation('_batch_test_nested_drupal_form_submit_callback', [$value]);
    batch_set($batch_builder->toArray());
    return batch_process('batch-test/redirect');
  }

  /** * Fires a batch process without a form submission. * * @return \Symfony\Component\HttpFoundation\RedirectResponse|null * A redirect response if the batch is progressive. No return value otherwise. */
  
$maintenance_mode = $this->state->get('system.maintenance_mode', FALSE);
    // Store the current maintenance mode status in the session so that it can     // be restored at the end of the batch.     $request->getSession()->set('maintenance_mode', $maintenance_mode);
    // During the update, always put the site into maintenance mode so that     // in-progress schema changes do not affect visiting users.     if (empty($maintenance_mode)) {
      $this->state->set('system.maintenance_mode', TRUE);
    }

    /** @var \Drupal\Core\Batch\BatchBuilder $batch_builder */
    $batch_builder = (new BatchBuilder())
      ->setTitle($this->t('Updating'))
      ->setInitMessage($this->t('Starting updates'))
      ->setErrorMessage($this->t('An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.'))
      ->setFinishCallback([DbUpdateController::class, 'batchFinished']);

    // Resolve any update dependencies to determine the actual updates that will     // be run and the order they will be run in.     $start = $this->getModuleUpdates();
    $updates = update_resolve_dependencies($start);

    // Store the dependencies for each update function in an array which the

class BatchBuilderTest extends UnitTestCase {

  /** * Tests the default values. * * @covers ::toArray */
  public function testDefaultValues() {
    $batch = (new BatchBuilder())->toArray();

    $this->assertIsArray($batch);
    $this->assertArrayHasKey('operations', $batch);
    $this->assertIsArray($batch['operations']);
    $this->assertEmpty($batch['operations'], 'Operations array is empty.');
    $this->assertEquals(new TranslatableMarkup('Processing')$batch['title']);
    $this->assertEquals(new TranslatableMarkup('Initializing.')$batch['init_message']);
    $this->assertEquals(new TranslatableMarkup('Completed @current of @total.')$batch['progress_message']);
    $this->assertEquals(new TranslatableMarkup('An error has occurred.')$batch['error_message']);
    $this->assertNull($batch['finished']);
    $this->assertNull($batch['file']);
    
$this->messenger()->addError($this->renderer->renderPlain($message));
      }
    }
    return $build;
  }

  /** * Manually checks the update status without the use of cron. */
  public function updateStatusManually() {
    $this->updateManager->refreshUpdateData();
    $batch_builder = (new BatchBuilder())
      ->setTitle(t('Checking available update data'))
      ->addOperation([$this->updateManager, 'fetchDataBatch'][])
      ->setProgressMessage(t('Trying to check available update data ...'))
      ->setErrorMessage(t('Error checking available update data.'))
      ->setFinishCallback('update_fetch_data_finished');
    batch_set($batch_builder->toArray());
    return batch_process('admin/reports/updates');
  }

}
$form['#attached']['library'][] = 'migrate_drupal_ui/base';

    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config['source_base_path'] = $this->store->get('source_base_path');
    $config['source_private_file_path'] = $this->store->get('source_private_file_path');
    $batch_builder = (new BatchBuilder())
      ->setTitle($this->t('Running upgrade'))
      ->setProgressMessage('')
      ->addOperation([
        MigrateUpgradeImportBatch::class,
        'run',
      ][array_keys($this->migrations)$config])
      ->setFinishCallback([MigrateUpgradeImportBatch::class, 'finished']);
    batch_set($batch_builder->toArray());
    $form_state->setRedirect('<front>');
    $this->store->set('step', 'overview');
    $this->state->set('migrate_drupal_ui.performed', REQUEST_TIME);
  }
return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity_type_id = $form_state->getValue('entity_type_id');

    $entity_type_plural = $this->entityTypeManager->getDefinition($entity_type_id)->getPluralLabel();
    $batch_builder = (new BatchBuilder())
      ->setTitle($this->t('Deleting @entity_type_plural', ['@entity_type_plural' => $entity_type_plural]))
      ->setProgressMessage('')
      ->setFinishCallback([__CLASS__, 'moduleBatchFinished'])
      ->addOperation([__CLASS__, 'deleteContentEntities'][$entity_type_id]);
    batch_set($batch_builder->toArray());
  }

  /** * Deletes the content entities of the specified entity type. * * @param string $entity_type_id * The entity type ID from which data will be deleted. * @param array|\ArrayAccess $context * The batch context array, passed by reference. * * @internal * This batch callback is only meant to be used by this form. */
$this->themeHandler,
      $this->getStringTranslation(),
      $this->moduleExtensionList,
      $this->themeExtensionList
    );
    if ($config_importer->alreadyImporting()) {
      $this->messenger()->addStatus($this->t('Another request may be synchronizing configuration already.'));
    }
    else {
      try {
        $sync_steps = $config_importer->initialize();
        $batch_builder = (new BatchBuilder())
          ->setTitle($this->t('Synchronizing configuration'))
          ->setFinishCallback([ConfigImporterBatch::class, 'finish'])
          ->setInitMessage($this->t('Starting configuration synchronization.'))
          ->setProgressMessage($this->t('Completed step @current of @total.'))
          ->setErrorMessage($this->t('Configuration synchronization has encountered an error.'));
        foreach ($sync_steps as $sync_step) {
          $batch_builder->addOperation([ConfigImporterBatch::class, 'process'][$config_importer$sync_step]);
        }

        batch_set($batch_builder->toArray());
      }
      
Home | Imprint | This part of the site doesn't use cookies.