Skip to content

Commit f45f900

Browse files
committed
Added batching to the migration
1 parent e1abc9d commit f45f900

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

modules/os2forms_digital_signature/os2forms_digital_signature.install

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ use Drupal\webform\Entity\Webform;
1818
* now lives on the os2forms_digital_signature handler, so this update
1919
* replicates that selection into the handler config of every webform using
2020
* the handler, and strips the legacy element properties from all webforms.
21+
*
22+
* Processes the webforms in batches of 25 via the update sandbox.
2123
*/
2224
function os2forms_digital_signature_update_10001(&$sandbox) {
2325
$logger = \Drupal::logger('os2forms_digital_signature');
24-
$migrated = 0;
25-
$skipped = [];
2626

27+
if (!isset($sandbox['ids'])) {
28+
$sandbox['ids'] = array_values(\Drupal::entityQuery('webform')->accessCheck(FALSE)->execute());
29+
$sandbox['migrated'] = 0;
30+
}
31+
32+
// array_splice() removes the chunk from the sandbox, so the next pass
33+
// continues with the remaining ids.
2734
/** @var \Drupal\webform\WebformInterface[] $webforms */
28-
$webforms = Webform::loadMultiple();
35+
$webforms = Webform::loadMultiple(array_splice($sandbox['ids'], 0, 25));
2936
foreach ($webforms as $webform) {
3037
$handlers = $webform->getHandlers();
3138
$signatureHandler = NULL;
@@ -63,7 +70,9 @@ function os2forms_digital_signature_update_10001(&$sandbox) {
6370
}
6471

6572
if ($signedKey === NULL) {
66-
$skipped[$webform->id()] = 'the handler is enabled but the webform has no os2forms_digital_signature_document or os2forms_attachment element';
73+
$logger->warning('Skipped webform @id during digital signature config migration: the handler is enabled but the webform has no os2forms_digital_signature_document or os2forms_attachment element. Configure the handler manually.', [
74+
'@id' => $webform->id(),
75+
]);
6776
}
6877
else {
6978
if (count($candidates) > 1) {
@@ -89,7 +98,7 @@ function os2forms_digital_signature_update_10001(&$sandbox) {
8998
$config['settings']['signature_position'] = $signedPosition;
9099
$signatureHandler->setConfiguration($config);
91100
$needsSave = TRUE;
92-
$migrated++;
101+
$sandbox['migrated']++;
93102
}
94103
}
95104

@@ -110,16 +119,12 @@ function os2forms_digital_signature_update_10001(&$sandbox) {
110119
}
111120
}
112121

113-
if ($skipped) {
114-
foreach ($skipped as $id => $reason) {
115-
$logger->warning('Skipped webform @id during digital signature config migration: @reason. Configure the handler manually.', [
116-
'@id' => $id,
117-
'@reason' => $reason,
118-
]);
119-
}
120-
}
122+
// Anything below 1 makes the runner call us again. Done when no ids remain.
123+
$sandbox['#finished'] = empty($sandbox['ids']) ? 1 : 0;
121124

122-
return t('Migrated digital signature config on @count webform(s).', ['@count' => $migrated]);
125+
if ($sandbox['#finished'] >= 1) {
126+
return t('Migrated digital signature config on @count webform(s).', ['@count' => $sandbox['migrated']]);
127+
}
123128
}
124129

125130
/**

0 commit comments

Comments
 (0)