1.20.x node.module | node_build_content(Node $node, $view_mode = 'full', $langcode = NULL) |
Builds a structured array representing the node's content.
The content built for the node (field values, comments, file attachments or other node components) will vary depending on the $view_mode parameter.
Backdrop core defines the following display modes for nodes, with the following default use cases:
- full (default): node is being displayed on its own page (node/123)
- teaser: node is being displayed on the default home page listing, or on taxonomy listing pages.
- rss: node displayed in an RSS feed.
If search.module is enabled:
- search_index: node is being indexed for search.
- search_result: node is being displayed as a search result.
If book.module is enabled:
- print: node is being displayed in print-friendly mode.
Contributed modules might define additional display modes, or use existing display modes in additional contexts.
Parameters
Node $node: A node entity.
$view_mode: (optional) Display mode, e.g. 'full' or 'teaser'. Defaults to 'full'.
$langcode: (optional) A language code to use for rendering. Defaults to the global content language of the current request.
File
- modules/
node/ node.module, line 1183 - The core module that allows content to be submitted to the site.
Code
function node_build_content(Node $node, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->langcode;
}
// Remove previously built content, if exists.
$node->content = array();
// Allow modules to change the display mode.
$view_mode = key(entity_view_mode_prepare('node', array($node->nid => $node), $view_mode, $langcode));
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (node_hook($node, 'view')) {
$node = node_invoke($node, 'view', $view_mode, $langcode);
}
// Build fields content.
// In case of a multiple view, node_view_multiple() already ran the
// 'prepare_view' step. An internal flag prevents the operation from running
// twice.
field_attach_prepare_view('node', array($node->nid => $node), $view_mode, $langcode);
entity_prepare_view('node', array($node->nid => $node));
$node->content += field_attach_view('node', $node, $view_mode, $langcode);
// Always display a read more link on teasers because we have no way
// to know when a teaser view is different than a full view.
$links = array();
$node->content['links'] = array(
'#theme' => 'links__node',
'#pre_render' => array('backdrop_pre_render_links'),
'#attributes' => array('class' => array('links', 'inline')),
);
if ($view_mode == 'teaser') {
$node_title_stripped = strip_tags($node->title);
$links['node-readmore'] = array(
'title' => t('Read more<span class="element-invisible"> about @title</span>', array('@title' => $node_title_stripped)),
'href' => 'node/' . $node->nid,
'html' => TRUE,
'attributes' => array('rel' => 'tag', 'title' => $node_title_stripped),
);
}
$node->content['links']['node'] = array(
'#theme' => 'links__node__node',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
// Allow modules to make their own additions to the node.
module_invoke_all('node_view', $node, $view_mode, $langcode);
module_invoke_all('entity_view', $node, 'node', $view_mode, $langcode);
// Make sure the current display mode is stored if no module has already
// populated the related key.
$node->content += array('#view_mode' => $view_mode);
}