Skip to content
Snippets Groups Projects

throwing error while creating tags which have required field.

All threads resolved!
@@ -22,6 +22,7 @@
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageException;
/**
* Default plugin implementation of the Entity Reference Selection plugin.
@@ -416,7 +417,6 @@ public function validateReferenceableEntities(array $ids) {
*/
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$values = [
$entity_type->getKey('label') => $label,
];
@@ -431,6 +431,16 @@ public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$entity->setOwnerId($uid);
}
$violations = $entity->validate();
if ($violations->count() > 0) {
$messages = [];
foreach ($violations as $violation) {
$messages[] = $violation->getPropertyPath() . ': ' . $violation->getMessage();
}
$message = "Cannot auto-create entity due to validation errors: " . implode('; ', $messages);
throw new EntityStorageException($message);
}
return $entity;
}
Loading