You are here

function commerce_product_reference_options_list in Commerce Core 7

Implements hook_options_list().

File

modules/product_reference/commerce_product_reference.module, line 1081
Defines a field type for referencing products from other entities.

Code

function commerce_product_reference_options_list($field, $instance = NULL) {
  $options = array();

  // Look for an options list limit in the field settings.
  if (!empty($field['settings']['options_list_limit'])) {
    $limit = (int) $field['settings']['options_list_limit'];
  }
  else {

    // If there is no such limit, then default it to NULL (i.e. no limit) unless
    // a variable has been set that provides an alternate default. This is
    // usually required when product matching has been configured to use an
    // EntityFieldQuery instead of the standard db_select(). Refer to the inline
    // comments in commerce_product_match_products() for more information.
    $limit = variable_get('commerce_product_reference_default_options_list_limit', NULL);
  }

  // Loop through all product matches.
  foreach (commerce_product_match_products($field, $instance, '', 'contains', array(), $limit) as $product_id => $data) {

    // Add them to the options list in optgroups by product type.
    $name = check_plain(commerce_product_type_get_name($data['type']));
    if (!empty($instance['widget']['type']) && $instance['widget']['type'] == 'options_select') {
      $options[$name][$product_id] = t('!sku: !title', array(
        '!sku' => $data['sku'],
        '!title' => $data['title'],
      ));
    }
    else {
      $options[$name][$product_id] = t('@sku: @title', array(
        '@sku' => $data['sku'],
        '@title' => $data['title'],
      ));
    }
  }

  // Simplify the options list if only one optgroup exists.
  if (count($options) == 1) {
    $options = reset($options);
  }
  return $options;
}