You are here

class MerciAllowOvernightConstraintValidator 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 MerciAllowOvernightConstraintValidator

File

src/Plugin/Validation/Constraint/MerciAllowOvernightConstraintValidator.php, line 18
Contains \Drupal\merci\Plugin\Validation\Constraint\MerciAllowOvernightConstraintValidator.

Namespace

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

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

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

    // '0' or NULL are considered valid empty references.
    if (empty($id)) {
      return;
    }
    if ($constraint->grouping_field) {
      $entity = $value->entity->{$constraint->grouping_field}
        ->first()->entity;
    }
    else {
      $entity = $value->entity;
    }
    if ($entity->{$constraint->overnight_field}->value != 1) {
      $datetime_start = $value
        ->getEntity()->{$constraint->date_field}
        ->first()->start_date;
      $datetime_end = $value
        ->getEntity()->{$constraint->date_field}
        ->first()->end_date;
      $datetime_start
        ->setTimeZone(timezone_open(drupal_get_user_timezone()));
      $datetime_end
        ->setTimeZone(timezone_open(drupal_get_user_timezone()));
      $date_format = DateFormat::load('html_date')
        ->getPattern();
      if ($datetime_start
        ->format($date_format) != $datetime_end
        ->format($date_format)) {
        $this->context
          ->addViolation($constraint->message);
      }
    }
  }

}

Members