You are here

function commerce_addressbook_get_saved_addresses in Commerce Addressbook 7

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?

Parameters

$uid: The uid of the user you want to pull saved field data.

Return value

An array of saved user entity profiles with associated field items.

2 calls to commerce_addressbook_get_saved_addresses()
commerce_addressbook_commerce_order_presave in ./commerce_addressbook.module
Implements hook_commerce_order_presave().
commerce_addressbook_get_saved_addresses_options in ./commerce_addressbook.module
Get all available addressbook select list options, per customer profile entity bundle.

File

./commerce_addressbook.module, line 470
:

Code

function commerce_addressbook_get_saved_addresses($uid) {
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results[$uid])) {

    // Don't return addresses for anonymous users
    if (!user_is_logged_in()) {
      return array();
    }
    $addressfield_bundles = commerce_addressbook_get_bundles_with_field('addressfield');

    // Query entity_types that match our bundles.
    $entity_data = array();
    foreach ($addressfield_bundles as $entity_type => $bundles) {
      $entity_profiles = array();

      // $bundles is an array with key => value : bundlename => addressfield name
      $bundle_names = array_keys($bundles);
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', $entity_type)
        ->entityCondition('bundle', $bundle_names, 'IN')
        ->propertyCondition('uid', $uid);

      // Specific additions for commerce_customer_profile entities
      if ($entity_type == 'commerce_customer_profile') {
        $query
          ->propertyOrderBy('profile_id', 'DESC');
        $query
          ->propertyCondition('status', 1);
      }
      $result = $query
        ->execute();

      // $result contains entity objects with the current entity_type and one of the selected bundles
      if (!empty($result[$entity_type])) {
        $entity_profiles = entity_load($entity_type, array_keys($result[$entity_type]));
      }

      // We found all bundles for this user for the current entity_type.
      // Get the appriopriate field values for each of them.
      if ($entity_profiles) {
        foreach ($entity_profiles as $entity_id => $entity) {
          $bundle = $entity->type;
          $addressfield = $bundles[$bundle];
          $field = field_get_items($entity_type, $entity, $addressfield);
          if (is_array($field)) {

            // @TODO: what if there are multiple values in the addressfield?
            // Is that a reasonable use case?
            $address_values = array_shift($field);
          }

          // @TODO: make value configurable
          if (isset($address_values['thoroughfare'])) {
            $label = $address_values['thoroughfare'];
          }
          drupal_alter('commerce_addressbook_label', $label, $entity);
          $results[$uid][$entity_type][$bundle][$entity_id] = filter_xss($label);
        }
      }
    }
  }
  return $results[$uid];
}