class RulesDataUICommercePrice in Commerce Core 7
Defines a commerce_price input form for Rules actions altering price fields.
Hierarchy
- class \RulesDataUI
- class \RulesDataUICommercePrice implements RulesDataDirectInputFormInterface
Expanded class hierarchy of RulesDataUICommercePrice
1 string reference to 'RulesDataUICommercePrice'
- commerce_price_rules_data_info in modules/
price/ commerce_price.rules.inc - Implements hook_rules_data_info().
File
- modules/
price/ commerce_price.rules.inc, line 29 - Rules integration for the Price module.
View source
class RulesDataUICommercePrice extends RulesDataUI implements RulesDataDirectInputFormInterface {
public static function getDefaultMode() {
return 'input';
}
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;
}
public static function render($value) {
return array(
'content' => array(
'#markup' => commerce_currency_format($value['amount'], $value['currency_code']),
),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RulesDataUI:: |
public static | function | Returns the data type and parameter information for the given arguments. | |
RulesDataUI:: |
public static | function | Renders the value with a label if an options list is available. | |
RulesDataUI:: |
public static | function | Provides the selection form for a parameter. | |
RulesDataUICommercePrice:: |
public static | function |
Specifies the default input mode per data type. Overrides RulesDataUI:: |
|
RulesDataUICommercePrice:: |
public static | function |
Constructs the direct input form. Overrides RulesDataDirectInputFormInterface:: |
|
RulesDataUICommercePrice:: |
public static | function |
Render the configured value. Overrides RulesDataDirectInputFormInterface:: |