summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.txt2
-rw-r--r--includes/common.inc12
-rw-r--r--modules/simpletest/tests/common.test54
-rw-r--r--modules/simpletest/tests/common_test.module8
4 files changed, 70 insertions, 6 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 633985059..7ecc4cf8e 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,6 +1,8 @@
Drupal 7.23, xxxx-xx-xx (development version)
-----------------------
+- Fixed drupal_render() to always return an empty string when there is no
+ output, rather than sometimes returning NULL (minor API change).
- Added protection to cache_clear_all() to ensure that non-cache tables cannot
be truncated (API addition: a new isValidBin() method has been added to the
default database cache implementation).
diff --git a/includes/common.inc b/includes/common.inc
index d2a36be2b..262e1c57b 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -5795,23 +5795,23 @@ function drupal_render_page($page) {
* array to be rendered independently and prevents them from being rendered
* more than once on subsequent calls to drupal_render() (e.g., as part of a
* larger array). If the same array or array element is passed more than once
- * to drupal_render(), it simply returns a NULL value.
+ * to drupal_render(), it simply returns an empty string.
*
- * @param $elements
+ * @param array $elements
* The structured array describing the data to be rendered.
*
- * @return
+ * @return string
* The rendered HTML.
*/
function drupal_render(&$elements) {
// Early-return nothing if user does not have access.
if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) {
- return;
+ return '';
}
// Do not print elements twice.
if (!empty($elements['#printed'])) {
- return;
+ return '';
}
// Try to fetch the element's markup from cache and return.
@@ -5847,7 +5847,7 @@ function drupal_render(&$elements) {
// Allow #pre_render to abort rendering.
if (!empty($elements['#printed'])) {
- return;
+ return '';
}
// Get the children of the element, sorted by weight.
diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test
index 974812636..8694ff32e 100644
--- a/modules/simpletest/tests/common.test
+++ b/modules/simpletest/tests/common.test
@@ -1565,6 +1565,60 @@ class DrupalRenderTestCase extends DrupalWebTestCase {
}
/**
+ * Tests the output drupal_render() for some elementary input values.
+ */
+ function testDrupalRenderBasics() {
+ $types = array(
+ array(
+ 'name' => 'null',
+ 'value' => NULL,
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'no value',
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'empty string',
+ 'value' => '',
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'no access',
+ 'value' => array(
+ '#markup' => 'foo',
+ '#access' => FALSE,
+ ),
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'previously printed',
+ 'value' => array(
+ '#markup' => 'foo',
+ '#printed' => TRUE,
+ ),
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'printed in prerender',
+ 'value' => array(
+ '#markup' => 'foo',
+ '#pre_render' => array('common_test_drupal_render_printing_pre_render'),
+ ),
+ 'expected' => '',
+ ),
+ array(
+ 'name' => 'basic renderable array',
+ 'value' => array('#markup' => 'foo'),
+ 'expected' => 'foo',
+ ),
+ );
+ foreach($types as $type) {
+ $this->assertIdentical(drupal_render($type['value']), $type['expected'], '"' . $type['name'] . '" input rendered correctly by drupal_render().');
+ }
+ }
+
+ /**
* Test sorting by weight.
*/
function testDrupalRenderSorting() {
diff --git a/modules/simpletest/tests/common_test.module b/modules/simpletest/tests/common_test.module
index e75b45237..674a49446 100644
--- a/modules/simpletest/tests/common_test.module
+++ b/modules/simpletest/tests/common_test.module
@@ -101,6 +101,14 @@ function common_test_destination() {
}
/**
+ * Applies #printed to an element to help test #pre_render.
+ */
+function common_test_drupal_render_printing_pre_render($elements) {
+ $elements['#printed'] = TRUE;
+ return $elements;
+}
+
+/**
* Implements hook_TYPE_alter().
*/
function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {