Commit 03cc400b authored by catch's avatar catch

Issue #2272279 by joelpittet, chx: Kill RenderWrapper class.

parent 94e0188a
......@@ -20,7 +20,6 @@
use Drupal\Core\Page\MetaElement;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\RenderWrapper;
use Drupal\Core\Theme\ThemeSettings;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
......@@ -1958,7 +1957,7 @@ function template_preprocess_html(&$variables) {
$variables['html_attributes'] = $page->getHtmlAttributes();
$variables['attributes'] = $page->getBodyAttributes();
$variables['page'] = $page->getContent();
$variables['page'] = $page;
// Compile a list of classes that are going to be applied to the body element.
// This allows advanced theming based on context (home page, node of certain type, etc.).
......@@ -2028,21 +2027,8 @@ function template_preprocess_html(&$variables) {
$page->addMetaElement($metatag);
}
}
$variables['page_top'][] = array('#markup' => $page->getBodyTop());
$variables['page_bottom'][] = array('#markup' => $page->getBodyBottom());
// Add footer scripts as '#markup' so they can be rendered with other
// elements in page_bottom.
$footer_scripts = new RenderWrapper('drupal_get_js', array('footer'));
$variables['page_bottom'][] = array('#markup' => $footer_scripts);
// Wrap function calls in an object so they can be called when printed.
$variables['head'] = new RenderWrapper(function() use ($page) {
return implode("\n", $page->getMetaElements()) . implode("\n", $page->getLinkElements());
});
$variables['styles'] = new RenderWrapper('drupal_get_css');
$variables['scripts'] = new RenderWrapper('drupal_get_js');
}
/**
......
......@@ -77,6 +77,44 @@ public function getHtmlAttributes() {
return $this->htmlAttributes;
}
/**
* Implodes the meta and link elements for the template.
*
* @return string
* A string of meta and link tags.
*/
public function getHead() {
return implode("\n", $this->getMetaElements()) . implode("\n", $this->getLinkElements());
}
/**
* Returns a themed presentation of all JavaScript code for the current page.
*
* @param string $scope
* (optional) The scope for which the JavaScript rules should be returned.
* Defaults to 'header'.
*
* @return string
* All JavaScript code segments and includes for the scope as HTML tags.
*
* @see drupal_get_js()
*/
public function getScripts($scope = 'header') {
return drupal_get_js($scope);
}
/**
* Returns a themed representation of all stylesheets to attach to the page.
*
* @return string
* A string of XHTML CSS tags.
*
* @see drupal_get_css()
*/
public function getStyles() {
return drupal_get_css();
}
/**
* Returns the HTML attributes for the body element of this page.
*
......
<?php
/**
* @file
* Contains \Drupal\Core\Template\RenderWrapper.
*/
namespace Drupal\Core\Template;
/**
* A class that wraps functions to call them while printing in a template.
*
* To use, one may pass in the function name as a string followed by an array of
* arguments to the constructor.
* @code
* $variables['scripts'] = new RenderWrapper('drupal_get_js', array('footer'));
* @endcode
*/
class RenderWrapper {
/**
* Stores the callback function to be called when rendered.
*
* @var callable
*/
public $callback;
/**
* Stores the callback's arguments.
*
* @var array
*/
public $args = array();
/**
* Constructs a RenderWrapper object.
*
* @param string $callback
* The callback function name.
* @param array $args
* The arguments to pass to the callback function.
*/
public function __construct($callback, array $args = array()) {
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Callback passed to RenderWrapper is not callable.');
}
$this->callback = $callback;
$this->args = $args;
}
/**
* Implements the magic __toString() method.
*/
public function __toString() {
return $this->render();
}
/**
* Returns a string provided by the callback function.
*
* @return string
* The results of the callback function.
*/
public function render() {
if (!empty($this->callback) && is_callable($this->callback)) {
return call_user_func_array($this->callback, $this->args);
}
}
}
......@@ -22,7 +22,7 @@
<html{{ html_attributes }}>
<head>
<title>{{ title }}</title>
{{ head }}
{{ page.head }}
<base href="{{ base_url }}" />
<link type="text/css" rel="stylesheet" href="misc/print.css" />
</head>
......
......@@ -29,17 +29,18 @@
<!DOCTYPE html>
<html{{ html_attributes }}>
<head>
{{ head }}
{{ page.head }}
<title>{{ head_title }}</title>
{{ styles }}
{{ scripts }}
{{ page.styles }}
{{ page.scripts }}
</head>
<body{{ attributes }}>
<a href="#main-content" class="visually-hidden focusable skip-link">
{{ 'Skip to main content'|t }}
</a>
{{ page_top }}
{{ page }}
{{ page.content }}
{{ page_bottom }}
{{ page.scripts('footer') }}
</body>
</html>
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Common\RenderWrapperTest.
*/
namespace Drupal\Tests\Core\Common;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Template\RenderWrapper;
/**
* Tests the \Drupal\Core\Template\RenderWrapper functionality.
*/
class RenderWrapperTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Render wrapper',
'description' => 'Tests the RenderWrapper class used for late rendering.',
'group' => 'Common',
);
}
/**
* Provides data for the RenderWrapper test.
*
* @return array
*/
public function providerTestRenderWrapperData() {
return array(
array('ucwords', array('Amazingly few discotheques provide jukeboxes.'), 'Amazingly Few Discotheques Provide Jukeboxes.', 'Simple string manipulation callback.'),
array('phpversion', array(), phpversion(), 'Callback with no arguments.'),
array(array('Drupal\Component\Utility\String', 'checkPlain'), array('<script>'), '&lt;script&gt;', 'Namespaced callback.'),
);
}
/**
* Tests casting a RenderWrapper object to a string.
*
* @see \Drupal\Core\Template\RenderWrapper::__toString()
*
* @dataProvider providerTestRenderWrapperData
*/
public function testDrupalRenderWrapper($callback, $arguments, $expected, $message) {
$this->assertSame($expected, (string) new RenderWrapper($callback, $arguments), $message);
}
/**
* Tests that an invalid callback throws an exception.
*
* @expectedException InvalidArgumentException
*/
public function testInvalidCallback() {
new RenderWrapper(FALSE);
}
}
......@@ -7,7 +7,6 @@
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Template\RenderWrapper;
use Drupal\Core\Template\Attribute;
/**
......
......@@ -6,7 +6,6 @@
*/
use Drupal\Component\Utility\Xss;
use Drupal\Core\Template\RenderWrapper;
use Drupal\Component\Utility\String;
/**
......
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