public static function RulesDataUICommercePrice::inputForm in Commerce Core 7
Constructs the direct input form.
Return value
array The direct input form.
Overrides RulesDataDirectInputFormInterface::inputForm
File
- modules/
price/ commerce_price.rules.inc, line 34 - Rules integration for the Price module.
Class
- RulesDataUICommercePrice
- Defines a commerce_price input form for Rules actions altering price fields.
Code
public static function inputForm($name, $info, $settings, RulesPlugin $element) {
$settings += array(
$name => isset($info['default value']) ? $info['default value'] : array(
'amount' => NULL,
'currency_code' => NULL,
),
);
$value = $settings[$name];
// Legacy data stored price amount as a scalar value, so we convert it here
// to the expected data structure.
if (is_scalar($value)) {
$value = array(
'amount' => $value,
'currency_code' => NULL,
);
}
$currency_code = empty($value['currency_code']) || $value['currency_code'] == 'default' ? commerce_default_currency() : $value['currency_code'];
$currency = commerce_currency_load($currency_code);
if (isset($value['amount']) && is_numeric($value['amount'])) {
// Price amount should always be saved as integers (minor units), but in
// case they're not we round them.
if (strpos($value['amount'], '.') === FALSE) {
$default_amount = $value['amount'];
}
else {
$default_amount = commerce_round(COMMERCE_ROUND_HALF_UP, $value['amount']);
}
// Format the number to the proper decimal places for the textfield.
$default_amount = commerce_currency_amount_to_decimal($default_amount, $currency_code);
$currency = commerce_currency_load($currency_code);
$default_amount = number_format($default_amount, $currency['decimals'], '.', '');
}
else {
$default_amount = NULL;
}
$form[$name]['#element_validate'] = array(
'_commerce_price_rules_data_ui_element_validate',
);
$form[$name]['amount'] = array(
'#type' => 'textfield',
'#default_value' => $default_amount,
'#size' => 10,
'#required' => TRUE,
);
// Build a currency options list from all enabled currencies.
$options = array();
foreach (commerce_currencies(TRUE) as $currency_key => $currency_data) {
$options[$currency_key] = check_plain($currency_data['code']);
}
// If the current currency value is not available, add it now with a message
// in the help text explaining it.
if (empty($options[$currency['code']])) {
$options[$currency['code']] = check_plain($currency['code']);
$description = t('The currency set for this action 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) {
$form[$name]['amount']['#field_suffix'] = reset($options);
$form[$name]['currency_code'] = array(
'#type' => 'value',
'#default_value' => key($options),
);
}
else {
$form[$name]['#attached']['css'][] = drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css';
$form[$name]['amount']['#prefix'] = '<div class="commerce-price-full">';
$form[$name]['currency_code'] = array(
'#type' => 'select',
'#description' => $description,
'#options' => $options,
'#default_value' => $currency_code,
'#suffix' => '</div>',
);
}
return $form;
}