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
Comment #1
bendiy commentedSee attached
Comment #3
bendiy commentedLet's try that again...
Comment #4
anybody+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?
Comment #5
johnpitcairn commented+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.
Comment #6
rszrama commentedDoes 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.
Comment #7
adamgerthel commentedI'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:
The field name should be correct. I'm not 100% sure how to double check that though. Also, is the 'theme' property required?