function commerce_price_field_widget_form in Commerce Core 7
Implements hook_field_widget_form().
File
- modules/
price/ commerce_price.module, line 612 - Defines the Price field with widgets and formatters used to add prices with currency codes to various Commerce entities.
Code
function commerce_price_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Use the default currency if the setting is not present.
if (empty($instance['widget']['settings']['currency_code']) || $instance['widget']['settings']['currency_code'] == 'default') {
$default_currency_code = NULL;
}
else {
$default_currency_code = $instance['widget']['settings']['currency_code'];
}
// If a price has already been set for this instance prepare default values.
if (isset($items[$delta]['amount']) && $items[$delta]['amount'] !== '') {
$currency = commerce_currency_load($items[$delta]['currency_code']);
// Convert the price amount to a user friendly decimal value.
$default_amount = commerce_currency_amount_to_decimal($items[$delta]['amount'], $currency['code']);
// Run it through number_format() to ensure it has the proper number of
// decimal places.
$default_amount = number_format($default_amount, $currency['decimals'], '.', '');
$default_currency_code = $items[$delta]['currency_code'];
}
else {
$default_amount = NULL;
}
// Load the default currency for this instance.
$default_currency = commerce_currency_load($default_currency_code);
$element['#attached']['css'][] = drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css';
// Build the form based on the type of price widget.
switch ($instance['widget']['type']) {
// The simple widget is just a textfield with a non-changeable currency.
case 'commerce_price_simple':
$element['amount'] = array(
'#type' => 'textfield',
'#title' => $element['#title'],
'#default_value' => $default_amount,
'#required' => $instance['required'] && ($delta == 0 || $field['cardinality'] > 0),
'#size' => 10,
'#field_suffix' => $default_currency['code'],
);
// Add the help text if specified.
if (!empty($element['#description'])) {
$element['amount']['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
}
$element['currency_code'] = array(
'#type' => 'value',
'#default_value' => $default_currency['code'],
);
break;
// The full widget is a textfield with a currency select list.
case 'commerce_price_full':
$element['amount'] = array(
'#type' => 'textfield',
'#title' => $element['#title'],
'#default_value' => $default_amount,
'#required' => $instance['required'] && ($delta == 0 || $field['cardinality'] > 0),
'#size' => 10,
);
// Build a currency options list from all enabled currencies.
$options = array();
foreach (commerce_currencies(TRUE) as $currency_code => $currency) {
$options[$currency_code] = check_plain($currency['code']);
}
// If the current currency value is not available, add it now with a
// message in the help text explaining it.
if (empty($options[$default_currency['code']])) {
$options[$default_currency['code']] = check_plain($default_currency['code']);
$description = t('The currency set for this price is not currently enabled. If you change it now, you will not be able to set it back.');
}
else {
$description = '';
}
// If only one currency option is available, don't use a select list.
if (count($options) == 1) {
$currency_code = key($options);
$element['amount']['#field_suffix'] = $currency_code;
// Add the help text if specified.
if (!empty($element['#description'])) {
$element['amount']['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
}
$element['currency_code'] = array(
'#type' => 'value',
'#default_value' => $currency_code,
);
}
else {
$element['amount']['#prefix'] = '<div class="commerce-price-full">';
$element['currency_code'] = array(
'#type' => 'select',
'#description' => $description,
'#options' => $options,
'#default_value' => isset($items[$delta]['currency_code']) ? $items[$delta]['currency_code'] : $default_currency['code'],
'#suffix' => '</div>',
);
// Add the help text if specified.
if (!empty($element['#description'])) {
$element['currency_code']['#suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
}
}
break;
}
$element['data'] = array(
'#type' => 'value',
'#default_value' => !empty($items[$delta]['data']) ? $items[$delta]['data'] : array(
'components' => array(),
),
);
$element['#element_validate'][] = 'commerce_price_field_widget_validate';
return $element;
}