I have the label of some fields set to display above the content, and would like the labels to show up only if the fields have any content, right now even if the fields are empty, the label shows.
looking at ds.module at line 535-543
function ds_render_region($content, $region, $layout) {
$output = '';
if (isset($layout['settings']['regions'][$region])) {
foreach ($layout['settings']['regions'][$region] as $key => $field) {
$output .= render($content[$field]);
}
}
return $output;
}
By adding an extra if statement:
function ds_render_region($content, $region, $layout) {
$output = '';
if (isset($layout['settings']['regions'][$region])) {
foreach ($layout['settings']['regions'][$region] as $key => $field) {
if ($content[$field]['#items'][0] != '') {
$output .= render($content[$field]);
}
}
}
return $output;
}
The labels are gone from empty fields, but I am getting a warning
Notice: Undefined offset: 0 in ds_render_region() (line 539 of /Volumes/Data/Users/vinsab/Sites/feral/sites/all/modules/ds/ds.module)
Would it be possible to have this feature ? Many thanks
Comments
Comment #2
vinsabus commentedsorry was looking at RC3 code.. in the latest dev the code is between lines 523-531
edit: No, the code I pasted above does not work if I am using fieldgroups
Comment #3
swentel commentedUsing 'if (isset($content[$field]['#items'][0]))' will make the notice go away normally. I'm going to check how core Field API does this and see what the most elegant solution is. I'm going to add some more options for developers/themers too so they can more easily override the default behavior of the rendering that DS does now. Will come back on this later.
Comment #4
swentel commentedCan you give me an example of such a field, because when I add new fields (say text fields) and leave their content empty, I don't get labels at all.
Comment #5
altrugon commentedI having the same problem and also have noticed that the markup for the field get into the output even if the field is empty and there is not label to displayed.
I have attached a screenshot so you can see the type of fields that I'm using.
Comment #6
vinsabus commentedSorry, I've been away for long. The fields I've had problems with are file field with display set to jplayer. And multi-image field with display set to field-slideshow.
Comment #7
swentel commentedWell, I'm still not really able to reproduce. Granted, haven't tested all fields from #5, but I'm assuming one of them is the e-mail field from the email module and I don't have any problems with it. I also tested with the jplayer and the problem there is with that module that doesn't check if $items is empty or not in jplayer_field_formatter_view(). Even without a display suite layout, you will still see the label, so this isn't DS's fault at all.
Comment #8
altrugon commentedI fell into this problem again and found out that you have to return an empty array on your hook_field_formatter_view(), you can check taxonomy_field_formatter_view() as example.
Comment #9
drupalreggie commentedI've got this problem also using the D6 version of DS.
My conditions:
Any clues?
Comment #10
havabeer commentedThe issue occurs when using Term Reference fields. The label prints even when the field is empty.
d7.9 ds7x-1.4
Comment #11
azinck commentedI'm seeing this with Term Reference fields as well. Same exact version of Drupal and DS as havabeer.
Comment #12
arcane commentedI'm curious as to why this is a won't fix as this seems to be a fairly common problem.
Comment #13
azinck commented@arcane: I think the problem is caused by the display formatter, not DS. In the case of Term Reference fields I was using Taxonomy Formatter which returned a wrongly-constructed render array: #1352050: Label is always printed, when taxonomy is empty.
What specific display formatter are you seeing this with?
Comment #14
arcane commentedI am using a view that is taken over by display suite. The field is a long text type.
Comment #15
arcane commentedNever mind, it was a views field setting. Thanks!
Comment #16
a.milkovskyI've fixed this problem by altering field output with hook_field_attach_view_alter().
Comment #17
skdrupal88thanks a.milkovsky it works, but you also need to add condition:
because your code hides labels for fields with value 0.
Comment #18
a.milkovskySergey, thank you. You're absolutely right.
Here is updated code:
Comment #19
hongpong commentedalso for complex field hiding in DS conditionally this gets the jerb done: https://drupal.org/project/ffc - if field A is empty it can hide B.
Comment #20
hongpong commentedi expanded this logic for addressfields with no thoroughfare field data, and link fields with no URL data. (how annoyingly difficult to nail addressfield labels. anyway)
Comment #21
jim_keller commented#18 needs an update in order to work correctly. The "if $field[0]['#markup'] != 0" statement should be updated to have two equals signs, since a blank string is == 0 in PHP, but not === 0. The if statement evaluates to false on blank strings, which is where we want it to evaluate to true.
updated code is:
Comment #22
a.milkovskythank you, agree with jim_keller
Comment #23
eigentor commentedO.K. I was searching for a solution for field collections. I see this issue is for regular fields.
Comment #24
dremy commentedNote this can also be done with EVA (Entity Views Attachment) module by creating a view that shows the fields and their values of the node through an argument. Then you can adjust the rewrite results if empty configuration for the field. Lastly, you need to hide all the fields on the node display that are being now showcased through the view.
Comment #25
2dareis2do commented#20 HongPong works like a charm for me. Many thanks. Maybe there should be a module for this?
Comment #26
kristindev commentedAnyone got a fix for this for D8?