BatchController.php 3.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\system\Controller\BatchController.
 */

namespace Drupal\system\Controller;

10
11
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Page\DefaultHtmlFragmentRenderer;
13
14
15
use Drupal\Core\Page\HtmlPage;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
18
19
20
21
22
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Controller routines for batch routes.
 */
23
24
25
class BatchController implements ContainerInjectionInterface {

  /**
26
   * The fragment rendering service.
27
   *
28
   * @var \Drupal\Core\Page\DefaultHtmlFragmentRenderer
29
   */
30
  protected $fragmentRenderer;
31
32
33
34
35
36
37
38
39
40
41

  /**
   * The title resolver.
   *
   * @var \Drupal\Core\Controller\TitleResolverInterface
   */
  protected $titleResolver;

  /**
   * Constructs a new BatchController.
   *
42
43
   * @param \Drupal\Core\Page\DefaultHtmlFragmentRenderer $html_fragment_renderer
   *   The fragment rendering service.
44
45
46
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   */
47
48
  public function __construct(DefaultHtmlFragmentRenderer $html_fragment_renderer, TitleResolverInterface $title_resolver) {
    $this->fragmentRenderer = $html_fragment_renderer;
49
50
51
52
53
54
55
56
    $this->titleResolver = $title_resolver;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
57
      $container->get('html_fragment_renderer'),
58
59
60
      $container->get('title_resolver')
    );
  }
61
62
63
64

  /**
   * Returns a system batch page.
   *
65
   * @param \Symfony\Component\HttpFoundation\Request $request
66
67
68
   *   The current request object.
   *
   * @return mixed
69
70
71
72
   *   A \Symfony\Component\HttpFoundation\Response object or page element or
   *   NULL.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
   */
  public function batchPage(Request $request) {
    require_once DRUPAL_ROOT . '/core/includes/batch.inc';
    $output = _batch_page($request);

    if ($output === FALSE) {
      throw new AccessDeniedHttpException();
    }
    elseif ($output instanceof Response) {
      return $output;
    }
    elseif (isset($output)) {
      // Force a page without blocks or messages to
      // display a list of collected messages later.
      drupal_set_page_content($output);
      $page = element_info('page');
      $page['#show_messages'] = FALSE;
90
91
92

      $page = $this->render($page);

93
94
95
96
      return $page;
    }
  }

97
98
99
100
101
102
103
104
  /**
   * {@inheritdoc}
   */
  public function render(array $output, $status_code = 200) {
    if (!isset($output['#title'])) {
      $request = \Drupal::request();
      $output['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
    }
105
    $page = new HtmlPage('', isset($output['#cache']) ? $output['#cache'] : array(), $output['#title']);
106
107
108

    $page_array = drupal_prepare_page($output);

109
    $page = $this->fragmentRenderer->preparePage($page, $page_array);
110
111
112
113
114
115
116
117
118
119

    $page->setBodyTop(drupal_render($page_array['page_top']));
    $page->setBodyBottom(drupal_render($page_array['page_bottom']));
    $page->setContent(drupal_render($page_array));

    $page->setStatusCode($status_code);

    return $page;
  }

120
}