diff --git a/src/Entity/Key.php b/src/Entity/Key.php index 97a7077..af94afd 100644 --- a/src/Entity/Key.php +++ b/src/Entity/Key.php @@ -287,10 +287,27 @@ class Key extends ConfigEntityBase implements KeyInterface, EntityWithPluginColl * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { - // If the provider allows setting a key value and a value exists. - if ($this->getKeyProvider() instanceof KeyProviderSettableValueInterface && isset($this->keyValue)) { - // Set the key value using the key provider. - $this->getKeyProvider()->setKeyValue($this, $this->keyValue); + // If the key provider supports setting a key value. + if ($this->getKeyProvider() instanceof KeyProviderSettableValueInterface) { + // If a key value was defined. + if (isset($this->keyValue)) { + // Set the key value using the key provider. + $this->getKeyProvider()->setKeyValue($this, $this->keyValue); + } + // If a key value was not defined. + else { + // If the provider requires a key value, throw an exception. + if ($this->getKeyProvider()->getPluginDefinition()['key_value']['required']) { + throw new KeyValueNotSetException('The selected key provider requires a key value.'); + } + } + } + // If the key provider does not support setting a value. + else { + // If a key value was defined, throw an exception. + if (isset($this->keyValue)) { + throw new KeyValueNotSetException('The selected key provider does not support setting a key value.'); + } } parent::preSave($storage);