When displaying fields on a product display, commerce does its magic to include the fields from the Product entity. However, when a field of a certain product that's being displayed is empty, the following Render API code is output (at least from field_group's perspective):
array(
'#weight' => 1,
'#access' => TRUE,
);
Whereas regular fields, when they are empty, produce no output at all. And to be honest I have absolutely no clue where this comes from. #weight might be added by field_group itself, but #access? What I do know is that the code that prepares the fields in commerce_product_reference_entity_view sets an empty array for empty fields (unless cardinality is not 1, then #prefix and #suffix are set for AJAX refreshing). Unsetting the field altogether (or setting to NULL) solves the problem.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | commerce-product_reference_empty_field-1985606-5.patch | 3.84 KB | nixou |
| #4 | product_reference_empty_field-1985606-4.patch | 3.84 KB | nixou |
Comments
Comment #1
a.ross commentedSorry, apparently field_group does set #access. Or at least it causes it to be set.
Comment #2
a.ross commentedMoving over to Field Group
Comment #2.0
a.ross commentedm
Comment #3
leendertdb commentedI have also came accross this issue where a field_group item (horizontal tab) was visible despite all elements in that tab being empty.
An example of an empty referenced product field in my case looked like this:
I solved this problem for now with a quick and dirty fix with the following code.
Comment #4
nixou commentedSame problem here.
I think that the issue is on Drupal Commerce side.
Explanation
Field Group hide empty groups using field_group_remove_empty_display_groups().
This function hide the group if its children are "empty".
"empty" : The field is empty or its #access property is undefined/false.
In our case, Commerce Product Reference return an empty array in $node->content[].
This occurs when field_view_field() is called in commerce_product_reference_entity_view() and return an empty array.
Then the #weight and #access (TRUE) properties are added by _field_extra_fields_pre_render().
So the field is not empty for Field Group.
If we take a look as how it works for node field (in node core module) we can see empty fields are not added to $node->content[].
So I think Commerce Product Reference have to do the same thing : if the field is empty, do not add it to $node->content[].
I attach a patch as a proposal.
This solves the problem for me but I'm not sure about the potential consequences.
If someone can have a look it will be great.
Comment #5
nixou commentedReroll