You are here

function _webform_edit_validate_productfield in Commerce Webform 8

Same name and namespace in other branches
  1. 7.2 productfield.inc \_webform_edit_validate_productfield()
  2. 7 productfield.inc \_webform_edit_validate_productfield()

Element validation callback. Ensure keys are not duplicated.

1 string reference to '_webform_edit_validate_productfield'
_webform_edit_productfield in ./productfield.inc
Implements _webform_edit_component().

File

./productfield.inc, line 119

Code

function _webform_edit_validate_productfield($element, &$form_state) {

  // If a value was entered into the autocomplete...
  if (!empty($element['#value'])) {

    // Translate SKUs into product IDs.
    $typed_skus = drupal_explode_tags($element['#value']);
    $value = array();

    // Loop through all the entered SKUs...
    foreach ($typed_skus as $typed_sku) {

      // To see if the product actually exists...
      if ($product = commerce_product_load_by_sku(trim($typed_sku))) {

        // And store its product ID for later validation.
        $value[$product->product_id] = array(
          'product_id' => $product->product_id,
          'title' => $product->title,
          'sku' => $product->sku,
          'type' => $product->type,
        );
      }
    }
  }
  else {
    $value = array();
  }

  // Update the value of this element so the field can validate the product IDs.
  form_set_value($element, $value, $form_state);
}