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.

Comments

a.ross’s picture

Sorry, apparently field_group does set #access. Or at least it causes it to be set.

a.ross’s picture

Project: Commerce Core » Field Group
Component: Product reference » Code
Priority: Minor » Normal

Moving over to Field Group

a.ross’s picture

Issue summary: View changes

m

leendertdb’s picture

Issue summary: View changes

I 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:

array(
  '#prefix' => '<div class="commerce-product-field commerce-product-field-field-what-you-can-learn field-field-what-you-can-learn node-50-product-field-what-you-can-learn commerce-product-field-empty">',
 '#suffix' => '</div>',
  '#weight' => 1,
  '#access' => TRUE,
);

I solved this problem for now with a quick and dirty fix with the following code.

/**
 * Implements theme_preprocess_node()
 */
function theme_preprocess_node(&$vars) {
    // Dirty fix to prevent empty commerce product referenced fields from showing..
    // See https://www.drupal.org/node/1985606.
    foreach ($vars['content'] as $key => $value) {
      if (stristr($key, 'product:') && is_array($value) && count($value) == 4 && stristr($value['#prefix'], 'commerce-product-field-empty')) {
        unset($vars['elements'][$key]);
        unset($vars['content'][$key]);
      }
    }
}
nixou’s picture

Project: Field Group » Commerce Core
Component: Code » Product reference
Status: Active » Needs review
StatusFileSize
new3.84 KB

Same 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.

nixou’s picture