You are here

function opigno_ilt_opigno_ilt_presave in Opigno Instructor-led Trainings 3.x

Same name and namespace in other branches
  1. 8 opigno_ilt.module \opigno_ilt_opigno_ilt_presave()

Implements hook_ENTITY_TYPE_presave().

Updates a calendar event related to a instructor-led training.

File

./opigno_ilt.module, line 265
Contains opigno_ilt.module.

Code

function opigno_ilt_opigno_ilt_presave(EntityInterface $entity) {

  /** @var \Drupal\opigno_ilt\ILTInterface $entity */

  /** @var \Drupal\opigno_calendar_event\Entity\CalendarEvent $calendar_event */
  $calendar_event = $entity
    ->getCalendarEvent();
  if (!isset($calendar_event)) {
    $calendar_event = CalendarEvent::create([
      'type' => 'ilt_calendar_event',
    ]);
  }
  $calendar_event
    ->set('title', $entity
    ->getTitle());
  $calendar_event
    ->set('uid', $entity
    ->getOwnerId());

  // Set date.
  $start_date = $entity
    ->getStartDate();
  $end_date = $entity
    ->getEndDate();
  $date = [
    'value' => _opigno_ilt_datetime_to_iso($start_date),
    'end_value' => _opigno_ilt_datetime_to_iso($end_date),
  ];
  $calendar_event
    ->set('date_daterange', $date);

  // Set members.
  $members = $entity
    ->getMembersIds();
  if (empty($members)) {
    $training = $entity
      ->getTraining();
    if (isset($training)) {
      $members = array_map(function ($member) {

        /** @var \Drupal\group\GroupMembership $member */
        return $member
          ->getUser()
          ->id();
      }, $training
        ->getMembers());
    }
  }
  $calendar_event
    ->set('field_calendar_event_members', $members);
  $calendar_event
    ->save();
  $entity
    ->setCalendarEvent($calendar_event);
}