I want to add two new fields to the Commerce Discount entity --- an image and a long text. And I want these to be visible through Views.

I was going to use this code to add the fields:


But, I was thinking that there are already two fields attached to Node Bundles that do exactly what I want. All I really need to do is -copy- that field information to the Commerce Discount entity Bundles.

So I was thinking that this might do the trick, but I'm not

function() my_module_update_7000() { // copy field info from existing entity $field_name = 'field_page_banner'; $field = field_info_field($field_name) field_create_field($field);

// copy field instance from existing entity $field_instance = field_info_instance('node', 'field_page_banner', 'page')

// ???? will this screw up the data in any existing bundles? $field_instance['entity_type'] = 'commerce_discount', $field_instance['bundle'] = array('order_discount', 'product_discount'),

field_create_instance($field_instance); }

  1. Can someone look at this and let me know if I'm on the right track here? I'm not clear on how to assign the field to the new Bundle without 'un-assigning' it from the existing Bundles (if that makes sense.)

  2. Is there anything else I need to do to make these fields available to add to existing Views?

  3. Is there anything else I need to do to make these fields available to users when editing Commerce Discounts?

  • In regard to #1, that's kind of the point of field instances--to be able to reuse fields on different entities and bundles. So if you have an existing field, you don't have to recreate it. Just use field_create_instance and you have described above. You're not unassigning the existing field instance(s) unless you explicitly delete it from the entity and/or bundle. Mar 1, 2015 at 2:27
  • Got it. If I may ask a follow up... what is it in the Entity that determines whether or not one can add/edit fields from the Admin interface?
    – jchwebdev
    Mar 1, 2015 at 7:34
  • I believe by default, there are no restrictions on fields. Take a look at the Field Permissions module if you need something to do that. Mar 1, 2015 at 7:53
  • @jchwebdev there's also the 'fieldable' key on the $entity info "Set to TRUE if you want your entity type to accept fields being attached to it." api.drupal.org/api/drupal/modules%21system%21system.api.php/… Mar 1, 2015 at 9:42
  • @DavidThomas I looked in the Entities under Devel and it -says- that the entity type 'Commerce Discounts' has a key called 'fieldable' and it is set to TRUE. But in the Commerce Discounts Admin there was no way to add a new -field-. So there must be something else to it. Anyhoo, your suggestion got me to where I could add programmatically. I just wish I could understand the mechanism that allows one to add fields via Admin (for the future.)
    – jchwebdev
    Mar 2, 2015 at 6:02


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.