summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-08-20 14:36:47 (GMT)
committerAlex Pott2014-08-20 14:40:29 (GMT)
commit62edf5212d8930ec6b501e6d5897ed77759ef36c (patch)
tree898293a68dffc7a8dd18a2a63a56be5e506066d7
parent77c666c39ac2e5d1e81b42f50b0d00406288d85c (diff)
Issue #2319719 by Tim Bozeman, pcambra, undertext, bojanz: Fixed Widget validation crashes on ItemList violations.
-rw-r--r--core/lib/Drupal/Core/Field/WidgetBase.php6
-rw-r--r--core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php46
-rw-r--r--core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php51
-rw-r--r--core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraint.php23
-rw-r--r--core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraintValidator.php24
5 files changed, 145 insertions, 5 deletions
diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php
index d0cf50c..50f67a5 100644
--- a/core/lib/Drupal/Core/Field/WidgetBase.php
+++ b/core/lib/Drupal/Core/Field/WidgetBase.php
@@ -406,10 +406,6 @@ abstract class WidgetBase extends PluginSettingsBase implements WidgetInterface
// Separate violations by delta.
$property_path = explode('.', $violation->getPropertyPath());
$delta = array_shift($property_path);
- // Violations at the ItemList level are not associated to any delta,
- // we file them under $delta NULL.
- $delta = is_numeric($delta) ? $delta : NULL;
-
$violations_by_delta[$delta][] = $violation;
$violation->arrayPropertyPath = $property_path;
}
@@ -419,7 +415,7 @@ abstract class WidgetBase extends PluginSettingsBase implements WidgetInterface
// 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 || $delta === NULL) {
+ if ($handles_multiple || !is_numeric($delta)) {
$delta_element = $element;
}
// Otherwise, pass errors by delta to the corresponding sub-element.
diff --git a/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php
new file mode 100644
index 0000000..5fa873d
--- /dev/null
+++ b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\system\Tests\Entity\FieldWidgetConstraintValidatorTest.
+ */
+
+namespace Drupal\system\Tests\Entity;
+
+use Drupal\Core\Form\FormState;
+use Drupal\simpletest\KernelTestBase;
+use Drupal\system\Tests\TypedData;
+
+/**
+ * Tests validation constraints for FieldWidgetConstraintValidatorTest.
+ *
+ * @group Entity
+ */
+class FieldWidgetConstraintValidatorTest extends KernelTestBase {
+
+ public static $modules = array('entity', 'entity_test', 'field', 'user');
+
+ /**
+ * Tests widget constraint validation.
+ */
+ public function testValidation() {
+ $entity_type = 'entity_test_constraint_violation';
+ $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1));
+ $display = entity_get_form_display($entity_type, $entity_type, 'default');
+ $form = array();
+ $form_state = new FormState();
+ $display->buildForm($entity, $form, $form_state);
+
+ // Pretend the form has been built.
+ $form_state['build_info']['callback_object'] = \Drupal::entityManager()->getFormObject($entity_type, 'default');
+ \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form, $form_state);
+ \Drupal::formBuilder()->processForm('field_test_entity_form', $form, $form_state);
+
+ // Validate the field constraint.
+ $display->validateFormValues($entity, $form, $form_state);
+
+ $errors = $form_state->getErrors();
+ $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly');
+ }
+
+}
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
new file mode 100644
index 0000000..f90698d
--- /dev/null
+++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\entity_test\Entity\EntityTestConstraintViolation.
+ */
+
+namespace Drupal\entity_test\Entity;
+
+use Drupal\Core\Entity\EntityTypeInterface;
+
+/**
+ * Defines the test entity class for testing entity constraint violations.
+ *
+ * @ContentEntityType(
+ * id = "entity_test_constraint_violation",
+ * label = @Translation("Test entity constraint violation"),
+ * controllers = {
+ * "form" = {
+ * "default" = "Drupal\entity_test\EntityTestForm"
+ * }
+ * },
+ * base_table = "entity_test",
+ * fieldable = TRUE,
+ * persistent_cache = FALSE,
+ * entity_keys = {
+ * "id" = "id",
+ * "uuid" = "uuid",
+ * "bundle" = "type",
+ * "label" = "name"
+ * }
+ * )
+ */
+class EntityTestConstraintViolation extends EntityTest {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
+ $fields = parent::baseFieldDefinitions($entity_type);
+
+ $fields['name']->setDisplayOptions('form', array(
+ 'type' => 'string',
+ 'weight' => 0,
+ ));
+ $fields['name']->addConstraint('FieldWidgetConstraint', array());
+
+ return $fields;
+ }
+
+}
diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraint.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraint.php
new file mode 100644
index 0000000..ea2f448
--- /dev/null
+++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraint.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\entity_test\Plugin\Validation\Constraint\FieldWidgetConstraint.
+ */
+
+namespace Drupal\entity_test\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * Supports validating widget constraints.
+ *
+ * @Plugin(
+ * id = "FieldWidgetConstraint",
+ * label = @Translation("Field widget constraint.")
+ * )
+ */
+class FieldWidgetConstraint extends Constraint {
+
+ public $message = 'Widget constraint has failed.';
+
+}
diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraintValidator.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraintValidator.php
new file mode 100644
index 0000000..6e3c8b8
--- /dev/null
+++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/FieldWidgetConstraintValidator.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\entity_test\Plugin\Validation\Constraint\FieldWidgetConstraintValidator.
+ */
+
+namespace Drupal\entity_test\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+/**
+ * Validates the FieldWidgetConstraint constraint.
+ */
+class FieldWidgetConstraintValidator extends ConstraintValidator {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validate($field_item, Constraint $constraint) {
+ $this->context->addViolation($constraint->message);
+ }
+
+}