Skip to content

Commit aafd17c

Browse files
jsacksickbojanz
authored andcommitted
Issue #2509702 by jsacksick, bojanz: Inline errors look terrible on the price field
1 parent 40f94d2 commit aafd17c

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

modules/price/src/Element/Price.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function getInfo() {
3434
return [
3535
// List of currencies codes. If empty, all currencies will be available.
3636
'#available_currencies' => [],
37+
// The check is performed here so that it is cached.
38+
'#price_inline_errors' => \Drupal::moduleHandler()->moduleExists('inline_form_errors'),
3739

3840
'#size' => 10,
3941
'#maxlength' => 128,
@@ -42,6 +44,9 @@ public function getInfo() {
4244
'#attached' => [
4345
'library' => ['commerce_price/admin'],
4446
],
47+
'#element_validate' => [
48+
[$class, 'moveInlineErrors'],
49+
],
4550
'#process' => [
4651
[$class, 'processElement'],
4752
[$class, 'processAjaxForm'],
@@ -112,6 +117,7 @@ public static function processElement(array $element, FormStateInterface $form_s
112117
// '6' is the field storage maximum.
113118
'#max_fraction_digits' => 6,
114119
'#min' => $element['#allow_negative'] ? NULL : 0,
120+
'#error_no_message' => TRUE,
115121
];
116122
if (isset($element['#ajax'])) {
117123
$element['number']['#ajax'] = $element['#ajax'];
@@ -144,7 +150,7 @@ public static function processElement(array $element, FormStateInterface $form_s
144150
if (!empty($element['#description'])) {
145151
$element[$last_visible_element]['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
146152
}
147-
// Remove the keys that were transfered to child elements.
153+
// Remove the keys that were transferred to child elements.
148154
unset($element['#size']);
149155
unset($element['#maxlength']);
150156
unset($element['#ajax']);
@@ -171,4 +177,24 @@ public static function validateDefaultValue($default_value) {
171177
return TRUE;
172178
}
173179

180+
/**
181+
* Moves inline errors from the "number" element to the main element.
182+
*
183+
* This ensures that they are displayed in the right place
184+
* (below both number and currency_code, instead of between them).
185+
*
186+
* Only performed when the inline_form_errors module is installed.
187+
*
188+
* @param array $element
189+
* The form element.
190+
* @param \Drupal\Core\Form\FormStateInterface $form_state
191+
* The current state of the form.
192+
*/
193+
public static function moveInlineErrors(array $element, FormStateInterface $form_state) {
194+
$error = $form_state->getError($element['number']);
195+
if (!empty($error) && !empty($element['#price_inline_errors'])) {
196+
$form_state->setError($element, $error);
197+
}
198+
}
199+
174200
}

0 commit comments

Comments
 (0)