_batch_finished()
Ends the batch processing.
Call the 'finished' callback of each batch set to allow custom handling of the results and resolve page redirection.
File
- core/includes/batch.inc, line 400
- Batch processing API for processes to run in multiple HTTP requests.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | function _batch_finished() { $batch = &batch_get(); $batch_finished_redirect = NULL; // Execute the 'finished' callbacks for each batch set, if defined. foreach ( $batch [ 'sets' ] as $batch_set ) { if (isset( $batch_set [ 'finished' ])) { // Check if the set requires an additional file for function definitions. if (isset( $batch_set [ 'file' ]) && is_file ( $batch_set [ 'file' ])) { include_once \Drupal::root() . '/' . $batch_set [ 'file' ]; } if ( is_callable ( $batch_set [ 'finished' ])) { $queue = _batch_queue( $batch_set ); $operations = $queue ->getAllItems(); $batch_set_result = call_user_func_array( $batch_set [ 'finished' ], array ( $batch_set [ 'success' ], $batch_set [ 'results' ], $operations , \Drupal::service( 'date.formatter' )->formatInterval( $batch_set [ 'elapsed' ] / 1000))); // If a batch 'finished' callback requested a redirect after the batch // is complete, save that for later use. If more than one batch set // returned a redirect, the last one is used. if ( $batch_set_result instanceof RedirectResponse) { $batch_finished_redirect = $batch_set_result ; } } } } // Clean up the batch table and unset the static $batch variable. if ( $batch [ 'progressive' ]) { \Drupal::service( 'batch.storage' )-> delete ( $batch [ 'id' ]); foreach ( $batch [ 'sets' ] as $batch_set ) { if ( $queue = _batch_queue( $batch_set )) { $queue ->deleteQueue(); } } // Clean-up the session. Not needed for CLI updates. if (isset( $_SESSION )) { unset( $_SESSION [ 'batches' ][ $batch [ 'id' ]]); if ( empty ( $_SESSION [ 'batches' ])) { unset( $_SESSION [ 'batches' ]); } } } $_batch = $batch ; $batch = NULL; // Redirect if needed. if ( $_batch [ 'progressive' ]) { // Revert the 'destination' that was saved in batch_process(). if (isset( $_batch [ 'destination' ])) { \Drupal::request()->query->set( 'destination' , $_batch [ 'destination' ]); } // Determine the target path to redirect to. If a batch 'finished' callback // returned a redirect response object, use that. Otherwise, fall back on // the form redirection. if (isset( $batch_finished_redirect )) { return $batch_finished_redirect ; } elseif (!isset( $_batch [ 'form_state' ])) { $_batch [ 'form_state' ] = new FormState(); } if ( $_batch [ 'form_state' ]->getRedirect() === NULL) { $redirect = $_batch [ 'batch_redirect' ] ? : $_batch [ 'source_url' ]; // Any path with a scheme does not correspond to a route. if (! $redirect instanceof Url) { $options = UrlHelper::parse( $redirect ); if ( parse_url ( $options [ 'path' ], PHP_URL_SCHEME)) { $redirect = Url::fromUri( $options [ 'path' ], $options ); } else { $redirect = \Drupal::pathValidator()->getUrlIfValid( $options [ 'path' ]); if (! $redirect ) { // Stay on the same page if the redirect was invalid. $redirect = Url::fromRoute( '<current>' ); } $redirect ->setOptions( $options ); } } $_batch [ 'form_state' ]->setRedirectUrl( $redirect ); } // Use \Drupal\Core\Form\FormSubmitterInterface::redirectForm() to handle // the redirection logic. $redirect = \Drupal::service( 'form_submitter' )->redirectForm( $_batch [ 'form_state' ]); if ( is_object ( $redirect )) { return $redirect ; } // If no redirection happened, redirect to the originating page. In case the // form needs to be rebuilt, save the final $form_state for // \Drupal\Core\Form\FormBuilderInterface::buildForm(). if ( $_batch [ 'form_state' ]->isRebuilding()) { $_SESSION [ 'batch_form_state' ] = $_batch [ 'form_state' ]; } $callback = $_batch [ 'redirect_callback' ]; /** @var \Drupal\Core\Url $source_url */ $source_url = $_batch [ 'source_url' ]; if ( is_callable ( $callback )) { $callback ( $_batch [ 'source_url' ], array ( 'query' => array ( 'op' => 'finish' , 'id' => $_batch [ 'id' ]))); } elseif ( $callback === NULL) { // Default to RedirectResponse objects when nothing specified. $url = $source_url ->setAbsolute() ->setOption( 'query' , [ 'op' => 'finish' , 'id' => $_batch [ 'id' ]]); return new RedirectResponse( $url ->toString()); } } } |
Please login to continue.