You are here

function commerce_webform_autocomplete in Commerce Webform 8

Same name and namespace in other branches
  1. 7.2 commerce_webform.module \commerce_webform_autocomplete()
  2. 7 commerce_webform.module \commerce_webform_autocomplete()

Returns output for product autocompletes.

The values returned will be keyed by SKU and appear as such in the textfield, even though the preview in the autocomplete list shows "SKU: Title".

1 string reference to 'commerce_webform_autocomplete'
commerce_webform_menu in ./commerce_webform.module
Implements hook_menu().

File

./commerce_webform.module, line 218
Commerce Webform module file

Code

function commerce_webform_autocomplete($string = '') {
  $matches = array();

  // The user enters a comma-separated list of tags.
  // We only autocomplete the last tag.
  $tags_typed = drupal_explode_tags($string);
  $tag_last = drupal_strtolower(array_pop($tags_typed));
  if (!empty($tag_last)) {
    $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
    $products = commerce_product_match_products(array(
      'field_name' => 'commerce_webform',
    ), NULL, $tag_last, 'contains', array(), 10);

    // Loop through the products and convert them into autocomplete output.
    foreach ($products as $product_id => $data) {

      // Add a class wrapper for a few required CSS overrides.
      $matches[$prefix . $data['sku']] = '<div class="reference-autocomplete">' . $data['rendered'] . '</div>';
    }
  }
  drupal_json_output($matches);
}