class TransitionAccessConstraintValidator in Scheduler content moderation integration 8
Validator for the TransitionAccessConstraint.
Hierarchy
- class \Drupal\scheduler_content_moderation_integration\Plugin\Validation\Constraint\ConstraintValidatorBase extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
- class \Drupal\scheduler_content_moderation_integration\Plugin\Validation\Constraint\TransitionAccessConstraintValidator
Expanded class hierarchy of TransitionAccessConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ TransitionAccessConstraintValidator.php, line 10
Namespace
Drupal\scheduler_content_moderation_integration\Plugin\Validation\ConstraintView source
class TransitionAccessConstraintValidator extends ConstraintValidatorBase {
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
/** @var \Drupal\Core\Field\FieldItemList $value */
assert($constraint instanceof TransitionAccessConstraint);
$entity = $value
->getEntity();
// No need to validate entities that are not moderated.
if (!$this->moderationInformation
->isModeratedEntity($entity)) {
return;
}
// No need to validate if a moderation state has not ben set.
if ($value
->isEmpty()) {
return;
}
$field_name = $value
->getName();
// No need to validate when there is no time set.
if ($field_name === 'publish_state' && !isset($entity->publish_on->value) || $field_name === 'unpublish_state' && !isset($entity->unpublish_on->value)) {
return;
}
$from_state = $entity->moderation_state->value;
$to_state = $value->value;
// No need to validate if transition does not exist.
if (!$this
->isValidTransition($entity, $from_state, $to_state)) {
return;
}
$workflow = $this->moderationInformation
->getWorkflowForEntity($entity);
$workflow_type = $workflow
->getTypePlugin();
$from = $workflow_type
->getState($from_state);
$to = $workflow_type
->getState($to_state);
$transition = $from
->getTransitionTo($to_state);
if (!$this->account
->hasPermission('use ' . $workflow
->id() . ' transition ' . $transition
->id())) {
$this->context
->buildViolation($constraint->noAccessMessage, [
'%original_state' => $from
->label(),
'%new_state' => $to
->label(),
])
->atPath($field_name)
->addViolation();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintValidatorBase:: |
protected | property | The current user. | |
ConstraintValidatorBase:: |
protected | property | The content moderation information service. | |
ConstraintValidatorBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ConstraintValidatorBase:: |
protected | function | Gets the workflow type from the supplied entity's configured workflow. | |
ConstraintValidatorBase:: |
protected | function | Validate transition. | |
ConstraintValidatorBase:: |
public | function | SchedulerModerationConstraintValidator constructor. | |
TransitionAccessConstraintValidator:: |
public | function | Checks if the passed value is valid. |