class WorkflowsFieldContraintValidator in Workflows Field 8
Same name and namespace in other branches
- 2.x src/Plugin/Validation/Constraint/WorkflowsFieldContraintValidator.php \Drupal\workflows_field\Plugin\Validation\Constraint\WorkflowsFieldContraintValidator
Validates the workflows field.
Hierarchy
- class \Drupal\workflows_field\Plugin\Validation\Constraint\WorkflowsFieldContraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of WorkflowsFieldContraintValidator
File
- src/
Plugin/ Validation/ Constraint/ WorkflowsFieldContraintValidator.php, line 15
Namespace
Drupal\workflows_field\Plugin\Validation\ConstraintView source
class WorkflowsFieldContraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Drupal\Core\Session\AccountProxy definition.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* Creates an instance of WorkflowsFieldContraintValidator.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountInterface $currentUser) {
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('current_user'));
}
/**
* {@inheritdoc}
*/
public function validate($field, Constraint $constraint) {
/** @var \Drupal\workflows_field\Plugin\Field\FieldType\WorkflowsFieldItem $field */
$entity = $field
->getEntity();
$workflow_type = $field
->getWorkflow()
->getTypePlugin();
// An entity can start its life in any state.
if (!isset($field->value) || $entity
->isNew()) {
return;
}
$original_entity = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadUnchanged($entity
->id());
if (!$entity
->isDefaultTranslation() && $original_entity
->hasTranslation($entity
->language()
->getId())) {
$original_entity = $original_entity
->getTranslation($entity
->language()
->getId());
}
$previous_state = $original_entity->{$field
->getFieldDefinition()
->getName()}->value;
// The state does not have to change.
if ($previous_state === $field->value) {
return;
}
if (!$workflow_type
->hasTransitionFromStateToState($previous_state, $field->value)) {
$this->context
->addViolation($constraint->message, [
'%state' => $field->value,
'%previous_state' => $previous_state,
]);
}
else {
$transition = $workflow_type
->getTransitionFromStateToState($previous_state, $field->value);
if (!$this->currentUser
->hasPermission(sprintf('use %s transition %s', $field
->getWorkflow()
->id(), $transition
->id()))) {
$this->context
->addViolation($constraint->insufficientPermissionsTransition, [
'%transition' => $transition
->label(),
]);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
WorkflowsFieldContraintValidator:: |
protected | property | Drupal\Core\Session\AccountProxy definition. | |
WorkflowsFieldContraintValidator:: |
protected | property | Entity type manager. | |
WorkflowsFieldContraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
WorkflowsFieldContraintValidator:: |
public | function | ||
WorkflowsFieldContraintValidator:: |
public | function | Creates an instance of WorkflowsFieldContraintValidator. |