Commit 0142292c authored by webchick's avatar webchick
Browse files

#373201 by moshe weitzman, chx, Frando, eaton: Allow renderable array...

#373201 by moshe weitzman, chx, Frando, eaton: Allow renderable array properties to be passed directly to theme functions.
parent e9ca98b6
......@@ -3663,28 +3663,6 @@ function drupal_render(&$elements) {
}
}
// Add additional CSS and JavaScript files associated with this element.
foreach (array('css', 'js') as $kind) {
if (!empty($elements['#attached_' . $kind]) && is_array($elements['#attached_' . $kind])) {
foreach ($elements['#attached_' . $kind] as $data => $options) {
// If the value is not an array, it's a filename and passed as first
// (and only) argument.
if (!is_array($options)) {
$data = $options;
$options = NULL;
}
// When drupal_add_js with 'type' => 'setting' is called, the first
// parameter ($data) is an array. Arrays can't be keys in PHP, so we
// have to get $data from the value array.
if (is_numeric($data)) {
$data = $options['data'];
unset($options['data']);
}
call_user_func('drupal_add_' . $kind, $data, $options);
}
}
}
// Get the children of the element, sorted by weight.
$children = element_children($elements, TRUE);
......@@ -3716,6 +3694,28 @@ function drupal_render(&$elements) {
}
}
}
// Add additional CSS and JavaScript files associated with this element.
foreach (array('css', 'js') as $kind) {
if (!empty($elements['#attached_' . $kind]) && is_array($elements['#attached_' . $kind])) {
foreach ($elements['#attached_' . $kind] as $data => $options) {
// If the value is not an array, it's a filename and passed as first
// (and only) argument.
if (!is_array($options)) {
$data = $options;
$options = NULL;
}
// When drupal_add_js with 'type' => 'setting' is called, the first
// parameter ($data) is an array. Arrays can't be keys in PHP, so we
// have to get $data from the value array.
if (is_numeric($data)) {
$data = $options['data'];
unset($options['data']);
}
call_user_func('drupal_add_' . $kind, $data, $options);
}
}
}
$prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
$suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
......@@ -3982,9 +3982,6 @@ function drupal_common_theme() {
'item_list' => array(
'arguments' => array('items' => array(), 'title' => NULL, 'type' => 'ul', 'attributes' => NULL),
),
'list' => array(
'arguments' => array('elements' => NULL),
),
'more_help_link' => array(
'arguments' => array('url' => NULL),
),
......@@ -4011,7 +4008,7 @@ function drupal_common_theme() {
),
// from pager.inc
'pager' => array(
'arguments' => array('tags' => array(), 'element' => 0, 'parameters' => array()),
'arguments' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9),
),
'pager_first' => array(
'arguments' => array('text' => NULL, 'element' => 0, 'parameters' => array()),
......
......@@ -724,6 +724,17 @@ function theme() {
if (isset($info['function'])) {
// The theme call is a function.
if (drupal_function_exists($info['function'])) {
// If a theme function that does not expect a renderable array is called
// with a renderable array as the only argument (via drupal_render), then
// we take the arguments from the properties of the renderable array. If
// missing, use hook_theme() defaults.
if (isset($args[0]) && is_array($args[0]) && isset($args[0]['#theme']) && count($info['arguments']) > 1) {
$new_args = array();
foreach ($info['arguments'] as $name => $default) {
$new_args[] = isset($args[0]["#$name"]) ? $args[0]["#$name"] : $default;
}
$args = $new_args;
}
$output = call_user_func_array($info['function'], $args);
}
}
......@@ -1658,25 +1669,6 @@ function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attribu
return $output;
}
/**
* Return a themed list of items from a drupal_render() style array.
*
* @param $elements
* An array consisting of the following keys:
* - #items: an array of items as expected by theme('item_list').
* - #title: a title which prints above the list.
* - #list_type: the type of list to return. Defaults to "ul".
* - #attributes: an array of attributes as expected by theme('item_list').
* @return
* A string containing the list output.
*/
function theme_list($elements) {
// Populate any missing array elements with their defaults.
$elements += element_info('list');
return theme('item_list', $elements['#items'], $elements['#title'], $elements['#list_type'], $elements['#attributes']);
}
/**
* Returns code that emits the 'more help'-link.
*/
......
......@@ -99,8 +99,9 @@ function blog_node_view($node, $build_mode = 'full') {
'attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => $node->name))),
);
$node->content['links']['blog'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
}
}
......
......@@ -25,7 +25,7 @@ function blog_page_user($account) {
$build['blog_actions'] = array(
'#items' => $items,
'#theme' => 'list',
'#theme' => 'item_list',
'#weight' => -1,
);
......@@ -74,7 +74,7 @@ function blog_page_last() {
$items[] = l(t('Create new blog entry.'), "node/add/blog");
$build['blog_actions'] = array(
'#items' => $items,
'#theme' => 'list',
'#theme' => 'item_list',
'#weight' => -1,
);
}
......
......@@ -89,8 +89,9 @@ function book_node_view_link($node, $build_mode) {
if (!empty($links)) {
$node->content['links']['book'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
}
}
......
......@@ -528,8 +528,9 @@ function comment_node_view($node, $build_mode) {
}
$node->content['links']['comment'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
// Append the list of comments to $node->content for node detail pages.
......
......@@ -106,9 +106,6 @@ function node_theme() {
'node_admin_overview' => array(
'arguments' => array('name' => NULL, 'type' => NULL),
),
'node_links' => array(
'arguments' => array('element' => NULL),
),
);
}
......
......@@ -587,6 +587,10 @@ class DrupalRenderUnitTestCase extends DrupalWebTestCase {
);
}
function setUp() {
parent::setUp('common_test');
}
/**
* Test sorting by weight.
*/
......@@ -637,6 +641,24 @@ class DrupalRenderUnitTestCase extends DrupalWebTestCase {
// The elements should appear in output in the same order as the array.
$this->assertTrue(strpos($output, $second) < strpos($output, $first), t('Elements were not sorted.'));
}
/**
* Test passing arguments to the theme function.
*/
function testDrupalRenderThemeArguments() {
$element = array(
'#theme' => 'common_test_foo',
);
// Test that defaults work.
$this->assertEqual(drupal_render($element), 'foobar', 'Defaults work');
dd(drupal_render($e), 'defaults'); $element = array(
'#theme' => 'common_test_foo',
'#foo' => $this->randomName(),
'#bar' => $this->randomName(),
);
// Test that passing arguments to the theme function works.
$this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
}
}
/**
......
; $Id$
name = "Common Test"
description = "Support module for Common tests."
package = Testing
version = VERSION
core = 7.x
files[] = common_test.module
hidden = TRUE
<?php
// $Id$
/**
* @file
* Helper module for the Common tests.
*/
/**
* Implement hook_theme().
*/
function common_test_theme() {
return array(
'common_test_foo' => array(
'arguments' => array('foo' => 'foo', 'bar' => 'bar'),
),
);
}
/**
* Theme function for testing drupal_render() theming.
*/
function theme_common_test_foo($foo, $bar) {
return $foo . $bar;
}
......@@ -114,8 +114,9 @@ function statistics_node_view($node, $build_mode) {
}
$node->content['links']['statistics'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
}
}
......
......@@ -117,8 +117,9 @@ function taxonomy_node_view($node, $build_mode) {
}
$node->content['links']['terms'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
'#sorted' => TRUE,
);
}
......
/* $Id$ */
#tracker td.replies {
.page-tracker td.replies {
text-align: center;
}
#tracker table {
.page-tracker table {
width: 100%;
}
......@@ -11,11 +11,6 @@
* Menu callback. Prints a listing of active nodes on the site.
*/
function tracker_page($account = NULL, $set_title = FALSE) {
// Add CSS
drupal_add_css(drupal_get_path('module', 'tracker') . '/tracker.css', array('preprocess' => FALSE));
$header = array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last updated'));
// TODO: These queries are very expensive, see http://drupal.org/node/105639
$query = db_select('node', 'n', array('target' => 'slave'))->extend('PagerDefault');
$query->join('users', 'u', 'n.uid = u.uid');
......@@ -73,10 +68,18 @@ function tracker_page($account = NULL, $set_title = FALSE) {
$rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
}
$output = '<div id="tracker">';
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL);
$output .= '</div>';
$page['tracker'] = array(
'#rows' => $rows,
'#header' => array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last updated')),
'#theme' => 'table',
'#attached_css' => array(drupal_get_path('module', 'tracker') . '/tracker.css' => array('preprocess' => FALSE)),
);
$page['pager'] = array(
'#theme' => 'pager',
'#quantity' => 25,
'#weight' => 10,
);
$page['#sorted'] = TRUE;
return $output;
return $page;
}
......@@ -181,8 +181,9 @@ function translation_node_view($node, $build_mode) {
'attributes' => array('title' => $translations[$langcode]->title, 'class' => 'translation-link')
);
$node->content['links']['translation'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
}
}
......
......@@ -60,11 +60,11 @@ function upload_perm() {
*/
function upload_node_links($node, $build_mode) {
$links = array();
// Display a link with the number of attachments
$num_files = 0;
foreach ($node->files as $file) {
if ($file->list) {
if ((object)$file->list) {
$num_files++;
}
}
......@@ -76,8 +76,9 @@ function upload_node_links($node, $build_mode) {
'fragment' => 'attachments'
);
$node->content['links']['upload_attachments'] = array(
'#type' => 'node_links',
'#value' => $links,
'#theme' => 'links',
'#links' => $links,
'#attributes' => array('class' => 'links inline'),
);
}
}
......
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