EntityReferenceSupportedNewEntitiesConstraintValidator.php in Drupal 8
File
core/modules/workspaces/src/Plugin/Validation/Constraint/EntityReferenceSupportedNewEntitiesConstraintValidator.php
View source
<?php
namespace Drupal\workspaces\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\workspaces\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class EntityReferenceSupportedNewEntitiesConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
protected $workspaceManager;
protected $entityTypeManager;
public function __construct(WorkspaceManagerInterface $workspaceManager, EntityTypeManagerInterface $entityTypeManager) {
$this->workspaceManager = $workspaceManager;
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('workspaces.manager'), $container
->get('entity_type.manager'));
}
public function validate($value, Constraint $constraint) {
if (!$this->workspaceManager
->hasActiveWorkspace()) {
return;
}
$target_entity_type_id = $value
->getFieldDefinition()
->getFieldStorageDefinition()
->getSetting('target_type');
$target_entity_type = $this->entityTypeManager
->getDefinition($target_entity_type_id);
if ($value
->hasNewEntity() && !$this->workspaceManager
->isEntityTypeSupported($target_entity_type)) {
$this->context
->addViolation($constraint->message, [
'%collection_label' => $target_entity_type
->getCollectionLabel(),
]);
}
}
}