function commerce_order_field_attach_form in Commerce Core 7
Implements hook_field_attach_form().
This function adds a property to the customer profiles stored in a form array on order edit forms. The order module will use this to bypass considering that order when determining if the user should be able to delete a profile from that order's edit form.
File
- modules/
order/ commerce_order.module, line 415 - Defines the core Commerce order entity and API functions to manage orders and interact with them.
Code
function commerce_order_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
// If this is an order edit form...
if ($entity_type == 'commerce_order') {
// Get an array of customer profile reference fields attached to products.
$fields = commerce_info_fields('commerce_customer_profile_reference', 'commerce_order');
// Loop over each child element of the form.
foreach (element_children($form) as $key) {
// If the current element is for a customer profile reference field...
if (array_key_exists($key, $fields)) {
// Loop over each of its child items...
foreach (element_children($form[$key][$form[$key]['#language']]) as $delta) {
foreach (element_children($form[$key][$form[$key]['#language']][$delta]) as $subdelta) {
if (!isset($form[$key][$form[$key]['#language']][$delta][$subdelta]['profile'])) {
// There's no profile of this type for this order.
continue;
}
// Extract the customer profile from the widget form element.
$profile = $form[$key][$form[$key]['#language']][$delta][$subdelta]['profile']['#value'];
// Add the order context to the profile stored in the field element.
$profile->entity_context = array(
'entity_type' => 'commerce_order',
'entity_id' => $entity->order_id,
);
// Add a uid if it is empty and the order has one.
if (empty($profile->uid) && !empty($entity->uid)) {
$profile->uid = $entity->uid;
}
// If this means this profile can now be deleted and the reference
// field widget includes a remove element...
if ($profile->profile_id && commerce_customer_profile_can_delete($profile) && !empty($form[$key][$form[$key]['#language']][$delta][$subdelta]['remove'])) {
// Update its remove element's title accordingly.
$form[$key][$form[$key]['#language']][$delta][$subdelta]['remove']['#title'] = t('Delete this profile');
}
}
}
}
}
}
}