Commit 008bf086 authored by alexpott's avatar alexpott
Browse files

Issue #2256373 by Crell, joelpittet: Factor HtmlFragment out to an interface.

parent 7424e462
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\Core\Page; namespace Drupal\Core\Page;
use Drupal\Core\Cache\CacheableInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManager;
...@@ -35,15 +36,18 @@ public function __construct(LanguageManager $language_manager) { ...@@ -35,15 +36,18 @@ public function __construct(LanguageManager $language_manager) {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function render(HtmlFragment $fragment, $status_code = 200) { public function render(HtmlFragmentInterface $fragment, $status_code = 200) {
// Converts the given HTML fragment which represents the main content region // Converts the given HTML fragment which represents the main content region
// of the page into a render array. // of the page into a render array.
$page_content['main'] = array( $page_content['main'] = array(
'#markup' => $fragment->getContent(), '#markup' => $fragment->getContent(),
'#cache' => array('tags' => $fragment->getCacheTags()),
); );
$page_content['#title'] = $fragment->getTitle(); $page_content['#title'] = $fragment->getTitle();
if ($fragment instanceof CacheableInterface) {
$page_content['main']['#cache']['tags'] = $fragment->getCacheTags();
}
// Build the full page array by calling drupal_prepare_page(), which invokes // Build the full page array by calling drupal_prepare_page(), which invokes
// hook_page_build(). This adds the other regions to the page. // hook_page_build(). This adds the other regions to the page.
$page_array = drupal_prepare_page($page_content); $page_array = drupal_prepare_page($page_content);
...@@ -54,14 +58,17 @@ public function render(HtmlFragment $fragment, $status_code = 200) { ...@@ -54,14 +58,17 @@ public function render(HtmlFragment $fragment, $status_code = 200) {
$page->setBodyTop(drupal_render($page_array['page_top'])); $page->setBodyTop(drupal_render($page_array['page_top']));
$page->setBodyBottom(drupal_render($page_array['page_bottom'])); $page->setBodyBottom(drupal_render($page_array['page_bottom']));
$page->setContent(drupal_render($page_array)); $page->setContent(drupal_render($page_array));
// Collect cache tags for all the content in all the regions on the page.
$tags = $page_array['#cache']['tags'];
// Enforce the generic "content" cache tag on all pages.
// @todo Remove the "content" cache tag. @see https://drupal.org/node/2124957
$tags['content'] = TRUE;
$page->setCacheTags($tags);
$page->setStatusCode($status_code); $page->setStatusCode($status_code);
if ($fragment instanceof CacheableInterface) {
// Collect cache tags for all the content in all the regions on the page.
$tags = $page_array['#cache']['tags'];
// Enforce the generic "content" cache tag on all pages.
// @todo Remove the "content" cache tag. @see https://drupal.org/node/2124957
$tags['content'] = TRUE;
$page->setCacheTags($tags);
}
return $page; return $page;
} }
......
...@@ -13,13 +13,9 @@ ...@@ -13,13 +13,9 @@
use Drupal\Core\Utility\Title; use Drupal\Core\Utility\Title;
/** /**
* Response object that contains variables for injection into the html template. * Basic mutable implementation of an HTML fragment.
*
* @todo Should we have this conform to an interface?
* https://drupal.org/node/1871596#comment-7134686
* @todo Add method replacements for *all* data sourced by html.tpl.php.
*/ */
class HtmlFragment implements CacheableInterface { class HtmlFragment implements CacheableInterface, HtmlFragmentInterface {
/** /**
* An array of Link elements. * An array of Link elements.
...@@ -89,18 +85,14 @@ public function addLinkElement(LinkElement $link) { ...@@ -89,18 +85,14 @@ public function addLinkElement(LinkElement $link) {
} }
/** /**
* Returns an array of all enqueued links. * {@inheritdoc}
*
* @return \Drupal\Core\Page\LinkElement[]
*/ */
public function &getLinkElements() { public function &getLinkElements() {
return $this->links; return $this->links;
} }
/** /**
* Returns all feed link elements. * {@inheritdoc}
*
* @return \Drupal\Core\Page\FeedLinkElement[]
*/ */
public function getFeedLinkElements() { public function getFeedLinkElements() {
$feed_links = array(); $feed_links = array();
...@@ -126,9 +118,7 @@ public function addMetaElement(MetaElement $meta) { ...@@ -126,9 +118,7 @@ public function addMetaElement(MetaElement $meta) {
} }
/** /**
* Returns an array of all enqueued meta elements. * {@inheritdoc}
*
* @return \Drupal\Core\Page\MetaElement[]
*/ */
public function &getMetaElements() { public function &getMetaElements() {
return $this->metatags; return $this->metatags;
...@@ -154,10 +144,7 @@ public function setContent($content) { ...@@ -154,10 +144,7 @@ public function setContent($content) {
} }
/** /**
* Gets the main content of this HtmlFragment. * {@inheritdoc}
*
* @return string
* The content for this fragment.
*/ */
public function getContent() { public function getContent() {
return $this->content; return $this->content;
...@@ -197,19 +184,14 @@ public function setTitle($title, $output = Title::CHECK_PLAIN) { ...@@ -197,19 +184,14 @@ public function setTitle($title, $output = Title::CHECK_PLAIN) {
} }
/** /**
* Indicates whether or not this HtmlFragment has a title. * {@inheritdoc}
*
* @return bool
*/ */
public function hasTitle() { public function hasTitle() {
return !empty($this->title); return !empty($this->title);
} }
/** /**
* Gets the title for this HtmlFragment, if any. * {@inheritdoc}
*
* @return string
* The title.
*/ */
public function getTitle() { public function getTitle() {
return $this->title; return $this->title;
......
<?php
/**
* @file
* Contains \Drupal\Core\Page\HtmlFragmentInterface.
*/
namespace Drupal\Core\Page;
/**
* A domain object for a portion of an HTML page, including related data.
*
* Related data includes any additional information relevant to a fragment of
* HTML that would not be part of the HTML string itself. That includes, for
* example, required CSS files, Javascript files, link tags, meta tags, and the
* title of a page or page section.
*/
interface HtmlFragmentInterface {
/**
* Indicates whether or not this HtmlFragment has a title.
*
* @return bool
*/
public function hasTitle();
/**
* Gets the title for this HtmlFragment, if any.
*
* @return string
* The title.
*/
public function getTitle();
/**
* Gets the main content of this HtmlFragment.
*
* @return string
* The content for this fragment.
*/
public function getContent();
/**
* Returns an array of all enqueued links.
*
* @return \Drupal\Core\Page\LinkElement[]
*/
public function getLinkElements();
/**
* Returns all feed link elements.
*
* @return \Drupal\Core\Page\FeedLinkElement[]
*/
public function getFeedLinkElements();
/**
* Returns an array of all enqueued meta elements.
*
* @return \Drupal\Core\Page\MetaElement[]
*/
public function getMetaElements();
}
...@@ -24,7 +24,7 @@ interface HtmlFragmentRendererInterface { ...@@ -24,7 +24,7 @@ interface HtmlFragmentRendererInterface {
* metadata from the fragment and using the body of the fragment as the main * metadata from the fragment and using the body of the fragment as the main
* content region of the page. * content region of the page.
* *
* @param \Drupal\Core\Page\HtmlFragment $fragment * @param \Drupal\Core\Page\HtmlFragmentInterface $fragment
* The HTML fragment object to convert up to a page. * The HTML fragment object to convert up to a page.
* @param int $status_code * @param int $status_code
* (optional) The status code of the page. May be any legal HTTP response * (optional) The status code of the page. May be any legal HTTP response
...@@ -33,6 +33,6 @@ interface HtmlFragmentRendererInterface { ...@@ -33,6 +33,6 @@ interface HtmlFragmentRendererInterface {
* @return \Drupal\Core\Page\HtmlPage * @return \Drupal\Core\Page\HtmlPage
* An HtmlPage object derived from the provided fragment. * An HtmlPage object derived from the provided fragment.
*/ */
public function render(HtmlFragment $fragment, $status_code = 200); public function render(HtmlFragmentInterface $fragment, $status_code = 200);
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment