Using drupal 6 I had chosen contemplate module for theming a contenttype, I also thought it was the right thing.
But the problem was the need to have another place where to storetheming files, or put it in db, that is a practice I do not like.
Also there could be a number of Views in a website, with differentvisualization, and choosing field view mode for a view is something Ido not like, because there is a better way, in my opinion.
My practice is to use custom view modes, (module Entity view modesmake it easy to define new view modes), and then customize i themefile. So in every view choose to visualize entity with the chosen viewmode. (This does not work for Views Bulk Operation, though)
Every view modes could be setted up to have its own template in theme,this way:
template.php:
function THEMENAME_preprocess_node(&$variables, $hook) {
$variables['theme_hook_suggestions'][] = 'node__'.$variables['node']->type;
$variables['theme_hook_suggestions'][] = 'node__'.$variables['node']->type.'__'.$variables['view_mode'];
}
And in templates/node--type--mode.tpl.php visualizzation can becustomized for every view mode.
There is a problem when one needs to show different formatting optionfor a single field depending in entity (node) content, tipically otherfield's values.
$style = 'shrinked';
if($node->field_name ...) $style='shrinked';
$v = field_view_field('node',$node,'field_image',
array(
'type' => 'image',
'label'=> 'hidden',// inline, above
'settings'=>array(
'image_style'=> $style,
'image_link'=> 'content',
),
));
if($v) print render($v);
and so using this to render various field, or use
$image = field_get_items('node', $node, 'field_image');
$output = field_view_value('node', $node, 'field_image', $image[0], array(
'type' => 'image',
'settings' => array(
'image_style' => 'thumbnail',
'image_link' => 'content',
),
));
(refer to http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way where I found this for another task tough)
Drupal site say don't use this like that.
In http://api.drupal.org/api/drupal/modules!field!field.module/function/fie... it said to use
render($content[FIELD_NAME])
that of course don't let you decide how to show content.
Instead hook_field_display_ENTITY_TYPE_alter() can make a number oftricks without writing html in template:
function THEMENAME_field_display_node_alter(&$display,$context) {
if($context['entity_type'] != 'node'
|| $context['entity']->type!='mycontetype') return;
if(isset($context['entity']->field_promotion) &&
$context['entity']->field_promotion['und'][0]['value']) {
if($context['field']['type'] == 'specialtype') {
global $user;
if($user->uid != 1 && $context['view_mode'] == 'teaser') {
// do not show special field in teaser mode for no main user
$display['type'] = 'hidden';
}
}
if($context['field']['field_name'] == 'field_image') {
if(isset($context['entity']->field_checked['und']) &&
$context['entity']->field_checked['und'][0]['value']) {
if($context['view_mode'] == 'teaser') {
$display['label'] = 'above';
$display['settings']['image_style'] = 'thumbnail';
// or also:
//$display['type'] = 'galleryformatter_default';
//$display['module'] = 'galleryformatter';
//$display['weight'] = 100;
}
return;
}
}
}
}
here could be moved the label, changed the type of formatter (has tobe done together with the corrisponding module), settings and weight,almost all what you can do in template file without puttingconditional code there. Also, from a performance prospective, usingcode in template is wrong because the field is already rendered a thattime. Calling field_view_field in template file is not the drupal way.
Also in the code above I wrote to do not show a field type 'specialtype' for most user in 'teaser' view mode, such a thing is very powerful.