You are here

function opigno_ilt_group_content_insert in Opigno Instructor-led Trainings 8

Same name and namespace in other branches
  1. 3.x opigno_ilt.module \opigno_ilt_group_content_insert()

Implements hook_ENTITY_TYPE_insert().

File

./opigno_ilt.module, line 205
Contains opigno_ilt.module.

Code

function opigno_ilt_group_content_insert(EntityInterface $entity) {

  /** @var \Drupal\group\Entity\GroupContentInterface $entity */
  $bundle = $entity
    ->bundle();
  $type = $entity
    ->getEntity()
    ->getEntityTypeId();
  if ($bundle === 'learning_path-group_membership') {

    // We need to update all ILTs in the group
    // to show it in the calendar for a new member.
    $group = $entity
      ->getGroup();
    $ilts = \Drupal::entityTypeManager()
      ->getStorage('opigno_ilt')
      ->loadByProperties([
      'training' => $group
        ->id(),
    ]);
    foreach ($ilts as $ilt) {
      $ilt
        ->save();
    }
  }
  elseif ($type === 'opigno_ilt') {

    // Updates the related training field in an instructor-led training.

    /** @var \Drupal\opigno_ilt\ILTInterface $ilt */
    $ilt = $entity
      ->getEntity();
    $training = $entity
      ->getGroup();
    $ilt
      ->setTraining($training);
    $ilt
      ->save();
  }
}