0

I'm using some hook code I got from Drupal 7 show empty fields in order to show all of the empty fields on my node when viewing it. I would like to put a simple - as the displayed value when the field is empty.

function mymodule_field_attach_view_alter(&$output, $context) { // other stuff clipped... $output[$field_name] = array( '#theme' => 'field', '#title' => $instance['label'], '#label_display' => 'inline', '#field_type' => $field['type'], '#field_name' => $field_name, '#bundle' => $node->type, '#object' => $node, '#items' => array(), '#entity_type' => 'node', '#weight' => $display['weight'], 0 => array('#markup' => '-'), ); } 

I pulled the code from the linked page above and just changed the one line at the end that has #markup. As far as I can tell from the api docs, this code should work, but nothing that I put into that #markup value shows up.

All of the empty fields DO show up, it's just that the value shown is empty instead of - or blah or foo, because I've tried those as well. :)

How do I set the value to display when I use hook_field_attach_view_alter?

1
  • Did you find a way to get this working? It has been a while butI am facing exactly the same problem at the moment and hope that you might help me. Commented Mar 30, 2015 at 14:56

1 Answer 1

1

Ok, I found the answer: the key "#items" in the output array needs to have an array with an array as the value - not just the array:

$output[$field_name] = array( '#theme' => 'field', '#title' => $instance['label'], '#label_display' => 'above', '#field_type' => $field['type'], '#field_name' => $field_name, '#bundle' => $node->type, '#object' => $node, '#items' => array(), '#entity_type' => 'node', '#weight' => $display['weight'], 0 => array('#markup' => ' '), ); 

becomes

$output[$field_name] = array( '#theme' => 'field', '#title' => $instance['label'], '#label_display' => 'above', '#field_type' => $field['type'], '#field_name' => $field_name, '#bundle' => $node->type, '#object' => $node, '#items' => array(array()), '#entity_type' => 'node', '#weight' => $display['weight'], 0 => array('#markup' => ' '), ); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.