BatchController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Drupal\system\Controller;
  3. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. /**
  9. * Controller routines for batch routes.
  10. */
  11. class BatchController implements ContainerInjectionInterface {
  12. /**
  13. * The app root.
  14. *
  15. * @var string
  16. */
  17. protected $root;
  18. /**
  19. * Constructs a new BatchController.
  20. *
  21. * @param string $root
  22. * The app root.
  23. */
  24. public function __construct($root) {
  25. $this->root = $root;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function create(ContainerInterface $container) {
  31. return new static(
  32. $container->get('app.root')
  33. );
  34. }
  35. /**
  36. * Returns a system batch page.
  37. *
  38. * @param \Symfony\Component\HttpFoundation\Request $request
  39. * The current request object.
  40. *
  41. * @return \Symfony\Component\HttpFoundation\Response|array
  42. * A \Symfony\Component\HttpFoundation\Response object or render array.
  43. *
  44. * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
  45. */
  46. public function batchPage(Request $request) {
  47. require_once $this->root . '/core/includes/batch.inc';
  48. $output = _batch_page($request);
  49. if ($output === FALSE) {
  50. throw new AccessDeniedHttpException();
  51. }
  52. elseif ($output instanceof Response) {
  53. return $output;
  54. }
  55. elseif (isset($output)) {
  56. $title = isset($output['#title']) ? $output['#title'] : NULL;
  57. $page = [
  58. '#type' => 'page',
  59. '#title' => $title,
  60. '#show_messages' => FALSE,
  61. 'content' => $output,
  62. ];
  63. // Also inject title as a page header (if available).
  64. if ($title) {
  65. $page['header'] = [
  66. '#type' => 'page_title',
  67. '#title' => $title,
  68. ];
  69. }
  70. return $page;
  71. }
  72. }
  73. /**
  74. * The _title_callback for the system.batch_page.normal route.
  75. *
  76. * @return string
  77. * The page title.
  78. */
  79. public function batchPageTitle() {
  80. $current_set = _batch_current_set();
  81. return !empty($current_set['title']) ? $current_set['title'] : '';
  82. }
  83. }