/** * {@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; }
/** * 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; }