public function PriceModifierConstraintValidator::validate in Price 8
Same name and namespace in other branches
- 3.x src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator::validate()
- 3.0.x src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator::validate()
File
- src/
Plugin/ Validation/ Constraint/ PriceModifierConstraintValidator.php, line 45
Class
- PriceModifierConstraintValidator
- Validates the price modifier constraint.
Namespace
Drupal\price\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) {
// if (!($value instanceof PriceItem)) {
// throw new UnexpectedTypeException($value, PriceItem::class);
// }
$price_modified_item = $value;
$modifier = $price_modified_item
->get('modifier')
->getValue();
if ($modifier === NULL || $modifier === '') {
return;
}
$modifiers = $this->entityTypeManager
->getStorage('price_modifier')
->loadMultiple();
if (!isset($modifiers[$modifier])) {
$this->context
->buildViolation($constraint->invalidMessage)
->atPath('modifier')
->setParameter('%value', $this
->formatValue($modifier))
->addViolation();
return;
}
$available_modifiers = $constraint->availableModifiers;
if (!empty($available_modifiers) && !in_array($modifier, $available_modifiers)) {
$this->context
->buildViolation($constraint->notAvailableMessage)
->atPath('modifier')
->setParameter('%value', $this
->formatValue($modifier))
->addViolation();
}
}