View source
<?php
namespace Drupal\price\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
class PriceModified extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#available_currencies' => [],
'#available_modifiers' => [],
'#size' => 10,
'#maxlength' => 128,
'#default_value' => NULL,
'#allow_negative' => FALSE,
'#attached' => [
'library' => [
'price/admin',
],
],
'#process' => [
[
$class,
'processElement',
],
[
$class,
'processAjaxForm',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderGroup',
],
],
'#input' => TRUE,
'#theme_wrappers' => [
'container',
],
];
}
public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) {
$default_value = $element['#default_value'];
if (isset($default_value) && !self::validateDefaultValue($default_value)) {
throw new \InvalidArgumentException('The #default_value for a price_modified element must be an array with "number", "currency_code" and "modifier" keys.');
}
$currency_storage = \Drupal::service('entity_type.manager')
->getStorage('price_currency');
$currencies = $currency_storage
->loadMultiple();
$currency_codes = array_keys($currencies);
$available_currencies = $element['#available_currencies'];
if (isset($available_currencies) && !empty($available_currencies)) {
$currency_codes = array_intersect($currency_codes, $available_currencies);
}
if (empty($currency_codes)) {
return $element;
}
$fraction_digits = [];
foreach ($currencies as $currency) {
$fraction_digits[] = $currency
->getFractionDigits();
}
$modifier_storage = \Drupal::service('entity_type.manager')
->getStorage('price_modifier');
$modifiers = $modifier_storage
->loadMultiple();
$modifier_codes = array_keys($modifiers);
$available_modifiers = $element['#available_modifiers'];
if (isset($available_modifiers) && !empty($available_modifiers)) {
$modifier_codes = array_intersect($modifier_codes, $available_modifiers);
}
if (empty($modifier_codes)) {
return $element;
}
$modifier_options = [];
foreach ($modifier_codes as $modifier_code) {
$modifier_options[$modifier_code] = $modifiers[$modifier_code]
->label();
}
$element['#tree'] = TRUE;
$element['#attributes']['class'][] = 'form-type-price-modified';
$element['modifier'] = [
'#type' => 'select',
'#title' => t('Modifier'),
'#default_value' => $default_value ? $default_value['modifier'] : NULL,
'#options' => $modifier_options,
];
$element['number'] = [
'#type' => 'price_number',
'#title' => $element['#title'],
'#default_value' => $default_value ? $default_value['number'] : NULL,
'#required' => $element['#required'],
'#size' => $element['#size'],
'#maxlength' => $element['#maxlength'],
'#min_fraction_digits' => min($fraction_digits),
'#max_fraction_digits' => 6,
'#min' => $element['#allow_negative'] ? NULL : 0,
];
unset($element['#size']);
unset($element['#maxlength']);
if (count($currency_codes) == 1) {
$last_visible_element = 'number';
$currency_code = reset($currency_codes);
$element['number']['#field_suffix'] = $currency_code;
$element['currency_code'] = [
'#type' => 'hidden',
'#value' => $currency_code,
];
}
else {
$last_visible_element = 'currency_code';
$element['currency_code'] = [
'#type' => 'select',
'#title' => t('Currency'),
'#default_value' => $default_value ? $default_value['currency_code'] : NULL,
'#options' => array_combine($currency_codes, $currency_codes),
'#title_display' => 'invisible',
'#field_suffix' => '',
];
}
if (!empty($element['#description'])) {
$element[$last_visible_element]['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
}
return $element;
}
public static function validateDefaultValue($default_value) {
if (!is_array($default_value)) {
return FALSE;
}
if (!array_key_exists('number', $default_value) || !array_key_exists('currency_code', $default_value) || !array_key_exists('modifier', $default_value)) {
return FALSE;
}
return TRUE;
}
}