You are here

function commerce_customer_profile_copy_fields in Commerce Core 7

Copy field values from a source profile to a target array.

Parameters

$info: An array containing info for the entity type, bundle, and pane ID.

$target: An array (typically $form_state) in which values will be copied to.

$source: Can be either an array or object of values.

$form_state: The form state array from the form.

1 call to commerce_customer_profile_copy_fields()
commerce_customer_profile_copy_validate in modules/customer/commerce_customer.module
Element validate callback: Pertaining to the "copy profile" checkbox.

File

modules/customer/commerce_customer.module, line 1474
Defines the customer profile entity and API functions to manage customers and interact with them.

Code

function commerce_customer_profile_copy_fields($info, &$target, $source, &$form_state) {
  list($entity_type, $bundle, $pane_id) = $info;
  $form_state['order']->data['profile_copy'][$pane_id]['elements'] = array();

  // Loop over all the field instances that could be attached to this entity.
  foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
    $field = NULL;

    // Extract the field value from the source object or array.
    if (is_object($source) && isset($source->{$field_name})) {
      $field = $source->{$field_name};
    }
    elseif (is_array($source) && isset($source[$field_name])) {
      $field = $source[$field_name];
    }

    // Loop over the source field value and copy its items to the target.
    if (is_array($field)) {
      foreach ($field as $langcode => $items) {
        if (is_array($items)) {
          $target[$field_name][$langcode] = array();
          foreach ($items as $delta => $item) {
            $target[$field_name][$langcode][$delta] = $item;
            $form_state['order']->data['profile_copy'][$pane_id]['elements'][$field_name][$langcode][$delta] = TRUE;
          }
        }
        else {
          $target[$field_name][$langcode] = $items;
          $form_state['order']->data['profile_copy'][$pane_id]['elements'][$field_name][$langcode] = TRUE;
        }
      }
    }
  }
}