diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index 3ca2920..1654719 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -282,7 +282,7 @@ function testFieldAttachCache() { $this->assertEqual($cache->data[$this->field_name_2][$langcode], $values, 'Cached: correct cache entry on load'); // Update with different values, and check that the cache entry is wiped. - $values = $this->_generateTestFieldValues($this->field_2['cardinality']); + $values = $this->_generateTestFieldValues($this->field_2->getFieldCardinality()); $entity = entity_create($entity_type, array( 'type' => $entity_type, 'id' => $entity->id(), @@ -302,8 +302,8 @@ function testFieldAttachCache() { 'type' => $entity_type, 'id' => $entity->id(), )); - $values = $this->_generateTestFieldValues($this->field_2['cardinality']); - $entity->{$this->field_name} = $values; + $values = $this->_generateTestFieldValues($this->field_2->getFieldCardinality()); + $entity->{$this->field_name_2} = $values; $entity->setNewRevision(); $entity->save(); $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry on new revision creation'); @@ -429,13 +429,13 @@ function testFieldAttachExtractFormValues() { $expected_values[] = array('value' => $values[$key]['value']); } } - $this->assertIdentical($entity->{$this->field_name}->getValue(), $expected_values, 'Submit filters empty values'); + $this->assertIdentical($entity->{$this->field_name}->getValue(), $expected_values);//, 'Submit filters empty values'); foreach ($weights_2 as $key => $value) { if ($key != 1) { $expected_values_2[] = array('value' => $values_2[$key]['value']); } } - $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); + $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2);//, 'Submit filters empty values'); // Call field_attach_extract_form_values() for a single field (the second field). $options = array('field_name' => $this->field_name_2); @@ -447,8 +447,8 @@ function testFieldAttachExtractFormValues() { $expected_values_2[] = array('value' => $values_2[$key]['value']); } } - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'The first field does is empty in the entity object'); - $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); + $this->assertTrue($entity->{$this->field_name}->isEmpty());//, 'The first field does is empty in the entity object'); + $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2);//, 'Submit filters empty values'); } } diff --git a/core/modules/field/tests/modules/field_test/field_test.field.inc b/core/modules/field/tests/modules/field_test/field_test.field.inc index 88ffe87..1952d8d 100644 --- a/core/modules/field/tests/modules/field_test/field_test.field.inc +++ b/core/modules/field/tests/modules/field_test/field_test.field.inc @@ -47,44 +47,11 @@ function field_test_field_load($entity_type, $entities, $field, $instances, $lan } } - /** - * Implements hook_field_validate(). - * - * Possible error codes: - * - 'field_test_invalid': The value is invalid. + * Sample 'default value' callback. */ -function field_test_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) { - $args = func_get_args(); - field_test_memorize(__FUNCTION__, $args); - - foreach ($items as $delta => $item) { - if ($item['value'] == -1) { - $errors[$field['field_name']][$langcode][$delta][] = array( - 'error' => 'field_test_invalid', - 'message' => t('%name does not accept the value -1.', array('%name' => $instance['label'])), - ); - } - } -} - -/** - * Implements hook_field_is_empty(). - */ -function field_test_field_is_empty($item, $field_type) { - return empty($item['shape']) && empty($item['color']); -} - -/** - * Form element validation handler for 'test_field_widget_multiple' widget. - */ -function field_test_widget_multiple_validate($element, &$form_state) { - $values = array_map('trim', explode(',', $element['#value'])); - $items = array(); - foreach ($values as $value) { - $items[] = array('value' => $value); - } - form_set_value($element, $items, $form_state); +function field_test_default_value(EntityInterface $entity, $field, $instance) { + return array(array('value' => 99)); } /** diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Validation/Constraint/TestFieldConstraint.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Validation/Constraint/TestFieldConstraint.php new file mode 100644 index 0000000..fb258a6 --- /dev/null +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Validation/Constraint/TestFieldConstraint.php @@ -0,0 +1,61 @@ +compare) { + $this->context->addViolation($constraint->message, array('%name' => $constraint->name, '@compare' => $constraint->compare)); + } + } + +} diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php similarity index 92% rename from core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php rename to core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php index ba3d698..2f8e558 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php @@ -11,10 +11,10 @@ use Drupal\Core\Annotation\Translation; /** - * Defines the 'hidden_test_field' entity field item. + * Defines the 'hidden_test' entity field item. * * @FieldType( - * id = "hidden_test_field", + * id = "hidden_test", * label = @Translation("Hidden from UI test field"), * description = @Translation("Dummy hidden field type used for tests."), * default_widget = "test_field_widget", diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php similarity index 92% rename from core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php rename to core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php index cf94bcb..e7d8c7a 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php @@ -16,7 +16,7 @@ * Defines the 'shape_field' entity field item. * * @FieldType( - * id = "shape_field", + * id = "shape", * label = @Translation("Shape"), * description = @Translation("Another dummy field type."), * settings = { @@ -87,4 +87,12 @@ public static function schema(FieldInterface $field) { ) + $foreign_keys; } + /** + * {@inheritdoc} + */ + public function isEmpty() { + $item = $this->getValue(); + return empty($item['shape']) && empty($item['color']); + } + } diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php index b2aa0f6..7f39177 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php @@ -30,7 +30,8 @@ * "test_hook_field_load" = FALSE * }, * default_widget = "test_field_widget", - * default_formatter = "field_test_default" + * default_formatter = "field_test_default", + * default_value_function = "field_test_default_value" * ) */ class TestItem extends ConfigFieldItemBase implements PrepareCacheInterface { @@ -122,4 +123,31 @@ public function prepareCache() { } } + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); + $constraints = parent::getConstraints(); + + $constraints[] = $constraint_manager->create('ComplexData', array( + 'value' => array( + 'TestField' => array( + 'compare' => -1, + 'name' => $this->getFieldDefinition()->getFieldLabel(), + ) + ), + )); + + return $constraints; + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $value = $this->get('value')->getValue(); + return empty($value); + } + } diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetMultiple.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetMultiple.php index 86d7dde..eca862f 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetMultiple.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/widget/TestFieldWidgetMultiple.php @@ -66,7 +66,7 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang $element += array( '#type' => 'textfield', '#default_value' => implode(', ', $values), - '#element_validate' => array('field_test_widget_multiple_validate'), + '#element_validate' => array(array(get_class($this), 'multipleValidate')), ); return $element; } @@ -78,4 +78,16 @@ public function errorElement(array $element, ConstraintViolationInterface $error return $element; } + /** + * Element validation helper. + */ + public static function multipleValidate($element, &$form_state) { + $values = array_map('trim', explode(',', $element['#value'])); + $items = array(); + foreach ($values as $value) { + $items[] = array('value' => $value); + } + form_set_value($element, $items, $form_state); + } + }