Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How should I display labels of empty fields when viewing node?
In content-field.tpl.php at CCK module there was:
if (!$field_empty) :
but this IF statement is not in field.tpl.php in drupal7.
i also tried function
function MYTHEME_preprocess_field(&$variables, $hook) {
dpm($variables);
}
but this is not run for empty fields.
I do not want it in node.tpl.php - too much work, needs to be updated with each new field and i have fields in fieldsets. I want it globally (with respecting "Display fields" at content type).
Not important if i can do that in theme or in module.
I found only issues "how to NOT display label...".
Comments
Comment #1
StuartDH CreditAttribution: StuartDH commentedDid you ever find a solution to this? I've been looking for quite a while and can't find out how to display the labels for empty fields, which could then help prompt users to fill in the blanks on a page.
Comment #2
Everett Zufelt CreditAttribution: Everett Zufelt commentedhttp://ygerasimov.com/show-empty-fields
Haven't tested, but it seems right. Please set to Fixed if this resolves the issue.
Comment #3
chrylarson CreditAttribution: chrylarson commentedI can confirm that the link in #2 worked for me. I created a custom module with the code and enabled it.
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedThis worked as I'd hoped and now the labels for empty fields are displaying.
In case there are people even newer than me needing this fix, here's what I did to implement:
1. Copied the above code into a file called dispempty.module
2. Changed function example_field_attach_view_alter to function dispempty_field_attach_view_alter
3. Created a file called dispempty.info containing:
name = Display Empty Fields
description = Displays empty fields in forms
core = 7.x
4. Put both files into /sites/all/modules/dispempty
5. Enabled the Display Empty Fields module in Modules - it appeared in the Other section.
I'm not sure whether (2) is necessary - perhaps someone could confirm.
I'm REALLY surprised to see that there is no field display option to cover this from the UI!
Thanks a lot to ygerasimov for making this available!
Comment #5
Alan D. CreditAttribution: Alan D. commentedI've made a sandbox project that does this: http://drupal.org/sandbox/aland/1658254
It defines new formatter settings that enable administrators to show either "<none>" or a custom string.
This is customisable per instance per view mode.
[update]
http://drupal.org/project/empty_fields
Comment #8
mattc321 CreditAttribution: mattc321 commentedThis works however breaks editable fields and exposed inline entity forms.
Luckily they have a display type of editable, so you can check for them and continue the foreach if they are editable.
Show labels for empty fields without breaking exposed editable fields or inline entity forms
Comment #9
cbrody CreditAttribution: cbrody commentedThanks mattc321, very useful. Trying to get the empty fields to print their associated allowed_values below the label, but without success -- any ideas?
Comment #10
mattc321 CreditAttribution: mattc321 commentedHey cbrody, this is untested, but this is one way you could try. The only problem is that you would want some sort of IF statement, because you likely don't want to fetch the allowed values on every single empty field. For testing purposes though.
Comment #11
cbrody CreditAttribution: cbrody commentedWIth a couple minor modifications that works great, thanks.
Comment #12
ugintl CreditAttribution: ugintl commentedThere is an option to add default text to the field in the field settings. So that it appears on the page even if user hasn't added text to it, but it is not working. Has anybody tried it? I am also using editable module.
Comment #13
ugintl CreditAttribution: ugintl commentedInstalled the module from #8. Nothing happening nothing showing
Comment #14
GuyPaddock CreditAttribution: GuyPaddock at RedBottle Design, LLC for Inveniem commentedAn additional note in case it helps anyone else out: In our case, we wanted to put in a placeholder value ("Information needed") for any fields we were rendering like this. If you have a similar requirement, you will likely need to make sure you populate the
'#items'
array as well.For example:
Comment #15
GuyPaddock CreditAttribution: GuyPaddock at RedBottle Design, LLC for Inveniem commentedAlso, it's probably best to let Drupal tell you if the field value is empty instead of trying to check the node object directly. Different field types map to entities in different ways -- there's not always a 1:1 mapping from field name to node object.
So instead of this:
Use this:
Comment #16
capysara CreditAttribution: capysara commentedThanks all!
I used #8, #14, and #15 and it worked perfectly!
Also, if you're not into custom coding, the empty fields module, noted above, is great, but I didn't want to install 2 extra modules for just a couple of fields.
Comment #17
saki_rayogram CreditAttribution: saki_rayogram as a volunteer commentedThe above solutions didn't work for me on Drupal 8, but I downloaded the Empty Fields module and adapted its code to automatically show for every empty field, instead of having to enable it on hundred of fields:
Comment #18
pmaguniaThe solution in #17 seems to work without having to install another module. I added a check to only act on certain fields after the
foreach
: