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?