You are here

function commerce_product_reference_field_extra_fields in Commerce Core 7

Implements hook_field_extra_fields().

This implementation will define "extra fields" on node bundles with product reference fields to correspond with available fields on products. These fields will then also be present in the node view.

1 call to commerce_product_reference_field_extra_fields()
commerce_product_reference_form_field_ui_display_overview_form_alter in modules/product_reference/commerce_product_reference.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function commerce_product_reference_field_extra_fields() {
  $extra =& drupal_static(__FUNCTION__);
  if (isset($extra)) {
    return $extra;
  }
  $extra = array();

  // Loop through the product reference fields.
  foreach (commerce_info_fields('commerce_product_reference') as $field_name => $field) {
    foreach ($field['bundles'] as $entity_type => $bundles) {
      if ($entity_type == 'commerce_line_item' || $entity_type == 'commerce_product') {

        // We do not currently support the injection of product fields into the
        // display of line items or other products.
        continue;
      }
      foreach ($bundles as $bundle_name) {

        // Get the instance settings for the field on this entity bundle.
        $instance_settings = field_info_instance($entity_type, $field['field_name'], $bundle_name);

        // If field injection is turned off for this instance, skip adding the
        // extra fields to this bundle's view modes.
        if (empty($instance_settings['settings']['field_injection'])) {
          continue;
        }

        // Attach extra fields from products that may be visible on the bundle.
        // We have to call commerce_product_field_extra_fields() directly
        // instead of using field_info_extra_fields() because of the order in
        // which these items are rebuilt in the cache for use by "Manage
        // display" tabs. Otherwise these extra fields will not appear.
        $product_fields = commerce_product_field_extra_fields();

        // Prevent notices if there are no product types defined.
        if (empty($product_fields['commerce_product'])) {
          continue;
        }
        foreach ($product_fields['commerce_product'] as $key => $value) {
          foreach ($value['display'] as $product_extra_field_name => $product_extra_field) {
            $product_extra_field['label'] = t('Product: @label', array(
              '@label' => $product_extra_field['label'],
            ));
            $product_extra_field['display']['default'] = array(
              'weight' => 0,
              'visible' => FALSE,
            );
            $extra[$entity_type][$bundle_name]['display']['product:' . $product_extra_field_name] = $product_extra_field;
          }
        }

        // Do the same for fields on products that may be visible on the bundle.
        // First build a list of product types that may be referenced.
        $field_instance = field_info_instance($entity_type, $field_name, $bundle_name);
        $product_types = array_filter($field_instance['settings']['referenceable_types']);

        // If no product types are specified, use all product types.
        if (empty($product_types)) {
          $product_types = array_keys(commerce_product_types());
        }
        foreach ($product_types as $product_type) {
          foreach (field_info_instances('commerce_product', $product_type) as $product_field_name => $product_field) {
            $extra[$entity_type][$bundle_name]['display']['product:' . $product_field_name] = array(
              'label' => t('Product: @label', array(
                '@label' => $product_field['label'],
              )),
              'description' => t('Field from a referenced product.'),
              'weight' => $product_field['widget']['weight'],
              'configurable' => TRUE,
            );
          }
        }
      }
    }
  }
  return $extra;
}