From fbf2e26668dacc21adb1171ff545c61b0e82c4b4 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Mon, 18 Dec 2023 13:47:48 +1100 Subject: [PATCH] Added optional limit for the update helper. --- .../civictheme/src/CivicthemeUpdateHelper.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/web/themes/contrib/civictheme/src/CivicthemeUpdateHelper.php b/web/themes/contrib/civictheme/src/CivicthemeUpdateHelper.php index 042f32e9e..c7a37b61d 100644 --- a/web/themes/contrib/civictheme/src/CivicthemeUpdateHelper.php +++ b/web/themes/contrib/civictheme/src/CivicthemeUpdateHelper.php @@ -65,17 +65,26 @@ public static function create(ContainerInterface $container): self { * Finish callback called when batch finished. * @param int $batch_size * Batch size. Defaults to 10. + * @param int|null $limit + * Limit. Defaults to no limit. * * @return string|null * Return log message on the last pass of the update. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function update(array &$sandbox, $entity_type, array $entity_bundles, callable $start_callback, callable $process_callback, callable $finish_callback, int $batch_size = 10): ?string { + public function update(array &$sandbox, $entity_type, array $entity_bundles, callable $start_callback, callable $process_callback, callable $finish_callback, int $batch_size = 10, ?int $limit = NULL): ?string { $storage = $this->entityTypeManager->getStorage($entity_type); if (!isset($sandbox['entities'])) { - $sandbox['batch'] = 0; - $sandbox['entities'] = $storage->getQuery()->accessCheck(FALSE)->condition('type', $entity_bundles, 'IN')->execute(); + $query = $storage->getQuery()->accessCheck(FALSE)->condition('type', $entity_bundles, 'IN'); + if ($limit) { + $query->range(0, $limit); + } + $sandbox['entities'] = $query->execute(); + $sandbox['max'] = count($sandbox['entities']); + $sandbox['batch'] = 0; $sandbox['results']['processed'] = []; $sandbox['results']['updated'] = [];