With Drupal 7, the node type "article" comes with a field_image to upload an image. When I create a "node--article.tpl.php" and remove print render($content); I can display the body texte with this <?php print $node->body['fr'][0]['safe_value'] ?> but how can I display the field_image?

That should be a simple thing to do but I'm looking for that solution for the past three weeks and the module contemplate didn't help.

Thank you,

Comments

Jeff Burnz’s picture

Drupal 7 introduces granular theming and renderable arrays which is a whole different ball game to Drupal 6.

So, instead of removing print render($content); we use the hide() function to remove the field from $content, and then the render() function to print our field somewhere else in the template.

For example lets assume we have an image field called field_image_captures:

First hide the field to prevent it printing out in $content:

  hide($content['field_image_captures']);
  print render($content);

The we use the render() function and the field name to print the field somewhere else:

print render($content['field_image_captures']);

This is called "granular theming", because you can choose to hide just one or two fields, and print the rest of them in $content without having to remove the entire $content variable just to modify a couple of fields (the big pita of D6).

You can still use those hairy variables (aka $node->body['fr'][0]['safe_value'] bla bla bla...) to print fields, such as what Contemplate might show you, but you will do better to use Devel and dig into the $content array, for example if you have Devel module enabled you can simply place <?php dsm($node); ?> in the template to print out the node object in Krumo.

Swebmas’s picture

That's a great answer Jeff, and you also gave me something the improve my way of theming. Thank you very much.

rbp’s picture

This helped me tremendously. Thanks!

cityemp’s picture

That was really helpful. I am trying to print a field above the content. Let me be more clear. I am creating a news release - the format requires

My company name //field 1
News Release //field 2

Title

Address Line 1 //field 3
Address Line 2 //field 4

Body

I can hide fields 1 to 4 using hide($content['field_1']); format and render the content. But I need these fields placed before the content - in this case before the news body.

Further I have hidden the title at the top and that also needs to be placed above the rendered content in this format.

Any ideas?

Thanks!

cityemp’s picture

Know what, once I asked in the forum, I figured the answer myself . Here is what I did -

				hide($content['title']);
				hide($content);
		   

print render($content['field_news_caption1']);
print render($content['field_news_caption2']);
print render($content['field_address1']);
print render($content['field_address2']);
print render($content['title']);
print render($content['body']);

Now I have got everything to display as I wanted but for the title. Does title requires a special treatment to be displayed.

Help!

sachbearbeiter’s picture

subscribe

hansyg’s picture

Title is not part of the $content array so to access/print it just use

<?php print $title; ?>

HTH
Hans

catchlight’s picture

Great stuff. This is all a little new to me, so it's become a bit clearer because of this thread.

Regarding node page titles. I have a template "page--front.tpl.php". Can I use hide() to stop drupal from rendering the title of the page? If so how?

alexmoreno’s picture

catchlight, simply don't put the print $title :-)

digibrill’s picture

Hey Jeff, this is a great post, but what if your template does not have the $content variable? I have created two templates using page.tpl.php as a base file and that are made especially for two new content types I have created. I do know that I was able to dump out the $node and $page variables, but why won't your direction work? All I can do with those two variables is echo/var_dump the whole thing, not the specific fields I need. When you have a minute to help, thanks!

digibrill tic

vmevada102’s picture

I had a problem with the same code in print.tpl.php

The field field_student_photo render uising the same code explained by you, but in theme Creative Responsive Theme 7.x-1.1 would not allow me to print the same field in the print--html-nodetype.tpl.php

I had used several other methods to display the same field in the print template, but not succeeded to show the image in print page.

digibrill’s picture

What I meant is that I cannot echo/print the parts of the $content var I need - because they do not exist.

digibrill tic

tal.luder’s picture

Took me hours to understand why render() didn't work with my fields...

When using the module Fieldgroup to group custom fields dont forget to prefix $content with the group and the field then.

Example: having a fieldgroup group_fields_1 and inside a field named field_text_1 the right code to use in your node--your-content-type.tpl.php is:

<?php print render($content["group_fields_1"]["field_text_1"]); ?>

You have to eventually hide $content before with <?php hide($content); ?> or only your fieldgroup.

Hope it can help.

Jeff Burnz’s picture

Yeah, so true, the field group arrays are pretty massive, if you kpr($content) (requires devel module) you can more easily dig into them.