class PriceModifierConstraintValidator in Price 3.x
Same name and namespace in other branches
- 8 src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator
- 3.0.x src/Plugin/Validation/Constraint/PriceModifierConstraintValidator.php \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator
Validates the price modifier constraint.
Hierarchy
- class \Drupal\price\Plugin\Validation\Constraint\PriceModifierConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of PriceModifierConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ PriceModifierConstraintValidator.php, line 16
Namespace
Drupal\price\Plugin\Validation\ConstraintView source
class PriceModifierConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new PriceModifierConstraintValidator object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
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();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PriceModifierConstraintValidator:: |
protected | property | The entity type manager. | |
PriceModifierConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
PriceModifierConstraintValidator:: |
public | function | ||
PriceModifierConstraintValidator:: |
public | function | Constructs a new PriceModifierConstraintValidator object. |