Best practice to theming a Node by its type

Events happening in the community are now at Drupal community events on www.drupal.org.
strae's picture

Hi guys, my first post here ;)
My question is pretty simple but googlin around i didnt find anything that exactly answer me: what is the drupalish way to theme a node by its content-type?

For example, lets say that i added an image filed into my blog-entry nodes, and the default output for that field (both in the articles list and in the full node page) is:

Image_Label:<br />
<a href="$image_url">$image_name</a>

And my needs is to dont show the image field in the articles list, and to show it simply as an image <img src="$image_url" title="$image_name" /> in the full node page.

What is the best practice to fullfill this kind of issue?

In my first project, I used to deconstruct the $node variable into my node-blog.tpl.php file, now im thinking is better to using the theme_preprocess_node() function instead, this will keep my template files clean.

What do you guys think about?
Do wou know others way to do that?

Comments

That's what i've done

strae's picture

The theme_preprocess_node function in my template.php looks as follow:

function themename_preprocess_node(&$vars, $hook) {
    $node =$vars['node'];
    if($node->type == 'blog'){
        //im preprocessing a blog entry
        if(!$vars['page']){
            //we arent in articles page
            //i dont want the full links, just need the 'Read more' trimmed at 55 char max
            $link_title = t('Read full') . ' ' . $node->title . ' ' . t('article');
            if(strlen($node->title) > 55 ){
                $link_text = t('Read full') . ' <em>' . substr($node->title, 0, 52) . '...' . '</em> ' . t('article');
            }else{
                $link_text = t('Read full') . ' <em>' . $node->title . '</em> ' . t('article');
            }
            $my_custom_links = '<div class="read-more"><a href="' . $base_url . '/' . $node->path . '" title="' . $link_title . '">' . $link_text . ' &raquo;</a></div>';
            $node->content = $node->content['body']['#value'] . $my_custom_links;
        }else{
            //full node page, show the images
            $images = '';
            if(isset($node->field_showcase)){
                //the field i need is here
                foreach($node->field_showcase AS $image){
                    $images .= '<img src="' . $base_url . '/' . $image['filepath'] . '" /> ';
                }
            }
            $node->content = $node->content['body']['#value'] . $images;
        }
    }
}

Im a drupal newbie, did i some code-horror? ;)
Anyway, i think that (somewhare) there is a *.tpl.ph file for the custom node fields: but where is it, and how can i create another template for just 1 of my custom fields (lets say the custom field is named 'add_image')?

Why don't you change the

japanitrat's picture

Why don't you change the display settings for the teaser display of the node by making use of the administration pages for your content-type? You could easily hide the imagefields for teasers and show them in the full node view without a single line of code.

Mh.. i didnt find the options

strae's picture

Mh.. i didnt find the options you talk about anywhere.. where are they located?

you should find them under

japanitrat's picture

you should find them under [base-path]/admin/content/node-type/[name-of-your-content-type]/display
example: http://mydomain.com/admin/content/node-type/blog/display

PS: assuming you use Drupal6 + CCK

Youre right!

strae's picture

it works, i just didnt see the link!

Thanks man!
There is a way to configure in the same way the $links var too?

I need to allways show the 'read more' link in the article list, builded as read full <em>$node_title</em> article..

or i must edit the phptemplate_links() function?

Either you try out the

japanitrat's picture

Either you try out the ed_readmore module which allows you to modify this link in several ways, or you code it yourself by making use of the hook_link_alter($links, $node) function.

The second option does not work within your theme's template.php (as far as i know). You'll have to create a helper module (simply an .info file and the .module file) and put somthing like this in it:

function myhelpermodule_link_alter(&$links, $node){
  $links['readmore']['title'] = t('read full').' <em>'.check_plain($node->title).'</em> '.t('article');
}

Theme development

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: