Where are $content, $sidebar-left, $sidebar-right, etc. defined in Drupal?
To further my understanding of Drupal, I am trying to discover where the variables that are used in .tpl.php files, $content,$sidebar-left, etc. are defined in Drupal.
I have spent hours searching through Drupal code and still have not been able to find this. I am looking only for an answer to this question. Please do not sidetrack the question by telling me why I don't need to know this, or what I should be doing, or don't hack the Drupal core, etc. I have already seen it.
In my situation, I am dealing with a custom node, business, that is created by openresort/ecommerce. The $content variable is a composite containing several bits of information, and I need to customize this information and change the order in which it is displayed. Understanding how this information gets packaged into the $content variable seems like something that could help.

theme_regions
I use the following code in my D5 sites to define new regions. It calls the default 5 from phptemplate engine, and adds the new ones:
<?php
/**
* Merges the native phptemplate regions with our own regions
* to create special front page regions.
*
* @return array Theme region blocks
*/
function caignwebs_v5_regions() {
return array_merge(
phptemplate_regions(),
array(
'grey_footer' => t('grey footer'),
'front_left_panels' => t('front page left panels'),
'front_right_panels' => t('front page right panels'),
)
);
}
?>
Just insert it into your themes template.php file.
See http://api.drupal.org/api/function/phptemplate_regions/5
Hope this helps.
Alan Davison
www.caignwebs.com.au
I do not want to define a
I do not want to define a new region. I want to know where Drupal defines the variables used in the *.tpl.php files such as $content, $sidebar_left, etc.
These are regions!!
These are regions defined in theme_regions, called via system_region_list.
There are multiple variables here, did you mean the non-region variables?
Have a look at _phptemplate_variables, this adds number variables to page.
This is called via the hook_page, phptemplate_page that overrides Drupals core theme_page (see phptemplate.engine)
When doing searches on Drupal code, make sure you search *.module, *.install, *.engine, *.inc and *.php.
Alan Davison
www.caignwebs.com.au
Thanks Alan, What I am
Thanks Alan,
What I am looking for is $content that is used in the fourth line of the following excerpt from the page.tpl.php file in the Nirvana theme
<?phpprint $messages;
?>
<?phpprint $help;
?>
<?phpprint $content;
?>
<?phpif ($feed_icons) {
?>
<?phpprint $feed_icons;
?>
<?php}
?>
In my case, this $content variable contains multiple things, including a complete address, business information, and amenities list. I would like to modify what information is presented, and its order. I am trying to find where this $content variable is defined or created, or where these diverse elements are packaged together into the $content variable.
Actually what I am looking for is a more general answer. All themes based on phptemplate apparently have .tpl.php files that contain similar variables that contain all of the content for the main region or a sidebar region, and these variables typically have names such as $content or $sidebar_left. Where in Drupal do these variables get defined, created, or packaged? Does it happen in module code? Does it happen some place that I am missing in the theme folder? Does it happen somewhere in the core? I am just trying to understand how this part of Drupal works.
No short answer for $content
Some background....
The CMS can be thought of as 3 main areas (very simplistic view), the theme, regions and content. Content is a special region that is made up of the content returned by the menu item, plus any defined blocks for the content region.
The default front page $content is loaded by the menu 'node', via node_page_default, while a node/* is generated via node_view(node_id). "admin" is loaded by another and so forth.
The template functions I gave earlier set other variables, many common in different templates, depending on if it is a page view, block, node view, etc, etc.....
Each part of the page is made from a different template or theming function, each with it's own variables. These get nested, up to 7 or more layers deep in some complex cases. For example, theme_form_item wraps an individual form element, each of these have their own theming function to define the inner form part, like textarea.
However "complete address, business information, and amenities list" ... suggests a CCK node? Then have a look into CCK and node templates, just browse the docs to get a few weeks of reading.
If it is a CCK node, there should be some weight fields. These control the ordering. (look under Admin > Content > Content types > XXX > Edit Fields) The bigger the number, the deeper it "sinks" down the page. The display tab lets you modify some of these CCK fields, like how a link is displayed, etc.
Drupal can have a steep learning curve, just pick one thing and research this first, but you will be surprised at how quickly you pick things up.
One last thing, many variables are defined as arrays, eg: $vars['left_sidebar'] and these get extracted into the variable name space using extract, see http://www.php.net/extract for details.
Alan Davison
www.caignwebs.com.au
Thanks Alan. That helps, and
Thanks Alan. That helps, and confirms what I was already realizing, that it is a complicated mess. Fortunately, I found that a lot of the content is wrapped in 's that have classes with no style definition, that appear to be unique to that location, so I was able to add some styles to the stylesheet to take advantage of those (dummy?) classes to do most of what I wanted. I just hope the classes really are unique to that location and will not not blow up anything else.
It's all in phptemplate.engine
In Drupal 5.x, the answers you seek are in your theme's engine, PHPTemplate, which is located at
/themes/engines/phptemplate/phptemplate.engine. The engine sets up a number of default variables -- $content, $sidebar-left, $sidebar-right, $footer-message, etc. -- before it ever reaches your theme.Take a close look at the function called
phptemplate_page(). This is the function that, among other things, determines whichpage.tpl.phpfile to use and creates a number of variables for thatpage.tpl.phpfile to print. I will excerpt some of the more important parts of that rather large function:<?php
// Load blocks early for adding header info
foreach ($regions as $region) {
// Sidebar_indicator tells the block counting code
// to count sidebars separately.
if ($region == 'left' || $region == 'right') {
$sidebar_indicator = $region;
}
else {
$sidebar_indicator = NULL;
}
$variables['regions'][$region] = theme('blocks', $region);
}
$sidebar_indicator = NULL;
$sidebar_left = $variables['regions']['left'];
if ($sidebar_left != '') {
$layout = 'left';
}
$sidebar_right = $variables['regions']['right'];
if ($sidebar_right != '') {
$layout = ($layout == 'left') ? 'both' : 'right';
}
?>
In this case, "left" (
$variables['regions']['left']) and "right" ($variables['regions']['right']) are called using the region names defined at the engine level inphptemplate_regions(). These region names -- "left" and "right" -- are special to the engine. That's why most themes (should) use the names "left" and "right" in their ownmytheme_regions()override function.The code above is where $sidebar_left and $sidebar_right are defined in that function. Here's how they get added to the $variables array, which is responsible for the variables you see in page.tpl.php: $tabs, $title, $styles, and so forth:
<?php$variables = array_merge($variables, array(
'base_path' => base_path(),
'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()),
'closure' => theme('closure'),
'content' => $content,
'feed_icons' => drupal_get_feeds(),
'footer_message' => filter_xss_admin(variable_get('site_footer', FALSE)) . "\n" . $variables['regions']['footer'],
'head' => drupal_get_html_head(),
'head_title' => implode(' | ', $head_title),
'help' => theme('help'),
'language' => $GLOBALS['locale'],
'layout' => $layout,
'logo' => theme_get_setting('logo'),
'messages' => theme('status_messages'),
'mission' => isset($mission) ? $mission : '',
'primary_links' => menu_primary_links(),
'search_box' => (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : ''),
'secondary_links' => menu_secondary_links(),
'sidebar_left' => $sidebar_left,
'sidebar_right' => $sidebar_right,
'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
'css' => drupal_add_css(),
'styles' => drupal_get_css(),
'scripts' => drupal_get_js(),
'tabs' => theme('menu_local_tasks'),
'title' => drupal_get_title()
));
?>
Hope that helps.
Todd Ross Nienkerk
Co-founder, Four Kitchens
$content
I know this is long overdue. I'll just post my answer to your question in case you haven't found the answer yet.
$content is defined in node.module using drupal_render().
$content found
Hey there jeuelc thank you so much. I was in the same shoes as poor gavacho,
Now I can sleep tonight and eagerly find some of the real root knowledge of
drupal tomorrow. I just got to do some first PHP snips and the whole search for
the elusive assignment of $content was on. Thought I checked all files with text search
using Total Commander. Now TC finds it ...maybe did search wrong.
the Portal
This answer could lead people
This answer could lead people down the wrong path. The URL (http://mysite.com/menu-link) is translated by Drupal into a menu callback. It is this callback that generates the $content. Only in the case of 'node/123', is the menu callback the node view callback, which jeuelc is correct in saying that it is defined in the node.module using drupal_render().
Eg: the definition of the menu callback in the node module:
<?php$items[] = array('path' => 'node/'. arg(1), 'title' => t('View'),
'callback' => 'node_page_view',
'callback arguments' => array($node),
'access' => node_access('view', $node),
'type' => MENU_CALLBACK);
?>
There are about 100+ other menu callbacks in the core modules of Drupal!!!
The $content variable is then passed into the page theming layer using the call theme('page') in index.php. This loads up the blocks, page titles, logos, etc, that make up the rest of the page.
When looking for what is generating the page, searching for the static bits of the menu link is often the best way to find these:
Eg: For the forums, the 'forum' makes up the static bit, defined in forum.module
<?php$items[] = array('path' => 'forum',
'title' => t('Forums'),
'callback' => 'forum_page',
'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM);
?>
but I generally find searching for some static HTML in the content or source code even quicker when searching for the menu item that is generating the page. A strange class name, or part of, always generates a match pretty quickly! This is usually in a theming function which you can quickly override and get your work done.
Alan Davison
www.caignwebs.com.au
Nice job Alan . I am glad you
Nice job Alan . I am glad you came back to this topic to add your wisdom and
insights. I am aiming to do web site design for a living but am quite green. Today
I thought it time to have a go at the code files and learn the underlying php code.
Must say I feel intimidated but am going ahead despite it.
I see what you are saying and that there is more to it than:
"$content right here: ->$content!"So I shall return to your notes here between code lessons to gather up better
the ideas you express.
-cheers
the Portal