18

How to you check that a field is empty, when you have the $node object?.

I found this as possible solution:

$field = field_get_items('node', $node, 'field_post_image');

I can check now:

UPDATE.Ups! This not reliable, althought I promise that it worked for me yesterday.

isset($field);

but I wonder if there is a better way.

Enlighten me

30

If you read up on the documentation the function returns FALSE if there is no data. isset() would actually fail because you are assigning FALSE to the variable so it's actually been set to something. The correct syntax would be:

$field = field_get_items('node', $node, 'field_post_image');
if ($field) {
   //Do something with the field
}
else{
   //There are no results
}
  • It seems this is the best way. If it's empty will return FALSE, if has any data it will return an array, so there is no worry that $field evaluates to FALSE in case of number fields 0, etc. – corbacho Sep 16 '11 at 8:19
  • This did not work for me because it returned the default value when nothing was set. – river Dec 5 '12 at 1:47
  • @Sam152 Thanks! This worked great for when I was logged in, but when I am logged out it is not showing anything. Is there a reason for that and any solution? – Dan Apr 8 '14 at 22:29
  • Hi Dan. Being logged in shouldn't have any impact on the above code. Perhaps you are using devel to debug a variable and anon users don't have "access developer information"? – Sam152 Apr 11 '14 at 3:02
  • This is the wrong way of checking if a field is empty, you should use the approach described with calling the hook_field_is_empty defined by the module implementing the field, see the post by kiamlaluno. – lslinnet Mar 9 '15 at 6:59
8

You could use the following snippet to verify if a field is empty.

$info = field_info_field($field_name);
$function = $info['module'] . '_field_is_empty';

if (function_exists($function)) {
  $value = field_get_items('node', $node, $field_name);
  $is_empty = $function($value[0], $info);
}

This code is safer, as different fields are considered empty under different conditions. See, for example, the difference between file_field_is_empty(), number_field_is_empty(), and taxonomy_field_is_empty(): taxonomy_field_is_empty() checks the tid property of the $item parameter, while number_field_is_empty() checks the value property of the same parameter. Custom fields could require a more complex condition to be verified, in order to consider the field empty.

The description for hook_field_is_empty() given in the documentation is the following one:

Define what constitutes an empty item for a field type.

  • I appreciate this answer, but the hook_field_is_empty() seems to go very far. I want to know if the field is really empty. For example number_field_is_empty considers that 0 is also empty. Also I test that you can't apply this function directly if the field is multi-value. – corbacho Sep 16 '11 at 8:13
  • 1
    That is what does empty() when you pass it a variable containing 0, or the string "0"; it could be the field is using a value when it is empty, and invoking hook_field_is_empty() is the only way to know if the field is empty. – kiamlaluno Jul 17 '12 at 13:30
  • There is a mistake. Should be $function($value[0], $info); See api.drupal.org/api/drupal/modules!field!field.api.php/function/… – volocuga Dec 10 '18 at 23:07
2

We found the raw value in ($content['field_name']['#items'][0][value]), so you can determine if the field is empty or not with the expression

(!empty($content['field_name']['#items'][0][value]))

where field_name matches the name of the desired field.

  • No. The "value" key is just a db column name. It can be different depending on the field type. I think the best way - check hook_field_is_empty() (which is required) as proposed above – volocuga Dec 10 '18 at 22:48
1

Drupal 8

If somebody need the same answer in D8, that is what I do to test my field:

if ($entity->hasField('field_event_date_start')
  && !$entity->get('field_event_date_start')->isEmpty()) {
  // Do something ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.