You are here

public function ConstactStorageMaximumSubmissionsConstraintValidator::validate in Contact Storage 8

File

src/Plugin/Validation/Constraint/ConstactStorageMaximumSubmissionsConstraintValidator.php, line 16

Class

ConstactStorageMaximumSubmissionsConstraintValidator
Validates the maximum submission limit constraint.

Namespace

Drupal\contact_storage\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) {

  // Check if the current user has reached the form's maximum submission limit.
  $contact_form = $entity
    ->getParent()
    ->get('contact_form')
    ->referencedEntities()[0];
  $maximum_submissions_user = $contact_form
    ->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0);
  if ($maximum_submissions_user !== 0 && contact_storage_maximum_submissions_user($contact_form) >= $maximum_submissions_user) {

    // Limit reached; can't submit the form.
    $this->context
      ->addViolation($constraint->limitReached, [
      '@limit' => $maximum_submissions_user,
    ]);
  }
}