My page.tpl.php includes:

if ($node) {
    // field_suburb is a "text" field that doesn't exist on this node type
    $f = field_view_field('node', $node, 'field_suburb');
    print render($f);

This triggers:

PHP Fatal error: Cannot access empty property in field/field.attach.inc

I added the following debug code in field.attach.inc ("if field name is null" block):

foreach ($languages as $langcode) {
    if ($field_name === null) {
        print $field_name === null ? "FIELD NAME IS NULL\n" : '';
        print $instance == null ? "INSTANCE IS NULL\n" : '';
        print "$entity_type $bundle\n";
    $grouped_items[$field_id][$langcode][$id] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
    // Group the instances and entities corresponding to the current field
    $grouped_instances[$field_id][$langcode][$id] = $instance;
    $grouped_entities[$field_id][$langcode][$id] = $entities[$id];

That code prints:

    [0] => 
    [field_name] => field_suburb
    [language] => 
    [default] => 1
    [deleted] => 

This is easy enough to protect against - I can just check my node type before accessing the field - but surely field_view_field should just return null for a field that doesn't exist instead of having the entire site die?!?

Should it be safe to call field_view_field() - i.e. is this a bug? If it's not a bug, then what's the easiest way to check to see if my given node content type actually has the field I'm rendering? Checking the type against a list of known safe types isn't very flexible, and calling field_get_items() followed by field_view_value() seems redundant when field_view_field() is available.

Drupal version is 7.22.

1 Answer 1


Source code of this function looks as follows:

function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
  $output = array();

  if ($field = field_info_field($field_name)) {
  // Actual work on field, not needed here in this answer

  return $output;

It means that if there is no field, empty array will be returned. That's all. Is it safe? Depends what are you doing with that array down the road. For example you cannot render empty arrays with render(), as it accepts only arrays representing Drupal's renderable elements.

Note that this is from the current, 7.23 version of Drupal.

  • I swear I was getting a WSOD on 7.22 (on the field_view_field call), but it does look like there's no way for that to happen in 7.23.
    – George
    Sep 17, 2013 at 0:53
  • @George some WSODs was fixed between 7.22 and 7.23 - I don't have 7.22 handy now to test it. I haven't seen this piece mentioned in changelog so I was sure it should work the same.
    – Mołot
    Sep 17, 2013 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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