batch_set example

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);
  }

  /** * {@inheritdoc} */
  public function getConfirmText() {
    return $this->t('Perform upgrade');
  }

  
$post_updates = $this->postUpdateRegistry->getPendingUpdateFunctions();

    if ($post_updates) {
      // Now we rebuild all caches and after that execute the hook_post_update()       // functions.       $batch_builder->addOperation('drupal_flush_all_caches', []);
      foreach ($post_updates as $function) {
        $batch_builder->addOperation('update_invoke_post_update', [$function]);
      }
    }

    batch_set($batch_builder->toArray());

    // @todo Revisit once https://www.drupal.org/node/2548095 is in.     return batch_process(Url::fromUri('base://results'), Url::fromUri('base://start'));
  }

  /** * Finishes the update process and stores the results for eventual display. * * After the updates run, all caches are flushed. The update results are * stored into the session (for example, to be displayed on the update results * page in update.php). Additionally, if the site was off-line, now that the * update process is completed, the site is set back online. * * @param $success * Indicate that the batch API tasks were all completed successfully. * @param array $results * An array of all the results that were updated in update_do_one(). * @param array $operations * A list of all the operations that had not been completed by the batch API. */
/** * Form submission handler #1 for batch_test_chained_form. */
  public static function batchTestChainedFormSubmit1($form, FormStateInterface $form_state) {
    batch_test_stack(NULL, TRUE);

    batch_test_stack('submit handler 1');
    batch_test_stack('value = ' . $form_state->getValue('value'));

    $value = &$form_state->getValue('value');
    $value++;
    batch_set(_batch_test_batch_1());

    $form_state->setRedirect('batch_test.redirect');
  }

  /** * Form submission handler #2 for batch_test_chained_form. */
  public static function batchTestChainedFormSubmit2($form, FormStateInterface $form_state) {
    batch_test_stack('submit handler 2');
    batch_test_stack('value = ' . $form_state->getValue('value'));

    


  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    batch_test_stack(NULL, TRUE);

    $step = $form_state->get('step');
    switch ($step) {
      case 1:
        batch_set(_batch_test_batch_1());
        break;

      case 2:
        batch_set(_batch_test_batch_2());
        break;
    }

    if ($step < 2) {
      $form_state->set('step', ++$step);
      $form_state->setRebuild();
    }

    
$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());
      }
      catch (ConfigImporterException $e) {
        // There are validation errors.         $this->messenger()->addError($this->t('The configuration cannot be imported because it failed validation for the following reasons:'));
        foreach ($config_importer->getErrors() as $message) {
          $this->messenger()->addError($message);
        }
      }
    }
  }

}


  /** * 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. */
  public function testLargePercentage() {
    batch_test_stack(NULL, TRUE);

    batch_set(_batch_test_batch_5());
    return batch_process('batch-test/redirect');
  }

  /** * 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. */
$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());
  }

}
'entity_id' => $entity->id(),
          ],
        ],
      ];
    }

    if ($operations) {
      $batch = [
        'operations' => $operations,
        'finished' => [static::class, 'finishBatch'],
      ];
      batch_set($batch);
    }
  }

  /** * {@inheritdoc} */
  public function execute(ContentEntityInterface $entity = NULL) {
    $this->executeMultiple([$entity]);
  }

  /** * {@inheritdoc} */
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) {
        // There are validation errors.         $this->messenger()->addError($this->t('The configuration import failed for the following reasons:'));
        foreach ($config_importer->getErrors() as $message) {
          $this->messenger()->addError($message);
        }
      }
    }
  }

}
$language->save();
      $this->messenger()->addStatus($this->t('The language %language has been created.', ['%language' => $this->t($language->label())]));
    }
    $options = array_merge(_locale_translation_default_update_options()[
      'langcode' => $form_state->getValue('langcode'),
      'overwrite_options' => $form_state->getValue('overwrite_options'),
      'customized' => $form_state->getValue('customized') ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED,
    ]);
    $this->moduleHandler->loadInclude('locale', 'bulk.inc');
    $file = locale_translate_file_attach_properties($this->file, $options);
    $batch = locale_translate_batch_build([$file->uri => $file]$options);
    batch_set($batch);

    // Create or update all configuration translations for this language.     if ($batch = locale_config_batch_update_components($options[$form_state->getValue('langcode')])) {
      batch_set($batch);
    }

    $form_state->setRedirect('locale.translate_page');
  }

}
/** * 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');
  }

}
// Set the translation import options. This determines if existing     // translations will be overwritten by imported strings.     $options = _locale_translation_default_update_options();

    // If the status was updated recently we can immediately start fetching the     // translation updates. If the status is expired we clear it and run a batch     // to update the status and then fetch the translation updates.     $last_checked = $this->state->get('locale.translation_last_checked');
    if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
      locale_translation_clear_status();
      $batch = locale_translation_batch_update_build([]$langcodes$options);
      batch_set($batch);
    }
    else {
      // Set a batch to download and import translations.       $batch = locale_translation_batch_fetch_build($projects$langcodes$options);
      batch_set($batch);
      // Set a batch to update configuration as well.       if ($batch = locale_config_batch_update_components($options$langcodes)) {
        batch_set($batch);
      }
    }
  }

}
return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    batch_test_stack(NULL, TRUE);

    foreach ($form_state->getValue('batch') as $batch) {
      $function = '_batch_test_' . $batch;
      batch_set($function());
    }

    $form_state->setRedirect('batch_test.redirect');
  }

}

  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. */
Home | Imprint | This part of the site doesn't use cookies.