diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index e28d9975b4..2ff671613e 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -12,7 +12,7 @@ /** * A class for defining entity fields. */ -class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface { +class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface { use UnchangingCacheableDependencyTrait; @@ -723,4 +723,30 @@ public function getConfig($bundle) { return BaseFieldOverride::createFromBaseFieldDefinition($this, $bundle); } + /** + * {@inheritdoc} + */ + public function isStorageRequired() { + if (isset($this->definition['storage_required'])) { + return (bool) $this->definition['storage_required']; + } + + // Default to the 'required' property of the base field. + return $this->isRequired(); + } + + /** + * Sets whether the field storage is required. + * + * @param bool $required + * Whether the field storage is required. + * + * @return static + * The object itself for chaining. + */ + public function setStorageRequired($required) { + $this->definition['storage_required'] = $required; + return $this; + } + } diff --git a/core/lib/Drupal/Core/Field/RequiredFieldStorageDefinitionInterface.php b/core/lib/Drupal/Core/Field/RequiredFieldStorageDefinitionInterface.php new file mode 100644 index 0000000000..d29e56907e --- /dev/null +++ b/core/lib/Drupal/Core/Field/RequiredFieldStorageDefinitionInterface.php @@ -0,0 +1,21 @@ +linkManager->getTypeInternalIds($type['href'], $context)) { diff --git a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php index d3bd85a9a9..836a1b4716 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php @@ -97,21 +97,18 @@ protected function assertNormalizationEdgeCases($method, Url $url, array $reques if ($this->entity->getEntityType()->hasKey('bundle')) { $normalization = $this->getNormalizedPostEntity(); - // @todo Uncomment this in https://www.drupal.org/node/2824827. - // @codingStandardsIgnoreStart -/* + $normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name'); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); // DX: 400 when incorrect entity type bundle is specified. $response = $this->request($method, $url, $request_options); // @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853. -// $this->assertResourceErrorResponse(400, 'The type link relation must be specified.', $response); + // $this->assertResourceErrorResponse(400, 'No entity type(s) specified', $response); $this->assertSame(400, $response->getStatusCode()); $this->assertSame([static::$mimeType], $response->getHeader('Content-Type')); - $this->assertSame($this->serializer->encode(['error' => 'The type link relation must be specified.'], static::$format), (string) $response->getBody()); -*/ - // @codingStandardsIgnoreEnd + $this->assertSame($this->serializer->encode(['error' => 'No entity type(s) specified'], static::$format), (string) $response->getBody()); + unset($normalization['_links']['type']); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); diff --git a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php index 8f64049917..3f9388bce4 100644 --- a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php @@ -75,6 +75,36 @@ public function testTypeHandling() { } /** + * Tests link relation handling with an invalid type. + */ + public function testTypeHandlingWithInvalidType() { + $data_with_invalid_type = array( + '_links' => array( + 'type' => array( + 'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', array('absolute' => TRUE))->toString(), + ), + ), + ); + + $this->setExpectedException(UnexpectedValueException::class); + $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format); + } + + /** + * Tests link relation handling with no types. + */ + public function testTypeHandlingWithNoTypes() { + $data_with_no_types = array( + '_links' => array( + 'type' => array(), + ), + ); + + $this->setExpectedException(UnexpectedValueException::class); + $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format); + } + + /** * Test that a field set to an empty array is different than an absent field. */ public function testMarkFieldForDeletion() { diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index dbd50e795e..bb4668306b 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -255,6 +255,21 @@ public function testFieldRequired() { } /** + * Tests storage required. + * + * @covers ::isStorageRequired + * @covers ::setStorageRequired + */ + public function testFieldStorageRequired() { + $definition = BaseFieldDefinition::create($this->fieldType); + $this->assertFalse($definition->isStorageRequired()); + $definition->setStorageRequired(TRUE); + $this->assertTrue($definition->isStorageRequired()); + $definition->setStorageRequired(FALSE); + $this->assertFalse($definition->isStorageRequired()); + } + + /** * Tests provider. * * @covers ::getProvider