0

I need to add fields to the Ubercart entity uc_order_product. I was looking into the function field_create_instance, but don`t know exactly where to use it. I believe I should attach the field I want to the uc_order_product entity using field_create_instance.

The 'uc_order_product' definition in uc_order.module doesn't have a fieldable => TRUE definition. May I add that definition from my custom module and tell uc_order_product is fieldable? This is uc_order_product entity definition in the uc_order.module.

'uc_order_product' => array(
  'label' => t('Order product'),
  'base table' => 'uc_order_products',
  'controller class' => 'UcOrderProductController',
  'metadata controller class' => 'UcOrderProductMetadataController',
  'entity keys' => array(
    'id' => 'order_product_id',
    'label' => t('Order product'),
  ),
  'bundles' => array(
    'uc_order_product' => array(
      'label' => t('Order product'),
    ),
  ),
  'view modes' => array(
    'full' => array(
      'label' => t('Normal view'),
    ),
    'cart' => array(
      'label' => t('Cart view'),
    ),
  ),
  // Entity API callbacks.
  'access callback'   => 'uc_order_order_product_access',
  'save callback'     => 'uc_order_product_entity_save',
  'deletion callback' => 'uc_order_product_delete',
) 

Finally, I am trying to hook to this entity when it gets updated (hook_entity_update, hook_entity_presave) but they never get called on uc_order_product update. How can I hook to this entity when it is being saved, or when it has been saved? Thank you!

1

First of all, if you want alter the meta information of an entity, the hook_entity_info_alter in your custom module is the right way. More information in official page: hook_entity_info_alter

In this particular case, attributte 'fieldable' is not defined, so the function responsable to call the hook_entity_info and hook_entity_info_alter is the entity_get_info() in common.inc file. This function provide default values and the default value to 'fieldable' is FALSE. You only need change this value implementing hook_entity_info_alter.

 function YOUR_MODULE_entity_info_alter(&$entity_info){
   if(isset($entity_info['uc_order_product'])){
     $entity_info['uc_order_product']['fieldable'] = TRUE;
   }
 }

Second issue, This is a link to a previous question about how create fields programatically. The best answer suggests field_create_fieldand 'field_create_instance', but if you have a defined field (for example, an image type field created through the UI), you could use field_update_field and 'field_update_instance'. You can find more information about these functions in field.crud.inc in the field module folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.