Skip to content

Commit

Permalink
Added optional limit for the update helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 18, 2023
1 parent c49f1b3 commit fbf2e26
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions web/themes/contrib/civictheme/src/CivicthemeUpdateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [];
Expand Down

0 comments on commit fbf2e26

Please sign in to comment.