Commit cc30d654 authored by alexpott's avatar alexpott

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

parent 4f6dc9a3
......@@ -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;
}
}
<?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 @@ public function testFieldRequired() {
$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.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment