Skip to content

Commit c033191

Browse files
Updated the help and documenation for the project. Tweaked the description and weight of all menu items to have a logical order.
1 parent d6cd619 commit c033191

File tree

13 files changed

+88
-11
lines changed

13 files changed

+88
-11
lines changed

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
# Drupal Batch Examples
2+
3+
Compatible with Drupal 10 and 11.
4+
5+
This codebase contains a number of modules that show how to use the batch API
6+
in different ways. The modules have been split into different parts to show
7+
different parts of the batch API in action.
8+
9+
Every module has a form that can be submitted to initiate the batch process.
10+
11+
All modules have unit tests to test that the batch has run correctly.
12+
13+
## Drupal Batch Examples
14+
15+
A meta-module to provide a consistent navigation system for the other examples.
16+
17+
Path: /drupal-batch-examples
18+
19+
## Batch Form Example
20+
21+
An example of running a batch in a self contained form.
22+
23+
Path: `/drupal-batch-examples/batch-form-example`
24+
25+
## Batch Class Example
26+
27+
An example of running batch via a class. Includes Drush support.
28+
29+
Path: `/drupal-batch-examples/batch-class-example`
30+
31+
This module also shows how to use the batch API in Drush. The same batch process
32+
is run via the form and via the Drush command `drush batch_class_example:run`.
33+
34+
## Batch Finish Example
35+
36+
An example of using the finish property of the batch API.
37+
38+
Path: `/drupal-batch-examples/batch-finish-example`
39+
40+
## Batch Addition Example
41+
42+
An example of adding a batch inside a running batch.
43+
44+
Path: `/drupal-batch-examples/batch-addition-example`
45+
46+
## Batch CSV Example
47+
48+
An example of using the batch API to process a CSV file.
49+
50+
This batch process will create nodes of the type "article", which comes with the
51+
standard install profile. In this case we are taking a two column CSV and using
52+
the first column for the title and the second column for the body content.
53+
54+
Path: `/drupal-batch-examples/batch-csv-example`
55+
56+
You can download the CSV needed for this form at the path
57+
`/drupal-batch-examples/batch-csv-example/generate-csv`, which is also linked to
58+
from the batch form itself. The CSV is generated from random data and so will
59+
be different every time you download it.

drupal_batch_examples.links.menu.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
drupal_batch_examples:
22
title: 'Drupal Batch Examples'
33
parent: system.admin
4-
description: 'Drupal batch examples.'
4+
description: 'Examples of using the batch API in different ways.'
55
route_name: drupal_batch_examples
66
weight: 1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
batch_addition_example:
22
title: 'Batch Addition Example'
33
parent: drupal_batch_examples
4-
description: 'Batch addition example.'
4+
description: 'An example of adding to an batch run whilst a batch run is being processed.'
55
route_name: batch_addition_example
6-
weight: 2
6+
weight: 4

modules/batch_addition_example/src/Form/BatchForm.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function getFormId() {
2424
* {@inheritdoc}
2525
*/
2626
public function buildForm(array $form, FormStateInterface $form_state) {
27+
$form['help'] = [
28+
'#markup' => $this->t('This form will initiate a batch run that will add a number of additional batch runs during the first pass. Once the first batch has completed the secondary batches will be processed.'),
29+
];
30+
2731
$form['actions'] = [
2832
'#type' => 'actions',
2933
'submit' => [
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
batch_class_example:
22
title: 'Batch Class Example'
33
parent: drupal_batch_examples
4-
description: 'Batch class example.'
4+
description: 'An example of splitting the batch out of a form and into a class.'
55
route_name: batch_class_example
66
weight: 2

modules/batch_class_example/src/Form/BatchForm.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function getFormId() {
2424
* {@inheritdoc}
2525
*/
2626
public function buildForm(array $form, FormStateInterface $form_state) {
27+
$form['help'] = [
28+
'#markup' => $this->t('Submit this form to run a batch operation held in another class. This operation will run through 1000 items in chunks of 100.'),
29+
];
30+
2731
$form['actions'] = [
2832
'#type' => 'actions',
2933
'submit' => [
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
batch_csv_example:
22
title: 'Batch CSV Example'
33
parent: drupal_batch_examples
4-
description: 'Process a CSV using the batch API.'
4+
description: 'This form will take a CSV file and process it in a batch to create content.'
55
route_name: batch_csv_example
6-
weight: 2
6+
weight: 5

modules/batch_csv_example/src/Form/BatchForm.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function getFormId() {
2525
* {@inheritdoc}
2626
*/
2727
public function buildForm(array $form, FormStateInterface $form_state) {
28+
$form['help'] = [
29+
'#markup' => $this->t('This form will run a batch operation that will import a CSV to create Article content items. The CSV is processed in the batch operation so the size of the file does not matter.'),
30+
];
31+
2832
$form['csv_file'] = [
2933
'#type' => 'file',
3034
'#title' => $this->t('The CSV file to process'),
@@ -33,7 +37,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
3337

3438
$form['csv_file_generate'] = [
3539
'#type' => 'link',
36-
'#title' => $this->t('Generate a CSV file'),
40+
'#title' => $this->t('Generate a CSV file containing 500 items.'),
3741
'#url' => Url::fromRoute('batch_csv_example.generate'),
3842
];
3943

modules/batch_csv_example/tests/Functional/BatchFormTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Drupal\Tests\batch_csv_example\Functional;
44

5-
use Drupal\Core\File\FileExists;
65
use Drupal\Tests\BrowserTestBase;
76

87
/**
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
batch_finish_example:
22
title: 'Batch Finish Example'
33
parent: drupal_batch_examples
4-
description: 'Batch finish example.'
4+
description: 'An example of using the finish property of the batch API to stop the batch.'
55
route_name: batch_finish_example
6-
weight: 2
6+
weight: 3

modules/batch_finish_example/src/Form/BatchForm.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function getFormId() {
2424
* {@inheritdoc}
2525
*/
2626
public function buildForm(array $form, FormStateInterface $form_state) {
27+
$form['help'] = [
28+
'#markup' => $this->t('Submit this form to run a batch operation that will process 1000 items in batches of 100. Only one batch operation is given to the batch API, and this is called until a finish state is reached.'),
29+
];
30+
2731
$form['actions'] = [
2832
'#type' => 'actions',
2933
'submit' => [
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
batch_form_example:
22
title: 'Batch Form Example'
33
parent: drupal_batch_examples
4-
description: 'Batch form example.'
4+
description: 'A simple example of the batch API contained within a form class.'
55
route_name: batch_form_example
66
weight: 1

modules/batch_form_example/src/Form/BatchForm.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function getFormId() {
2323
* {@inheritdoc}
2424
*/
2525
public function buildForm(array $form, FormStateInterface $form_state) {
26+
$form['help'] = [
27+
'#markup' => $this->t('Submit this form to run a self contained batch operation. This operation will run through 1000 items in chunks of 100.'),
28+
];
29+
2630
$form['actions'] = [
2731
'#type' => 'actions',
2832
'submit' => [

0 commit comments

Comments
 (0)