You are here

function commerce_product_reference_field_extra_fields_display_alter in Commerce Core 7

Implements hook_field_extra_fields_display_alter().

This whole implementation is basically a hack because Drupal core does not allow you to specify default visibility for extra fields. We don't want any of the Product extra fields to be visible by default in referencing entities, so we have to alter the display settings at this point until such a time as the settings have been updated for the given bundle.

File

modules/product_reference/commerce_product_reference.module, line 127
Defines a field type for referencing products from other entities.

Code

function commerce_product_reference_field_extra_fields_display_alter(&$displays, $context) {

  // Load the bundle settings for the current bundle.
  $bundle_settings = field_bundle_settings($context['entity_type'], $context['bundle']);

  // Loop over the extra fields defined by the Product module.
  $product_fields = commerce_product_field_extra_fields();

  // Prevent notices if there are no product types defined.
  if (empty($product_fields['commerce_product'])) {
    return;
  }
  foreach ($product_fields['commerce_product'] as $key => $value) {
    foreach ($value['display'] as $product_extra_field_name => $product_extra_field) {

      // If the current extra field is represented in the $displays array...
      if (!empty($displays['product:' . $product_extra_field_name])) {

        // And no data yet exists for the extra field in the bundle settings...
        if (empty($bundle_settings['extra_fields']['display']['product:' . $product_extra_field_name]) || empty($bundle_settings['view_modes'][$context['view_mode']]['custom_settings']) && empty($bundle_settings['extra_fields']['display']['product:' . $product_extra_field_name]['default']) || !empty($bundle_settings['view_modes'][$context['view_mode']]['custom_settings']) && empty($bundle_settings['extra_fields']['display']['product:' . $product_extra_field_name][$context['view_mode']])) {

          // Default the extra field to be invisible.
          $displays['product:' . $product_extra_field_name]['visible'] = FALSE;
        }
      }
    }
  }
}