Issue #2390495 by amateescu, Berdir: Support marking field storage definitions as required
parent
4f6dc9a303
commit
cc30d65427
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue