Score:0

How show field labels in the node display even if it's empty

cn flag

While showing a node, only the filled fields are rendered. I wanted to be able to show the empty field labels, and I was looking for something like the ability to uncheck an option such as 'Hide if empty' or check a 'Display even if empty.'. Unfortunately, there are no such options. In order to display them, I used a hook_preprocess_node:

/**
 * Implements hook_preprocess_node().
 */
function MYMODULE_preprocess_node(&$variables) {
    $node = $variables['node'];
    $all_fields = $node->getFieldDefinitions();
    $node_fields = $node->getFields();
    foreach ($node_fields as $field_name => $field) {
        $value = $field->getString();
        if (str_contains($field_name, 'field_') && !$value) {
            $variables['content']['_field_layout']['content'][$field_name] =
            [
                '#theme' => 'field',
                '#title' => $label = $all_fields[$field_name]->getLabel(),
                '#field_type' => $all_fields[$field_name]->getType(),
                '#label_display' => "inline",
            ];
        }
    }
}

This made all the hidden fields display, but are not honouring the order set on the node display. Is there a way to get and pass a #weight?

Harsh Vashisht avatar
sk flag
I think you could render those through the managed display section from content types.
rfmarcelino avatar
cn flag
Thank you @harsh-vashisht, but that's not an option using core. It made me however look for contrib again and I did find what I was looking for.
Score:2
cn flag

I found a contrib module that allows showing hidden fields: https://www.drupal.org/project/empty_fields

Thank you for people chipping in that made me dig deeper ( I still can't believe how I missed it before moving to the preprocess )

Score:0
br flag

As far as understand, most (if not all) twig files check:

if the field is empty
    don’t show both label or content
else
    show everything

I’ve came across the kind of situation you describe. My solution was to change (appropriately) the twig files so that the label is outside the if statement.

rfmarcelino avatar
cn flag
Yes, that was my initial thought, however {{ content }} in the node.html.twig template is already missing the info such as labels from empty fields.
Balde Binos avatar
br flag
No. I'm referring to field.html.twig or some other template at the field level.
rfmarcelino avatar
cn flag
Unfortunately, those templates are only being called for fields with content. I think something in core is removing it from the rendered array. The preprocess above was an attempt to add it again...and it does, but not in the correct order.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.