You are here

function commerce_addressbook_get_bundles_with_field in Commerce Addressbook 7

Helper function to get a list of bundles (grouped by entity type) that contain a certain field type, and each of the fields that are attached to them

Parameters

$field_type: The field type to look for

$only_enabled: Setting this to TRUE returns only entity types that have been explicitly enabled for usage with Commerce Addressbook. Setting this to FALSE might result in errors.

Return value

Associative array in the form [entity_type] [bundle] => field_name

3 calls to commerce_addressbook_get_bundles_with_field()
commerce_addressbook_commerce_order_presave in ./commerce_addressbook.module
Implements hook_commerce_order_presave().
commerce_addressbook_field_storage_pre_insert in ./commerce_addressbook.module
Implements hook_field_storage_pre_insert().
commerce_addressbook_get_saved_addresses in ./commerce_addressbook.module
Get field items from a user's saved entities that have an addressfield in their bundle. @TODO: do we really need to get all entities, or can be have hardcoded default to commerce_customer_profile?

File

./commerce_addressbook.module, line 582
:

Code

function commerce_addressbook_get_bundles_with_field($field_type = 'addressfield', $only_enabled = TRUE) {
  $result =& drupal_static(__FUNCTION__);
  if (!isset($result)) {
    $entities = entity_get_info();

    // Find bundles that have an addressfield.
    $field_info = field_info_field_by_ids();

    // Get a list of entity_type / bundles combinations
    // that actually have an addressfield attached to them
    foreach ($field_info as $field) {
      if ($field['type'] == $field_type) {
        foreach ($field['bundles'] as $entity_type => $bundles) {
          if ($bundles) {
            foreach ($bundles as $bundle_name) {

              // @TODO: support the case where there are multiple addressfields for one bundle
              // Although that's probably not that common
              $result[$entity_type][$bundle_name] = $field['field_name'];
            }
          }
        }
      }
    }

    // Remove entities that have not been explicitly defined to work with this module
    if ($only_enabled) {
      $enabled_entities = commerce_addressbook_enabled_entities();
      if ($result) {
        foreach ($result as $entity_type => $data) {
          if (!in_array($entity_type, $enabled_entities)) {
            unset($result[$entity_type]);
          }
        }
      }
    }
  }
  return $result;
}