class BatchExampleForm in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/batch_example/src/Form/BatchExampleForm.php \Drupal\batch_example\Form\BatchExampleForm
Form with examples on how to use cache.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\batch_example\Form\BatchExampleForm
Expanded class hierarchy of BatchExampleForm
1 string reference to 'BatchExampleForm'
- batch_example.routing.yml in batch_example/
batch_example.routing.yml - batch_example/batch_example.routing.yml
File
- batch_example/
src/ Form/ BatchExampleForm.php, line 11
Namespace
Drupal\batch_example\FormView source
class BatchExampleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'batch_example_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'markup',
'#markup' => $this
->t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
];
$form['batch'] = [
'#type' => 'select',
'#title' => 'Choose batch',
'#options' => [
'batch_1' => $this
->t('batch 1 - 1000 operations'),
'batch_2' => $this
->t('batch 2 - 20 operations.'),
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Go',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Gather our form value.
$value = $form_state
->getValues()['batch'];
// Set the batch, using convenience methods.
$batch = [];
switch ($value) {
case 'batch_1':
$batch = $this
->generateBatch1();
break;
case 'batch_2':
$batch = $this
->generateBatch2();
break;
}
batch_set($batch);
}
/**
* Generate Batch 1.
*
* Batch 1 will process one item at a time.
*
* This creates an operations array defining what batch 1 should do, including
* what it should do when it's finished. In this case, each operation is the
* same and by chance even has the same $nid to operate on, but we could have
* a mix of different types of operations in the operations array.
*/
public function generateBatch1() {
$num_operations = 1000;
$this
->messenger()
->addMessage($this
->t('Creating an array of @num operations', [
'@num' => $num_operations,
]));
$operations = [];
// Set up an operations array with 1000 elements, each doing function
// batch_example_op_1.
// Each operation in the operations array means at least one new HTTP
// request, running Drupal from scratch to accomplish the operation. If the
// operation returns with $context['finished'] != TRUE, then it will be
// called again.
// In this example, $context['finished'] is always TRUE.
for ($i = 0; $i < $num_operations; $i++) {
// Each operation is an array consisting of
// - The function to call.
// - An array of arguments to that function.
$operations[] = [
'batch_example_op_1',
[
$i + 1,
$this
->t('(Operation @operation)', [
'@operation' => $i,
]),
],
];
}
$batch = [
'title' => $this
->t('Creating an array of @num operations', [
'@num' => $num_operations,
]),
'operations' => $operations,
'finished' => 'batch_example_finished',
];
return $batch;
}
/**
* Generate Batch 2.
*
* Batch 2 will process five items at a time.
*
* This creates an operations array defining what batch 2 should do, including
* what it should do when it's finished. In this case, each operation is the
* same and by chance even has the same $nid to operate on, but we could have
* a mix of different types of operations in the operations array.
*/
public function generateBatch2() {
$num_operations = 20;
$operations = [];
// 20 operations, each one loads all nodes.
for ($i = 0; $i < $num_operations; $i++) {
$operations[] = [
'batch_example_op_2',
[
$this
->t('(Operation @operation)', [
'@operation' => $i,
]),
],
];
}
$batch = [
'operations' => $operations,
'finished' => 'batch_example_finished',
// @current, @remaining, @total, @percentage, @estimate and @elapsed.
// These placeholders are replaced with actual values in _batch_process(),
// using strtr() instead of t(). The values are determined based on the
// number of operations in the 'operations' array (above), NOT by the
// number of nodes that will be processed. In this example, there are 20
// operations, so @total will always be 20, even though there are multiple
// nodes per operation.
// Defaults to t('Completed @current of @total.').
'title' => $this
->t('Processing batch 2'),
'init_message' => $this
->t('Batch 2 is starting.'),
'progress_message' => $this
->t('Processed @current out of @total.'),
'error_message' => $this
->t('Batch 2 has encountered an error.'),
];
return $batch;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BatchExampleForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
BatchExampleForm:: |
public | function | Generate Batch 1. | |
BatchExampleForm:: |
public | function | Generate Batch 2. | |
BatchExampleForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
BatchExampleForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |