Commit 6a30c869 authored by catch's avatar catch

Issue #1283892 by tim.plunkett, chx: Let Render API fail in a tale-telling way on invalid element.

parent 398b181e
......@@ -6145,9 +6145,16 @@ function element_children(&$elements, $sort = FALSE) {
$sortable = FALSE;
foreach ($elements as $key => $value) {
if ($key === '' || $key[0] !== '#') {
$children[$key] = $value;
if (is_array($value) && isset($value['#weight'])) {
$sortable = TRUE;
if (is_array($value)) {
$children[$key] = $value;
if (isset($value['#weight'])) {
$sortable = TRUE;
}
}
// Only trigger an error if the value is not null.
// @see http://drupal.org/node/1283892
elseif (isset($value)) {
trigger_error(t('"@key" is an invalid render array key', array('@key' => $key)), E_USER_ERROR);
}
}
}
......
......@@ -158,7 +158,7 @@ function menu_overview_form_submit($form, &$form_state) {
// are sent by $_POST, ensuring parents are saved first, then their children.
// See http://drupal.org/node/181126#comment-632270
$order = array_flip(array_keys($form_state['input'])); // Get the $_POST order.
$form = array_merge($order, $form); // Update our original form with the new order.
$form = array_intersect_key(array_merge($order, $form), $form); // Update our original form with the new order.
$updated_items = array();
$fields = array('weight', 'plid');
......
......@@ -1724,6 +1724,23 @@ class DrupalRenderTestCase extends DrupalWebTestCase {
));
}
/**
* Test rendering elements with invalid keys.
*/
function testDrupalRenderInvalidKeys() {
$error = array(
'%type' => 'User error',
'!message' => '"child" is an invalid render array key',
'%function' => 'element_children()',
);
$message = t('%type: !message in %function (line ', $error);
variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
$this->drupalGet('common-test/drupal-render-invalid-keys');
$this->assertResponse(200, t('Received expected HTTP status code.'));
$this->assertRaw($message, t('Found error message: !message.', array('!message' => $message)));
}
protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) {
$original_element = $element;
$this->drupalSetContent(drupal_render($element));
......
......@@ -52,6 +52,12 @@ function common_test_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['common-test/drupal-render-invalid-keys'] = array(
'title' => 'Drupal Render',
'page callback' => 'common_test_drupal_render_invalid_keys',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -100,6 +106,22 @@ function common_test_destination() {
print "The destination: " . check_plain($destination['destination']);
}
/**
* Render an element with an invalid render array key.
*/
function common_test_drupal_render_invalid_keys() {
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
// Keys that begin with # may contain a value of any type, otherwise they must
// contain arrays.
$key = 'child';
$value = 'This should be an array.';
$element = array(
$key => $value,
);
return drupal_render($element);
}
/**
* Implements hook_TYPE_alter().
*/
......
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