HtmlControllerBase.php 3.05 KB
Newer Older
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
<?php

/**
 * @file
 * Contains \Drupal\Core\Controller\HtmlControllerBase.
 */

namespace Drupal\Core\Controller;

use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Page\HtmlFragment;
use Drupal\Core\Utility\Title;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Base class for HTML page-generating controllers.
 */
class HtmlControllerBase {

  /**
   * The translation manager service.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationManager;

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

  /**
   * Constructs a new HtmlControllerBase object.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
   *   The translation manager.
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   */
  public function __construct(TranslationInterface $translation_manager, TitleResolverInterface $title_resolver) {
    $this->translationManager = $translation_manager;
    $this->titleResolver = $title_resolver;
  }

  /**
   * Converts a render array into an HtmlFragment object.
   *
   * @param array|string $page_content
   *   The page content area to display.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Drupal\Core\Page\HtmlPage
   *   A page object.
   */
  protected function createHtmlFragment($page_content, Request $request) {
    // Allow controllers to return a HtmlFragment or a Response object directly.
    if ($page_content instanceof HtmlFragment || $page_content instanceof Response) {
      return $page_content;
    }

    if (!is_array($page_content)) {
      $page_content = array(
        'main' => array(
          '#markup' => $page_content,
        ),
      );
    }

74
75
76
    $cache_tags = $this->drupalRenderCollectCacheTags($page_content);
    $cache = !empty($cache_tags) ? array('tags' => $cache_tags) : array();
    $fragment = new HtmlFragment($this->drupalRender($page_content), $cache);
77
78
79
80
81
82
83
84
85
86
87
88

    // A title defined in the return always wins.
    if (isset($page_content['#title'])) {
      $fragment->setTitle($page_content['#title'], Title::FILTER_XSS_ADMIN);
    }
    else if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
      $fragment->setTitle($this->titleResolver->getTitle($request, $route), PASS_THROUGH);
    }

    return $fragment;
  }

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  /**
   * Wraps drupal_render().
   *
   * @todo: Remove as part of https://drupal.org/node/2182149
   */
  protected function drupalRender(&$elements, $is_recursive_call = FALSE) {
    return drupal_render($elements, $is_recursive_call);
  }

  /**
   * Wraps drupal_render_collect_cache_tags()
   *
   * @todo: Remove as part of https://drupal.org/node/2182149
   */
  protected function drupalRenderCollectCacheTags($element, $tags = array()) {
    return drupal_render_collect_cache_tags($element, $tags);
  }

107
}