You are here

function _batch_append_set in Drupal 9

Same name and namespace in other branches
  1. 8 core/includes/form.inc \_batch_append_set()

Appends a batch set to a running batch.

Inserts the new set right after the current one to ensure execution order, and stores its operations in a queue. If the current batch has already inserted a new set, additional sets will be inserted after the last inserted set.

Parameters

&$batch: The batch array.

$batch_set: The batch set.

Related topics

1 call to _batch_append_set()
batch_set in core/includes/form.inc
Adds a new batch.

File

core/includes/form.inc, line 804
Functions for form and batch generation and processing.

Code

function _batch_append_set(&$batch, $batch_set) {
  $append_after_index = $batch['current_set'];
  $reached_current_set = FALSE;
  foreach ($batch['sets'] as $index => $set) {

    // As the indexes are not ordered numerically we need to first reach the
    // index of the current set and then search for the proper place to append
    // the new batch set.
    if (!$reached_current_set) {
      if ($index == $batch['current_set']) {
        $reached_current_set = TRUE;
      }
      continue;
    }
    if ($index > $append_after_index) {
      if (isset($set['appended_after_index'])) {
        $append_after_index = $index;
      }
      else {
        break;
      }
    }
  }
  $batch_set['appended_after_index'] = $append_after_index;

  // Iterate by reference over the existing batch sets and assign them by
  // reference in the new batch sets array in order not to break a retrieved
  // reference to the current set. Among other places a reference to the current
  // set is being retrieved in _batch_process(). Additionally, we have to
  // preserve the original indexes, as they are used to generate the queue name
  // of each batch set, otherwise the operations of the new batch set will be
  // queued in the queue of a previous batch set.
  // @see _batch_populate_queue().
  $new_sets = [];
  foreach ($batch['sets'] as $index => &$set) {
    $new_sets[$index] =& $set;
    if ($index == $append_after_index) {
      $new_set_index = count($batch['sets']);
      $new_sets[$new_set_index] = $batch_set;
    }
  }
  $batch['sets'] = $new_sets;
  _batch_populate_queue($batch, $new_set_index);
}