At the next Drupal Group meeting, I will be discussing what I have learned so far about theming a view using the template.php file. I will explain how I was able to create a nice little table, and how I could not only control the formatting, but also where and when the data is displayed.
As a web programmer who knows php and SQL but is relatively new to Drupal, I have been feeling incapacitated when it comes to controlling my themes while trying to adhere to best practices. (if I could only write a SQL query in my template! aaargh!) Recently, with a hint from Barry at Advantage Labs, I was able to get past this hurdle, so I now have more control over my views themes. What I learned is based on a lesson at drupaldojo.com:
http://www.drupaldojo.com/lesson/theming-views-table
Date: Wednesday, October 24th
Advantage Labs' conference room at:
2104 Stevens Avenue South
Minneapolis, MN 55409
Comments
Meeting Notes
Thanks for the presentation! Here is the code Lila Karash presented - she may follow up with further comments!
the template.php file
<?php
function MY_THEME_regions() {
//add cropbox region
return array(
'right' => t('right sidebar'),
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer'),
'mailbox' => t('mailbox'),
'cropbox' => t('cropbox'), // the specific region created for the block in question
);
}
function MY_THEME_views_view_list_VIEWNAME($view, $nodes, $type) {
//themeName_views_view_viewType_viewName(.....)
global $user;
$lastmonth = ''; //initializie the variable that will store the most recent month
$output = '<table class="chart">'; //start building the table
//loop through the items in the view and build the table rows
foreach ($nodes as $node) {
$output .= '<tr><td id="month">';
//check to see if the month has already been printed
if ($lastmonth != $node->term_data_name) {
$output .= $node->term_data_name; //print the month
$lastmonth = $node->term_data_name; //set the month placeholder to the current month
} else {
$output .= ' '; //don't print the month (printed a space instead)
}
$output .= '</td>'; //end month column tag
$output .= '<td id="inseason">'; //begin season column tag
$output .= $node->node_title;
$output .= '</td>'; //end season column tag
// if it's the person editing the site, let them see a delete link (add one more column)
if ($user->uid == '2') {
$output .= '<td><a href="/node/' . $node->nid . '/delete">delete</a></td>';
}
$output .= '</tr>';
} $output .= '</table>';
//print_r($nodes); //uncomment this line to print an array of available data terms
return $output;
}
?>
print_r($nodes)
Array ( [0] => stdClass Object ( [nid] => 13 [term_data_weight] => 0 [term_data_name] => FEBRUARY [node_title] => Potatoes [node_changed] => 1187658963 [node_type] => crop [node_uid] => 1 ) [1] => stdClass Object ( [nid] => 8 [term_data_weight] => 4 [term_data_name] => JUNE [node_title] => Cauliflower [node_changed] => 1187657990 [node_type] => crop [node_uid] => 1 ) [2] => stdClass Object ( [nid] => 32 [term_data_weight] => 4 [term_data_name] => JUNE [node_title] => Broccoli [node_changed] => 1190079494 [node_type] => crop [node_uid] => 2 ) [3] => stdClass Object ( [nid] => 31 [term_data_weight] => 4 [term_data_name] => JUNE [node_title] => Carrots [node_changed] => 1190079414 [node_type] => crop [node_uid] => 1 ) [4] => stdClass Object ( [nid] => 14 [term_data_weight] => 5 [term_data_name] => JULY [node_title] => corn [node_changed] => 1187659014 [node_type] => crop [node_uid] => 1 ) [5] => stdClass Object ( [nid] => 12 [term_data_weight] => 6 [term_data_name] => AUGUST [node_title] => Apples [node_changed] => 1187658904 [node_type] => crop [node_uid] => 1 ) )
CSS style sheet
table.chart {
border-collapse: collapse;
border: 1px solid none #CC6633;
background: #FFFFE6;
padding: 0px;
}
table.chart td {
border: 1px solid #CC6633;
padding: 3px;
}
#month {
color: #CC6633;
font-family: Verdana, Arial, Sans-serif;
font-size: 10px;
font-weight: bold;
text-align: right;
padding-left: 15px;
padding-right: 3px;
}
#inseason {
color: #336666;
font-family: Verdana, Arial, Sans-serif;
font-weight: bold;
font-size: 10px;
text-align: left;
padding-right: 40px;
}
Credit goes to: EclipseGC
http://www.drupaldojo.com/lesson/theming-views-table
Drew Gorton
A further thought on usability, themes and functions...
There was a very brief discussion about permissions and ways to handle something like the 'delete' link in Lila's example, above. Ronan mentioned node_access as the wrapper you might want to use in such a case:
From the API: http://api.drupal.org/api/function/node_access/5
Drupal 6 has added an account parameter as well.
In any case, a re-written line might look something like this:
if ($user->uid == '2') {if ( node_access('delete', $node) ) {
Resulting in:
<?php
if ( node_access('delete', $node)) {
$output .= ( '<td><a href="/node/' . $node->nid . '/delete">delete</a></td>' );
}
?>
One theme snippet that can occasionally be nice from a UI standpoint is to do something like the following on the teaser view of a node:
<?php
if ( node_access('update', $node)) {
return ( '<div class="edit-link">'.l( t( "edit this»" ), 'node/'.$nid.'/edit' ).'</div>' );
}
?>
It's just a tiny little thing, but can make administering a site just a smidge easier -- and can help clarify the way things are edited - especially for newer users.
Also related is the user_access function - which will return permissions against the 'Access Control' settings. So, for example, you could do something like the following in your standard block.tpl.php theme file:
<?php
if( user_access('administer blocks') ) {
print( '<div class="edit-link">'.l( t( "edit this»" ), "admin/build/block/configure/$block->module/$block->delta" ).'</div>' );
}
?>
or - if you want to avoid the possible confusions/dangers from editing 'menu' or 'view' blocks:
<?php
if( user_access('administer blocks') && $block->module == 'block' ) {
print( '<div class="edit-link">'.l( t( "edit this»" ), "admin/build/block/configure/$block->module/$block->delta" ).'</div>' );
}
?>
Again - just a smidge of code that can help your users (or yourself) manage a site more easily. As I look at that code, it could be further generalized and abstracted, but anyway, it's a start....
Drew Gorton
Gorton Studios