DeletedWorkspaceConstraintValidator.php in Drupal 9
File
core/modules/workspaces/src/Plugin/Validation/Constraint/DeletedWorkspaceConstraintValidator.php
View source
<?php
namespace Drupal\workspaces\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\workspaces\WorkspaceAssociationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class DeletedWorkspaceConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
protected $workspaceAssociation;
public function __construct(WorkspaceAssociationInterface $workspace_association) {
$this->workspaceAssociation = $workspace_association;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('workspaces.association'));
}
public function validate($value, Constraint $constraint) {
if (!isset($value) || !$value
->getEntity()
->isNew()) {
return;
}
if ($this->workspaceAssociation
->getTrackedEntities($value
->getEntity()
->id())) {
$this->context
->addViolation($constraint->message);
}
}
}