phpdrupaldrupal-7drupal-themes

How to use individual fields inside template files of a Drupal theme


I'm new to Drupal and I'm creating an event management website with it. I'm using Drupal 7 and I have created a custom content type called "event" and I have written my own theme. I have a problem with theming the custom content type. I have built a template file called node--event.tpl.php inside my theme folder so that it is used to display every event in the website. My problem is that I don't know how to use individual fields and put them in the places I want. For example my event content type has an overview (text), image, date, and venue (getlocation field) and I want to use them individually in my template file so that I can put each one inside an HTML tag individually and not as a whole. I have searched Google but found nothing. I have used modules like Panel and Display Suite but I can't make what I want with them. Any solutions or hints would be appreciated a lot.

Regards, Shakib


Solution

  • The following document will show you how to get access to the fields:

    https://api.drupal.org/api/drupal/modules!node!node.tpl.php/7

    All this information is stored in the $node array. You can print the array to see it with print_r wrapped in html pre tags.

    Example for a field named authors:

    <?php $node->field_authors[0]['value'] ?> 
    

    Or

    <?php $node->field_authors[0]['safe'] ?> 
    

    for a sanitized version.

    Instead of printing the array with print_r you can use the Devel module