diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php index 11f048e6f894d897f449aa8b0b1d0361010f7a6c..d7f9bded440dcbac6418fdb655bd6ad860d12e27 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php @@ -118,7 +118,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen * {@inheritdoc} */ public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) { - return isset($element['target_id']) ? $element['target_id'] : FALSE; + return $element['target_id']; } /** diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index bafdb7708c3ea0893ec54391e5009de44a974f04..63671576b2feb69d6983ccf114974e6bc8e2f045 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -423,21 +423,26 @@ public function flagErrors(FieldItemListInterface $items, ConstraintViolationLis if (Element::isVisibleElement($element)) { $handles_multiple = $this->handlesMultipleValues(); - $violations_by_delta = []; + $violations_by_delta = $item_list_violations = []; foreach ($violations as $violation) { // Separate violations by delta. $property_path = explode('.', $violation->getPropertyPath()); $delta = array_shift($property_path); - $violations_by_delta[$delta][] = $violation; + if (is_numeric($delta)) { + $violations_by_delta[$delta][] = $violation; + } + // Violations at the ItemList level are not associated to any delta. + else { + $item_list_violations[] = $violation; + } $violation->arrayPropertyPath = $property_path; } /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $delta_violations */ foreach ($violations_by_delta as $delta => $delta_violations) { - // Pass violations to the main element: - // - if this is a multiple-value widget, - // - or if the violations are at the ItemList level. - if ($handles_multiple || !is_numeric($delta)) { + // Pass violations to the main element if this is a multiple-value + // widget. + if ($handles_multiple) { $delta_element = $element; } // Otherwise, pass errors by delta to the corresponding sub-element. @@ -453,6 +458,13 @@ public function flagErrors(FieldItemListInterface $items, ConstraintViolationLis } } } + + /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $item_list_violations */ + // Pass violations to the main element without going through + // errorElement() if the violations are at the ItemList level. + foreach ($item_list_violations as $violation) { + $form_state->setError($element, $violation->getMessage()); + } } } } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php index e12c74a0d152584e5f06bfbe883a7b24485cf9d6..ea23d7968ff8797e6fcd6c0a47af8c51fe1d1823 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php @@ -3,6 +3,7 @@ namespace Drupal\entity_test\Entity; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; /** * Defines the test entity class for testing entity constraint violations. @@ -39,6 +40,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ]); $fields['name']->addConstraint('FieldWidgetConstraint', []); + // Add a field that uses a widget with a custom implementation for + // \Drupal\Core\Field\WidgetInterface::errorElement(). + $fields['test_field'] = BaseFieldDefinition::create('integer') + ->setLabel(t('Test field')) + ->setDisplayOptions('form', [ + 'type' => 'number', + 'weight' => 1, + ]) + ->addConstraint('FieldWidgetConstraint', []); + return $fields; } diff --git a/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php index f41e5b1e62ad3f8a20ddbc2e553a25cde8eef004..8f3730525e909612865f1e830b0d875c60c3366d 100644 --- a/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php +++ b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php @@ -87,15 +87,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { $element = parent::errorElement($element, $violation, $form, $form_state); - if ($element === FALSE) { - return FALSE; - } - elseif (isset($violation->arrayPropertyPath[0])) { - return $element[$violation->arrayPropertyPath[0]]; - } - else { - return $element; - } + return ($element === FALSE) ? FALSE : $element[$violation->arrayPropertyPath[0]]; } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php index 041b062f45260deca410a7d6f5f5129c64db9a33..21eb4d97ee7f01051d7fe8cd60c88d04473ef460 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php @@ -15,7 +15,7 @@ */ class FieldWidgetConstraintValidatorTest extends KernelTestBase { - public static $modules = ['entity_test', 'field', 'user', 'system']; + public static $modules = ['entity_test', 'field', 'field_test', 'user', 'system']; /** * {@inheritdoc} @@ -54,7 +54,8 @@ public function testValidation() { $display->validateFormValues($entity, $form, $form_state); $errors = $form_state->getErrors(); - $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly'); + $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation at the field items list level is generated correctly'); + $this->assertEqual($errors['test_field'], 'Widget constraint has failed.', 'Constraint violation at the field items list level is generated correctly for an advanced widget'); } /**