Saturday, May 18 2013
PHP
How to print fields on a NODE TPL (node-video.tpl.php, for example)
Drupal 6
Some examples:
<?php print $title; ?>
<a href="<?php print $node_url ?>"><?php print $title; ?></a>
<?php if (!empty($field_news_image)) {
print $field_news_image['0']['view'];
} ?>
<\div class="submitted">
<?php print format_date($node->created, 'custom', 'j F Y'); ?>
</div>
<\div class="field venue">
<?php print l(($node->field_venue[0]['value']), $node->field_venue_url[0]['value']); ?>
</div>
<\div class="field btn-tickets ticket-link">
<?php
if($node->field_sold_out[0]['Yes']){
print t('Sold out'); }
else {
print l(t('Tickets'), $node->field_ticket_sales_url[0]['value'], array('class' => 'buy-tickets')); }
?>
</div><?php print $title; ?>
<a href="<?php print $node_url ?>"><?php print $title; ?></a>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Print the complete CCK Link field:
<?php print $node->field_listing_website[0]['view'] ?>
Or separate the URL from the title:
<?php
$link_url = $node->field_link[0]['url'];
$link_title = $node->field_link[0]['title'];
?>
<div<?php print $node_attributes; ?>>
<?php if (!empty($link_url)) : ?>
<a href="<?php print ($link_url); ?>">
<?php endif; ?>
<\h2 class="node-title"><?php print $title; ?><\/h2>
<?php print $content; ?>
<?php if (!empty($link_url)) : ?>
</a>
<?php endif; ?>
</div>
This is also explained pretty well here: http://www.caucusllc.com/blog/theming-a-cck-node
How to print fields on a views tpl
Drupal core fields:
<?php print $fields['title']->content; ?>
<?php print $fields['body']->content; ?>
<?php print $fields['nid']->content; ?>
Print the teaser:
<?php print $fields['teaser']->content; ?>
CCK fields:
<?php if ($fields['field_location_value']): ?>
<?php print $fields['field_image_fid']->content; ?>
If a field is optional, you might want to say "if field is not empty", like this:
<?php if (!empty($fields['field_image_fid']->content)) {
print $fields['field_image_fid']->content; }
?>
Especially if you want to add a div or h2 around the print, like this:
<?php if ($fields['field_location_value']): ?>
<div class="sample"><?php print $fields['field_location_value']->content; ?></div>
<?php endif; ?>
<?php if ($fields['field_venue_value']): ?>
<<div class="sample">><?php print $fields['field_venue_value']->content; ?></div>
<?php endif; ?>
Finally, here's a great tutorial on views row theming from the good folks at Mustardseed Media [source]

