Commit 7f88540b authored by webchick's avatar webchick

#564632 by moshe weitzman: Unify various _build() functions and remove duplicate copy of content.

parent 66317fbe
......@@ -1061,7 +1061,7 @@ function book_export_traverse($tree, $visit_func) {
* The HTML generated for the given node.
*/
function book_node_export($node, $children = '') {
$node = node_build_content($node, 'print');
node_build_content($node, 'print');
$node->rendered = drupal_render($node->content);
return theme('book_node_export_html', $node, $children);
......
......@@ -767,9 +767,13 @@ function comment_prepare_thread(&$comments) {
*/
function comment_build($comment, $build_mode = 'full') {
$node = node_load($comment->nid);
$comment = comment_build_content($comment, $build_mode);
// Populate $comment->content with a render() array.
comment_build_content($comment, $build_mode);
$build = $comment->content;
// We don't need duplicate rendering info in comment->content.
unset($comment->content);
$build += array(
'#theme' => 'comment',
......@@ -812,9 +816,6 @@ function comment_build($comment, $build_mode = 'full') {
* A comment object.
* @param $build_mode
* Build mode, e.g. 'full', 'teaser'...
* @return
* A structured array containing the individual elements
* of the comment's content.
*/
function comment_build_content($comment, $build_mode = 'full') {
if (empty($comment->content)) {
......@@ -841,8 +842,6 @@ function comment_build_content($comment, $build_mode = 'full') {
// Allow modules to modify the structured comment.
drupal_alter('comment_build', $comment, $build_mode);
return $comment;
}
/**
......@@ -2084,13 +2083,18 @@ function template_preprocess_comment(&$variables) {
$variables['comment'] = $comment;
$variables['node'] = node_load($comment->nid);
$variables['author'] = theme('username', $comment);
$variables['content'] = $comment->content;
$variables['date'] = format_date($comment->timestamp);
$variables['new'] = !empty($comment->new) ? t('new') : '';
$variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '';
$variables['signature'] = $comment->signature;
$variables['title'] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => "comment-$comment->cid"));
$variables['template_files'][] = 'comment-' . $variables['node']->type;
// Helpful $content variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Set status to a string representation of comment->status.
if (isset($comment->in_preview)) {
$variables['status'] = 'comment-preview';
......
......@@ -1030,9 +1030,13 @@ function node_delete_multiple($nids) {
function node_build($node, $build_mode = 'full') {
$node = (object)$node;
$node = node_build_content($node, $build_mode);
// Populate $node->content with a render() array.
node_build_content($node, $build_mode);
$build = $node->content;
// We don't need duplicate rendering info in node->content.
unset($node->content);
$build += array(
'#theme' => 'node',
'#node' => $node,
......@@ -1066,9 +1070,6 @@ function node_build($node, $build_mode = 'full') {
* @param $build_mode
* Build mode, e.g. 'full', 'teaser'...
*
* @return
* A structured array containing the individual elements
* of the node's content.
*/
function node_build_content($node, $build_mode = 'full') {
// The 'view' hook can be implemented to overwrite the default function
......@@ -1104,8 +1105,6 @@ function node_build_content($node, $build_mode = 'full') {
// Allow modules to modify the structured node.
drupal_alter('node_build', $node, $build_mode);
return $node;
}
/**
......@@ -1163,9 +1162,14 @@ function template_preprocess_node(&$variables) {
// Flatten the node object's member fields.
$variables = array_merge((array)$node, $variables);
// Helpful $content variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Make the field variables available with the appropriate language.
field_attach_preprocess('node', $node, $node->content, $variables);
field_attach_preprocess('node', $node, $variables['content'], $variables);
// Display post information only on certain node types.
if (variable_get('node_submitted_' . $node->type, TRUE)) {
......@@ -1382,7 +1386,7 @@ function node_search_execute($keys = NULL) {
foreach ($find as $item) {
// Render the node.
$node = node_load($item->sid);
$node = node_build_content($node, 'search_result');
node_build_content($node, 'search_result');
$node->rendered = drupal_render($node->content);
// Fetch comments for snippet.
......@@ -1841,7 +1845,7 @@ function node_feed($nids = FALSE, $channel = array()) {
// The node gets built and modules add to or modify $node->rss_elements
// and $node->rss_namespaces.
$node = node_build_content($node, 'rss');
node_build_content($node, 'rss');
if (!empty($node->rss_namespaces)) {
$namespaces = array_merge($namespaces, $node->rss_namespaces);
......@@ -1985,7 +1989,7 @@ function _node_index_node($node) {
variable_set('node_cron_last', $node->changed);
// Render the node.
$node = node_build_content($node, 'search_index');
node_build_content($node, 'search_index');
$node->rendered = drupal_render($node->content);
$text = '<h1>' . check_plain($node->title) . '</h1>' . $node->rendered;
......
......@@ -88,7 +88,7 @@ class OpenIDFunctionalTest extends DrupalWebTestCase {
// Submit form to the OpenID Provider Endpoint.
$this->drupalPost(NULL, array(), t('Send'));
$this->assertText(t('My account'), t('User was logged in.'));
$this->assertText($this->web_user->name, t('User was logged in.'));
}
/**
......@@ -150,7 +150,7 @@ class OpenIDFunctionalTest extends DrupalWebTestCase {
// so the form is submitted manually instead.
$this->assertRaw('<script type="text/javascript">document.getElementById("openid-redirect-form").submit();</script>', t('JavaScript form submission found.'));
$this->drupalPost(NULL, array(), t('Send'));
$this->assertText(t('My account'), t('User was logged in.'));
$this->assertText('johndoe', t('User was logged in.'));
$user = user_load_by_name('johndoe');
$this->assertTrue($user, t('User was found.'));
......
......@@ -179,7 +179,7 @@ function hook_search_execute($keys = NULL) {
foreach ($find as $item) {
// Build the node body.
$node = node_load($item->sid);
$node = node_build_content($node, 'search_result');
node_build_content($node, 'search_result');
$node->body = drupal_render($node->content);
// Fetch comments for snippet.
......@@ -265,7 +265,7 @@ function hook_update_index() {
variable_set('node_cron_last', $node->changed);
// Render the node.
$node = node_build_content($node, 'search_index');
node_build_content($node, 'search_index');
$node->rendered = drupal_render($node->content);
$text = '<h1>' . check_plain($node->title) . '</h1>' . $node->rendered;
......
......@@ -1371,7 +1371,7 @@ function user_menu() {
'title' => 'My account',
'title callback' => 'user_page_title',
'title arguments' => array(1),
'page callback' => 'user_view',
'page callback' => 'user_build',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
......@@ -1502,12 +1502,9 @@ function user_uid_optional_to_arg($arg) {
}
/**
* Menu item title callback - use the user name if it's not the current user.
* Menu item title callback - use the user name.
*/
function user_page_title($account) {
if ($account->uid == $GLOBALS['user']->uid) {
return t('My account');
}
return $account->name;
}
......@@ -2068,23 +2065,61 @@ function _user_cancel($edit, $account, $method) {
}
/**
* Builds a structured array representing the profile content.
* Generate an array for rendering the given user.
*
* When viewing a user profile, the $page array contains:
*
* - $page['content']['Profile Category']:
* Profile categories keyed by their human-readable names.
* - $page['content']['Profile Category']['profile_machine_name']:
* Profile fields keyed by their machine-readable names.
* - $page['content']['user_picture']:
* User's rendered picture.
* - $page['content']['summary']:
* Contains the default "History" profile data for a user.
* - $page['content']['#account']:
* The user account of the profile being viewed.
*
* To theme user profiles, copy modules/user/user-profile.tpl.php
* to your theme directory, and edit it as instructed in that file's comments.
*
* @param $account
* A user object.
*
* @return
* A structured array containing the individual elements of the profile.
* An array as expected by drupal_render().
*/
function user_build($account) {
// Retrieve all profile fields and attach to $account->content.
user_build_content($account);
$build = $account->content;
// We don't need duplicate rendering info in account->content.
unset($account->content);
$build += array(
'#theme' => 'user_profile',
'#account' => $account,
);
return $build;
}
/**
* Builds a structured array representing the profile content.
*
* @param $account
* A user object.
*
*/
function user_build_content($account) {
$account->content = array();
// Build fields content.
// TODO D7 : figure out where exactly this needs to go
$account->content += field_attach_view('user', $account);
// Populate $account->content with a render() array.
module_invoke_all('user_view', $account);
return $account->content;
}
/**
......
......@@ -159,39 +159,6 @@ function user_logout() {
drupal_goto();
}
/**
* Menu callback; Displays a user or user profile page.
*
* The $page['content'] array for user profile pages contains:
*
* - $page['content']['Profile Category']:
* Profile categories keyed by their human-readable names.
* - $page['content']['Profile Category']['profile_machine_name']:
* Profile fields keyed by their machine-readable names.
* - $page['content']['user_picture']:
* User's rendered picture.
* - $page['content']['summary']:
* Contains the default "History" profile data for a user.
* - $page['content']['#account']:
* The user account of the profile being viewed.
*
* To theme user profiles, copy modules/user/user-profile.tpl.php
* to your theme directory, and edit it as instructed in that file's comments.
*/
function user_view($account) {
drupal_set_title($account->name);
// Retrieve all profile fields and attach to $account->content.
user_build_content($account);
$build = $account->content;
$build += array(
'#theme' => 'user_profile',
'#account' => $account,
);
return $build;
}
/**
* Process variables for user-profile.tpl.php.
*
......@@ -202,7 +169,10 @@ function user_view($account) {
*/
function template_preprocess_user_profile(&$variables) {
$account = $variables['elements']['#account'];
$variables['user_profile'] = $account->content;
// Helpful $user_profile variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['user_profile'][$key] = $variables['elements'][$key];
}
}
/**
......
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