You are here

public function CourseEnrollment::preSave in Course 8.2

Same name and namespace in other branches
  1. 8.3 src/Entity/CourseEnrollment.php \Drupal\course\Entity\CourseEnrollment::preSave()
  2. 3.x src/Entity/CourseEnrollment.php \Drupal\course\Entity\CourseEnrollment::preSave()

If a duration is set on the course, apply it to this enrollment.

Overrides ContentEntityBase::preSave

File

src/Entity/CourseEnrollment.php, line 90

Class

CourseEnrollment
Defines the profile entity class.

Namespace

Drupal\course\Entity

Code

public function preSave(EntityStorageInterface $storage) {
  $course = $this
    ->getCourse();
  if ($this
    ->get('enroll_end')
    ->isEmpty() && !$course
    ->get('duration')
    ->isEmpty()) {

    // Set enrollment end to now + the duration of the course.
    $this
      ->set('enroll_end', REQUEST_TIME + $course
      ->get('duration')
      ->getString());
  }
  $this
    ->evaluate();
  parent::preSave($storage);
}