You are here

class ReservationConflictConstraintValidator in MERCI (Manage Equipment Reservations, Checkout and Inventory) 8.2

Checks for conflicts when validating a entity with reservable items.

Hierarchy

Expanded class hierarchy of ReservationConflictConstraintValidator

File

src/Plugin/Validation/Constraint/ReservationConflictConstraintValidator.php, line 17
Contains \Drupal\merci\Plugin\Validation\Constraint\ReservationConflictConstraintValidator.

Namespace

Drupal\merci\Plugin\Validation\Constraint
View source
class ReservationConflictConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {

    /* @var \Drupal\Core\Field\FieldItemInterface $value */
    if (!isset($value)) {
      return;
    }
    $id = $value->target_id;

    // '0' or NULL are considered valid empty references.
    if (empty($id)) {
      return;
    }

    /* @var \Drupal\Core\Entity\FieldableEntityInterface $referenced_entity */
    $referenced_entity = $value->entity;

    //$conflicts = \Drupal::service('merci.reservation_conflicts');
    $conflicts = new ReservationConflicts();
    $conflicts
      ->setEntity($value
      ->getEntity());
    $conflicts
      ->setDateField($constraint->date_field);
    $conflicts
      ->setQuantityField($constraint->quantity_field);
    $conflicts
      ->setItemField($value
      ->getName());
    $conflicts
      ->setParentQuantityField($constraint->parent_quantity_field);
    foreach ($conflicts
      ->getErrors() as $delta => $errors) {
      $msg = array();
      if (array_key_exists(MERCI_ERROR_TOO_MANY, $errors)) {
        $msg[] = $errors[MERCI_ERROR_TOO_MANY];
      }
      elseif (array_key_exists(MERCI_ERROR_CONFLICT, $errors)) {
        foreach ($errors[MERCI_ERROR_CONFLICT] as $date_start => $message) {
          $msg[] = $message;
        }
      }
      $this->context
        ->addViolation(implode('<br>', $msg));
    }
  }

}

Members