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?

  • 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. – Tobias Krause Mar 30 '15 at 14:56
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' => ' '),
  );

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.