class UcPrice in Ubercart 8.4
Provides a form element for Ubercart price input.
Plugin annotation
@FormElement("uc_price");
Hierarchy
- class \Drupal\uc_store\Element\UcPrice extends \Element\FormElement
Expanded class hierarchy of UcPrice
21 #type uses of UcPrice
- AddLineItemForm::buildForm in uc_order/
src/ Form/ AddLineItemForm.php - Form constructor.
- CartSettingsForm::buildForm in uc_cart/
src/ Form/ CartSettingsForm.php - Form constructor.
- CashOnDelivery::buildConfigurationForm in payment/
uc_payment_pack/ src/ Plugin/ Ubercart/ PaymentMethod/ CashOnDelivery.php - Form constructor.
- CreditCardTerminalForm::buildForm in payment/
uc_credit/ src/ Form/ CreditCardTerminalForm.php - Form constructor.
- FlatRate::buildConfigurationForm in shipping/
uc_quote/ src/ Plugin/ Ubercart/ ShippingQuote/ FlatRate.php - Form constructor.
File
- uc_store/
src/ Element/ UcPrice.php, line 13
Namespace
Drupal\uc_store\ElementView source
class UcPrice extends Element\FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
$config = \Drupal::config('uc_store.settings')
->get('currency');
$sign_flag = $config['symbol_after'];
$currency_sign = $config['symbol'];
return [
'#input' => TRUE,
'#size' => 15,
'#maxlength' => 15,
'#process' => [
[
$class,
'processAjaxForm',
],
],
'#element_validate' => [
[
$class,
'validatePrice',
],
],
'#pre_render' => [
[
$class,
'preRenderPrice',
],
],
'#theme' => 'input__textfield',
'#theme_wrappers' => [
'form_element',
],
'#field_prefix' => $sign_flag ? '' : $currency_sign,
'#field_suffix' => $sign_flag ? $currency_sign : '',
'#allow_negative' => FALSE,
'#empty_zero' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE && !empty($element['#default_value'])) {
$exact = rtrim(rtrim(number_format($element['#default_value'], 6, '.', ''), '0'), '.');
$round = number_format($element['#default_value'], \Drupal::config('uc_store.settings')
->get('currency.precision'), '.', '');
return $exact == rtrim($round, '0') ? $round : $exact;
}
elseif (empty($input) && empty($element['#required']) && !empty($element['#empty_zero'])) {
// Empty non-required prices should be treated as zero.
return 0;
}
}
/**
* Form element validation handler for #type 'uc_price'.
*
* Note that #required is validated by _form_validate() already.
*/
public static function validatePrice(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
if ($value === '') {
return;
}
$name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
// Ensure the input is numeric.
if (!is_numeric($value)) {
$form_state
->setError($element, t('%name must be a number.', [
'%name' => $name,
]));
return;
}
// Ensure that the input is not negative, if specified.
if (empty($element['#allow_negative']) && $value < 0) {
$form_state
->setError($element, t('%name must not be negative.', [
'%name' => $name,
]));
}
}
/**
* Prepares a #type 'uc_price' render element for theme_input().
*
* @param array $element
* An associative array containing the properties of the element.
* Properties used: #title, #value, #description, #size, #maxlength,
* #placeholder, #required, #attributes.
*
* @return array
* The $element with prepared variables ready for theme_input().
*/
public static function preRenderPrice(array $element) {
$element['#attributes']['type'] = 'number';
$element['#attributes']['step'] = 'any';
if (empty($element['#allow_negative'])) {
$element['#attributes']['min'] = 0;
}
Element::setAttributes($element, [
'id',
'name',
'value',
'size',
'maxlength',
'placeholder',
]);
static::setAttributes($element, [
'form-uc-price',
]);
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UcPrice:: |
public | function | ||
UcPrice:: |
public static | function | Prepares a #type 'uc_price' render element for theme_input(). | |
UcPrice:: |
public static | function | Form element validation handler for #type 'uc_price'. | |
UcPrice:: |
public static | function |