You are here

function social_event_flagging_insert in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  2. 8 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  3. 8.2 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  4. 8.3 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  5. 8.5 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  6. 8.6 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  7. 8.7 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  8. 8.8 modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  9. 10.3.x modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  10. 10.0.x modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  11. 10.1.x modules/social_features/social_event/social_event.module \social_event_flagging_insert()
  12. 10.2.x modules/social_features/social_event/social_event.module \social_event_flagging_insert()

Implements hook_ENTITY_TYPE_insert().

File

modules/social_features/social_event/social_event.module, line 208
The Social event module.

Code

function social_event_flagging_insert(EntityInterface $entity) {
  $group_type_ids = \Drupal::config('social_event.settings')
    ->get('enroll');
  if (empty($group_type_ids)) {
    return;
  }
  $current_user = \Drupal::currentUser();
  $owner = $entity
    ->getOwnerId() == $current_user
    ->id();
  $is_node = $entity->entity_type->value == 'node';
  $following = $entity
    ->getFlagId() == 'follow_content';
  if (!($owner && $is_node && $following)) {
    return;
  }
  $nid = $entity->entity_id->value;
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($nid);

  /** @var \Drupal\group\Entity\GroupContentInterface $groupcontent */
  foreach (GroupContent::loadByEntity($node) as $groupcontent) {

    /** @var \Drupal\group\Entity\GroupInterface $group */
    $group = $groupcontent
      ->getGroup();
    $allowed_type = in_array($group
      ->bundle(), $group_type_ids);
    $is_member = $group
      ->getMember($current_user) instanceof GroupMembershipLoaderInterface;
    if ($allowed_type && !$is_member) {
      $account = \Drupal::entityTypeManager()
        ->getStorage('user')
        ->load($current_user
        ->id());
      $group
        ->addMember($account);
    }
  }
}