Issue #2390495 by amateescu, Berdir: Support marking field storage definitions as required

8.3.x
Alex Pott 2017-01-10 15:28:55 +00:00
parent 4f6dc9a303
commit cc30d65427
3 changed files with 63 additions and 1 deletions

View File

@ -12,7 +12,7 @@ use Drupal\Core\TypedData\OptionsProviderInterface;
/**
* 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 @@ class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionI
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;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Drupal\Core\Field;
/**
* Defines an interface for required field storage definitions.
*/
interface RequiredFieldStorageDefinitionInterface {
/**
* Returns whether the field storage is required.
*
* If a field storage is required, NOT NULL constraints will be added
* automatically for the required properties of a field type.
*
* @return bool
* TRUE if the field storage is required, FALSE otherwise.
*/
public function isStorageRequired();
}

View File

@ -254,6 +254,21 @@ class BaseFieldDefinitionTest extends UnitTestCase {
$this->assertFalse($definition->isRequired());
}
/**
* 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.
*