/** * 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)); } $content = $this->drupalRender($page_content); $cache = !empty($page_content['#cache']['tags']) ? array('tags' => $page_content['#cache']['tags']) : array(); $fragment = new HtmlFragment($content, $cache); // 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), Title::PASS_THROUGH); } } // Add feed links from the page content. $attached = drupal_render_collect_attached($page_content, TRUE); if (!empty($attached['drupal_add_feed'])) { foreach ($attached['drupal_add_feed'] as $feed) { $feed_link = new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0])); $fragment->addLinkElement($feed_link); } } return $fragment; }
/** * {@inheritdoc} */ public function render(array $render_array) { $content = $this->drupalRenderRoot($render_array); if (!empty($render_array)) { drupal_process_attached($render_array); } $cache = !empty($render_array['#cache']['tags']) ? ['tags' => $render_array['#cache']['tags']] : []; $fragment = new HtmlFragment($content, $cache); if (isset($render_array['#title'])) { $fragment->setTitle($render_array['#title'], Title::FILTER_XSS_ADMIN); } $attached = isset($render_array['#attached']) ? $render_array['#attached'] : []; $attached += ['feed' => [], 'html_head' => [], 'html_head_link' => []]; // Add feed links from the page content. foreach ($attached['feed'] as $feed) { $fragment->addLinkElement(new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0]))); } // Add generic links from the page content. foreach ($attached['html_head_link'] as $link) { $fragment->addLinkElement(new LinkElement($this->urlGenerator->generateFromPath($link[0]['href']), $link[0]['rel'])); } // @todo Also transfer the contents of "_drupal_add_html_head" once // https://www.drupal.org/node/2296951 lands. // @todo Transfer CSS and JS over to the fragment once those are supported // on the fragment object. return $fragment; }
/** * Constructs a new HtmlPage object. * * @param string $content * (optional) The body content of the page. * @param array $cache_info * The cache information. * @param string $title * (optional) The title of the page. */ public function __construct($content = '', array $cache_info = array(), $title = '') { parent::__construct($content, $cache_info); $this->title = $title; $this->htmlAttributes = new Attribute(); $this->bodyAttributes = new Attribute(); }
/** * @param $title * The page title of the response. * @param $body * The body of the error page. * @param $response_code * The HTTP response code of the response. * @return Response * An error Response object ready to return to the browser. */ protected function createResponse($title, $body, $response_code) { $fragment = new HtmlFragment($body); $fragment->setTitle($title); $page = $this->fragmentRenderer->render($fragment, $response_code); return new Response($this->htmlPageRenderer->render($page), $page->getStatusCode()); }