Commit 3d827c8a authored by catch's avatar catch
Browse files

Issue #3539919 by nicxvan, berdir: Convert batch callbacks to CallableResolver

(cherry picked from commit 29242328)
parent 7e34b3bb
Loading
Loading
Loading
Loading
Loading
+18 −12
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
use Drupal\Core\Batch\Percentage;
use Drupal\Core\Form\FormState;
use Drupal\Core\Url;
use Drupal\Core\Utility\CallableResolver;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -294,7 +295,9 @@ function _batch_process() {
        'finished' => &$finished,
        'message'  => &$task_message,
      ];
      call_user_func_array($callback, array_merge($args, [&$batch_context]));
      $callable_resolver = \Drupal::service(CallableResolver::class);
      $callable = $callable_resolver->getCallableFromDefinition($callback);
      $callable(...array_merge($args, [&$batch_context]));

      if ($finished >= 1) {
        // Make sure this step is not counted twice when computing $current.
@@ -429,7 +432,9 @@ function _batch_next_set() {
      // We use our stored copies of $form and $form_state to account for
      // possible alterations by previous form submit handlers.
      $complete_form = &$batch['form_state']->getCompleteForm();
      call_user_func_array($callback, [&$complete_form, &$batch['form_state']]);
      $callable_resolver = \Drupal::service(CallableResolver::class);
      $callable = $callable_resolver->getCallableFromDefinition($callback);
      $callable(...[&$complete_form, &$batch['form_state']]);
    }
    return TRUE;
  }
@@ -452,10 +457,12 @@ function _batch_finished() {
      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'], [$batch_set['success'], $batch_set['results'], $operations, \Drupal::service('date.formatter')->formatInterval((int) ($batch_set['elapsed'] / 1000))]);
      $callable_resolver = \Drupal::service(CallableResolver::class);
      $callable = $callable_resolver->getCallableFromDefinition($batch_set['finished']);
      $batch_set_result = $callable(...array_merge([$batch_set['success'], $batch_set['results'], $operations, \Drupal::service('date.formatter')->formatInterval((int) ($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.
@@ -464,7 +471,6 @@ function _batch_finished() {
      }
    }
  }
  }

  // Clean up the batch table and unset the static $batch variable.
  if ($batch['progressive']) {
+5 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Batch\BatchStorageInterface;
use Drupal\Core\Form\FormPreprocess;
use Drupal\Core\Url;
use Drupal\Core\Utility\CallableResolver;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
@@ -817,8 +818,10 @@ function batch_process($redirect = NULL, ?Url $url = NULL, $redirect_callback =
      $query_options['op'] = 'start';
      $query_options['id'] = $batch['id'];
      $batch_url->setOption('query', $query_options);
      if (($function = $batch['redirect_callback']) && function_exists($function)) {
        $function($batch_url->toString(), ['query' => $query_options]);
      if (($callback = $batch['redirect_callback'])) {
        $callable_resolver = \Drupal::service(CallableResolver::class);
        $callable = $callable_resolver->getCallableFromDefinition($callback);
        $callable($batch_url->toString(), ['query' => $query_options]);
      }
      else {
        return new RedirectResponse($batch_url->setAbsolute()->toString(TRUE)->getGeneratedUrl());
+3 −0
Original line number Diff line number Diff line
services:
  Drupal\batch_test\BatchInjectionCallbacks:
    autowire: true
+56 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\batch_test;

use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Batch callbacks using dependency injection for testing batches.
 */
class BatchInjectionCallbacks {
  use StringTranslationTrait;

  public function __construct(
    protected readonly ThemeManagerInterface $themeManager,
    protected readonly RouteMatchInterface $routeMatch,
    protected readonly RequestStack $requestStack,
    protected readonly TitleResolverInterface $titleResolver,
  ) {}

  /**
   * Implements callback_batch_operation().
   *
   * Tests the progress page theme.
   */
  public function themeCallback(): void {
    $batch_test_helper = new BatchTestHelper();
    // Because drupalGet() steps through the full progressive batch before
    // returning control to the test function, we cannot test that the correct
    // theme is being used on the batch processing page by viewing that page
    // directly. Instead, we save the theme being used in a variable here, so
    // that it can be loaded and inspected in the thread running the test.
    $theme = $this->themeManager->getActiveTheme()->getName();
    $batch_test_helper->stack($theme);
  }

  /**
   * Tests the title on the progress page by performing a batch callback.
   */
  public function titleCallback(): void {
    $batch_test_helper = new BatchTestHelper();
    // Because drupalGet() steps through the full progressive batch before
    // returning control to the test function, we cannot test that the correct
    // title is being used on the batch processing page by viewing that page
    // directly. Instead, we save the title being used in a variable here, so
    // that it can be loaded and inspected in the thread running the test.
    $title = $this->titleResolver->getTitle($this->requestStack->getCurrentRequest(), $this->routeMatch->getRouteObject());
    $batch_test_helper->stack($title);
  }

}
+0 −32
Original line number Diff line number Diff line
@@ -15,38 +15,6 @@
class BatchTestCallbacks {
  use StringTranslationTrait;

  /**
   * Implements callback_batch_operation().
   *
   * Tests the progress page theme.
   */
  public function themeCallback(): void {
    $batch_test_helper = new BatchTestHelper();
    // Because drupalGet() steps through the full progressive batch before
    // returning control to the test function, we cannot test that the correct
    // theme is being used on the batch processing page by viewing that page
    // directly. Instead, we save the theme being used in a variable here, so
    // that it can be loaded and inspected in the thread running the test.
    $theme = \Drupal::theme()->getActiveTheme()->getName();
    $batch_test_helper->stack($theme);
  }

  /**
   * Tests the title on the progress page by performing a batch callback.
   */
  public function titleCallback(): void {
    $batch_test_helper = new BatchTestHelper();
    // Because drupalGet() steps through the full progressive batch before
    // returning control to the test function, we cannot test that the correct
    // title is being used on the batch processing page by viewing that page
    // directly. Instead, we save the title being used in a variable here, so
    // that it can be loaded and inspected in the thread running the test.
    $request = \Drupal::request();
    $route_match = \Drupal::routeMatch();
    $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
    $batch_test_helper->stack($title);
  }

  /**
   * Implements callback_batch_operation().
   *
Loading