You are here

function commerce_customer_profile_manager_validate in Commerce Core 7

Validation callback for a commerce_customer_profile_manager element.

When the form is submitted, the profile reference field stores the profile IDs as derived from the $element['profiles'] array and updates any referenced profiles based on the extra form elements.

1 string reference to 'commerce_customer_profile_manager_validate'
commerce_customer_field_widget_form in modules/customer/commerce_customer.module
Implements hook_field_widget_form().

File

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

Code

function commerce_customer_profile_manager_validate($element, &$form_state, $form) {
  $value = array();
  $triggering_element = $form_state['triggering_element'];

  // If the triggering element is not the "add profile" button and it wants to
  // limit validation errors and the form is not going to be submitted...
  if (!isset($triggering_element['#commerce_customer_profile_type']) && isset($triggering_element['#limit_validation_errors']) && $triggering_element['#limit_validation_errors'] !== FALSE && !($form_state['submitted'] && !isset($triggering_element['#submit']))) {

    // Ensure this element wasn't specifically marked for validation in the
    // #limit_validation_errors sections array.
    $section_match = FALSE;
    foreach ($triggering_element['#limit_validation_errors'] as $section) {

      // Because #limit_validation_errors sections force validation for any
      // element that matches the section or is a child of it, we can consider
      // it a match if the section completely matches the beginning of this
      // element's #parents array even if #parents contains additional elements.
      if (array_intersect_assoc($section, $element['#parents']) === $section) {
        $section_match = TRUE;
      }
    }

    // Exit this validate function, because the form is going to be rebuilt and
    // the data submitted may very well be incomplete.
    if (!$section_match) {
      form_set_value($element, array(), $form_state);
      return;
    }
  }

  // Loop through the profiles in the manager table.
  foreach (element_children($element['profiles']) as $key) {
    if (!isset($element['profiles'][$key]['profile'])) {

      // There's no profile of this type.
      continue;
    }

    // Update the profile based on the values in the additional elements.
    $profile = clone $element['profiles'][$key]['profile']['#value'];
    $cancel = $triggering_element['#parents'][count($triggering_element['#parents']) - 1] === 'cancel_' . $profile->type;

    // If the profile adding was canceled or it has been marked for deletion...
    if ($cancel || $profile->profile_id && $element['profiles'][$key]['remove']['#value']) {

      // Delete the profile now if we can and don't include it in the $value array.
      if (commerce_customer_profile_can_delete($profile)) {

        // If another module altered in an entity context, be sure to pass it to
        // the delete function.
        if (!empty($profile->entity_context)) {
          commerce_customer_profile_delete($profile->profile_id, $profile->entity_context);
        }
        else {
          commerce_customer_profile_delete($profile->profile_id);
        }
      }
    }
    else {

      // Notify field widgets to validate their data.
      field_attach_form_validate('commerce_customer_profile', $profile, $element['profiles'][$key], $form_state);

      // TODO: Trap it on error, rebuild the form with error messages.
      // Notify field widgets to save the field data.
      field_attach_submit('commerce_customer_profile', $profile, $element['profiles'][$key], $form_state);

      // Only save if values were actually changed.
      if ($profile != $element['profiles'][$key]['profile']['#value']) {
        commerce_customer_profile_save($profile);
      }

      // Add the profile ID to the current value of the reference field.
      $value[] = array(
        'profile_id' => $profile->profile_id,
      );
    }
  }
  form_set_value($element, $value, $form_state);
}