I'm trying to get a new 'commerce_product' entity property (not field) I've added to show up on a product reference node. To do this, I've added the property with hook_field_extra_fields_alter() and hook_entity_property_info_alter(). However, it's impossible to get this new property to work with the 'product_reference' module's pattern for field injection.

The 'product_reference' module calls commerce_product_field_extra_fields() directly instead of field_info_extra_fields().

        // 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();

In this code: commerce_product_reference_field_extra_fields()

Because it calls commerce_product_field_extra_fields() directly, my hook_field_extra_fields_alter() is never called to add the new property as an extra field. I understand the reason why you're calling commerce_product_field_extra_fields() directly is because it's called inside of a hook_field_extra_fields() function and would recurse to infinity if you called field_info_extra_fields().

I propose a work around of exposing a hook_commerce_product_add_extra_fields_alter() in commerce_product. Then drupal_alter('commerce_product_add_extra_fields', $extra); can be called in commerce_product_field_extra_fields(). This will allow a custom module to implement this hook and expose their extra fields.

If you have another solution, please let me know.

Patch to follow.

Comments

bendiy’s picture

Status: Active » Needs review
StatusFileSize
new1.59 KB

See attached

Status: Needs review » Needs work
bendiy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.59 KB

Let's try that again...

anybody’s picture

Status: Needs review » Reviewed & tested by the community

+1 for this patch. I'm having exactly the same issue and the current solution makes adding custom extra fields impossible. The patch works great and without any further risks.

RTBC from my point of view.

Any module maintainer feedback?

johnpitcairn’s picture

+1 from me also please, I need to extra fields to the product that will be rendered on the product display.

Updated patch for latest dev, just fixes offsets.

rszrama’s picture

Status: Reviewed & tested by the community » Needs review

Does it create a naming conflict to just make this hook_commerce_product_extra_fields_alter()? It isn't clear to me where "add" came from.

adamgerthel’s picture

I'm trying to implement this but I'm unsure what to add to the $extra array. I've managed to display the field on the display node content type's manage display tab, but I can't get the field to actually display in the node template using <?php print render($content['product:product_options_products_eva']); ?>.

This is my attempt:

function custom_hooks_commerce_product_add_extra_fields_alter(&$extra) {
  $extra['commerce_product']['article']['display']['product_options_products_eva'] = array(
    'label' => 'Product options - EVA',
    'description' => '',
    'weight' => 0,
    'theme' => 'product_options_products_eva'
  );

  return $extra;
}

The field name should be correct. I'm not 100% sure how to double check that though. Also, is the 'theme' property required?