You are here

function opigno_moxtra_opigno_moxtra_meeting_presave in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x opigno_moxtra.module \opigno_moxtra_opigno_moxtra_meeting_presave()

Implements hook_ENTITY_TYPE_presave().

Updates the calendar event related to the live meeting.

File

./opigno_moxtra.module, line 363
Contains opigno_moxtra.module.

Code

function opigno_moxtra_opigno_moxtra_meeting_presave(EntityInterface $entity) {
  if (!_opigno_moxtra_is_active()) {
    return;
  }

  /** @var \Drupal\opigno_moxtra\Entity\Meeting $entity */

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

  // Set date.
  $start_value = $entity
    ->getStartDate();
  $end_value = $entity
    ->getEndDate();
  $start_date = isset($start_value) ? DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $start_value)
    ->setTimezone(new \DateTimeZone('UTC'))
    ->format('Y-m-d\\TH:i:s') : NULL;
  $end_date = isset($end_value) ? DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $end_value)
    ->setTimezone(new \DateTimeZone('UTC'))
    ->format('Y-m-d\\TH:i:s') : NULL;
  $date = [
    'value' => $start_date,
    'end_value' => $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);
}