Node.tpl.php file: Check is several fields are empty On my node, I have three location fields (primary, secondary, code) and three object fields (food, fuel, accommodation).

I want my node.tp.php to check if all the location fields are empty or if all the object fields are empty and then print a message. 

How can I do this?



Steps I have tried

1. Checking if the fields are empty.

This post says you can check if a single field is empty with the following code:

$field = field_get_items('node', $node, 'field_post_image');
if ($field) {
//Do something with the field
}
else{
//There are no results
}


I have adapted the code by adding the fields to an array and then testing to see if the array is empty:

&lt;?php $empty_message = "No items here, click edit to add some.";?&gt;

&lt;?php 
$field = array();
$field[]= field_get_items('node', $node, 'field_primary');
$field[]= field_get_items('node', $node, 'field_secondary');
$field[]= field_get_items('node', $node, 'field_code');

if (empty($field)) {
print $empty_message;
}
?&gt;


&lt;?php 
$field = array();
$field[]= field_get_items('node', $node, 'field_food');
$field[]= field_get_items('node', $node, 'field_fuel');
$field[]= field_get_items('node', $node, 'field_accomodation');

if (empty($field)) {
print $empty_message;
}
?&gt;


But it doesn't seem to work. The original post actually says this is the wrong way to check if fields are empty, but I'm not sure how to adapt the suggested code.

2. Using Field Group

I would prefer not use this module, as it adds too much complexity for my needs.
Put all the logic in your template.php (or wherever your preprocess functions go) with THEME_preprocess_node():

function THEME_preprocess_node(&amp;$variables) {
  // Grab the node
  $node = $variables['node'];

  // Only run for your content type
  if ($node-&gt;type == 'your_content_type') {

    // Grab your fields with field_get_items so that it is translation friendly
    $location_field1 = field_get_items('node', $node, 'location_field1');
    $location_field2 = field_get_items('node', $node, 'location_field2');
    $location_field3 = field_get_items('node', $node, 'location_field3');
    $object_field1 = field_get_items('node', $node, 'object_field1');
    $object_field2 = field_get_items('node', $node, 'object_field2');
    $object_field3 = field_get_items('node', $node, 'object_field3');

    // Check if all location fields are set or not
    if (isset($location_field1[0]['value']) &amp;&amp; isset($location_field2[0]['value'] &amp;&amp; isset($location_field3[0]['value'])) {
      // Create a variable that can be used in your node.tpl.php
      $variables['messages']['location_message'] = 'All location fields are set...';
    } else {
      // Create a variable that can be used in your node.tpl.php
      $variables['messages']['location_message'] = 'All location fields are not set...';
    }

    // Check if all object fields are set or not
    if (isset($object_field1[0]['value']) &amp;&amp; isset($object_field2[0]['value'] &amp;&amp; isset($object_field3[0]['value'])) {
      // Create a variable that can be used in your node.tpl.php
      $variables['messages']['object_message'] = 'All object fields are set...';
    } else {
      // Create a variable that can be used in your node.tpl.php
      $variables['messages']['object_message'] = 'All object fields are not set...';
    }
  }
}


Then, in your node.tpl.php you will have access to the message and you can print it anywhere with:

print $messages['location_message'];

and 

print $messages['object_message'];