function synonyms_commerce_field_widget_form in Synonyms 7
Implements hook_field_widget_form().
File
- synonyms_commerce/
synonyms_commerce.module, line 89 - Provides synonyms integration with Commerce.
Code
function synonyms_commerce_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$default_value = synonyms_select_default_value($field, $instance, $items);
switch ($instance['widget']['type']) {
case 'synonyms_commerce_autocomplete':
$default_value_string = array();
foreach (commerce_product_load_multiple($default_value) as $product) {
$default_value_string[] = entity_label('commerce_product', $product);
}
$default_value_string = drupal_implode_tags($default_value_string);
$element += array(
'#type' => 'textfield',
'#default_value' => $default_value_string,
'#autocomplete_path' => $instance['widget']['settings']['synonyms_autocomplete_path'] . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'],
'#size' => $instance['widget']['settings']['size'],
'#maxlength' => 1024,
'#element_validate' => array(
'synonyms_commerce_autocomplete_validate',
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'synonyms') . '/js/synonyms-autocomplete.js' => array(),
),
),
'#attributes' => array(
'class' => array(
'synonyms-autocomplete',
),
),
);
if (isset($instance['widget']['settings']['auto_creation'])) {
$element['#auto_creation'] = $instance['widget']['settings']['auto_creation'];
}
break;
case 'synonyms_commerce_select':
$multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$options = module_invoke('commerce_product_reference', 'options_list', $field, $instance);
$tmp = reset($options);
if (is_array($tmp)) {
// These options have optgroups.
foreach ($options as $k => $v) {
$options[$k] = synonyms_commerce_product_options_expand($v);
}
}
else {
$options = synonyms_commerce_product_options_expand($options);
}
$element += array(
'#type' => 'select',
'#empty_option' => t('- None -'),
'#options' => $options,
'#multiple' => $multiple,
'#element_validate' => array(
'synonyms_select_validate',
'synonyms_select_form_to_storage',
),
'#default_value' => $default_value,
);
break;
}
return $element;
}