You are here

public static function SetGroupsForNodeService::setGroupsForNode in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  2. 8.2 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  3. 8.3 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  4. 8.4 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  5. 8.5 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  6. 8.6 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  7. 8.7 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  8. 8.8 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  9. 10.3.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  10. 10.0.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  11. 10.1.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  12. 10.2.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()

Save groups for a given node.

File

modules/social_features/social_group/src/SetGroupsForNodeService.php, line 26

Class

SetGroupsForNodeService
Class SetGroupsForNodeService.

Namespace

Drupal\social_group

Code

public static function setGroupsForNode(NodeInterface $node, array $groups_to_remove, array $groups_to_add, array $original_groups = []) {
  $moved = FALSE;

  // Remove the notifications related to the node if a group is added or
  // moved.
  if ((empty($original_groups) || $original_groups != $groups_to_add) && !empty($groups_to_add)) {
    $entity_query = \Drupal::entityQuery('activity');
    $entity_query
      ->condition('field_activity_entity.target_id', $node
      ->id(), '=');
    $entity_query
      ->condition('field_activity_entity.target_type', 'node', '=');
    if (!empty($original_groups)) {
      $template = 'create_' . $node
        ->bundle() . '_group';
      $messages = \Drupal::entityTypeManager()
        ->getStorage('message')
        ->loadByProperties([
        'template' => $template,
      ]);
      $entity_query
        ->condition('field_activity_message.target_id', array_keys($messages), 'IN');
      $moved = TRUE;
    }
    if (!empty($ids = $entity_query
      ->execute())) {
      $controller = \Drupal::entityTypeManager()
        ->getStorage('activity');
      $controller
        ->delete($controller
        ->loadMultiple($ids));
    }
  }
  foreach ($groups_to_remove as $group_id) {
    $group = Group::load($group_id);
    self::removeGroupContent($node, $group);
  }
  foreach ($groups_to_add as $group_id) {
    $group = Group::load($group_id);
    self::addGroupContent($node, $group);
  }
  if ($moved) {
    $hook = 'social_group_move';
    foreach (\Drupal::moduleHandler()
      ->getImplementations($hook) as $module) {
      $function = $module . '_' . $hook;
      $function($node);
    }
  }
  return $node;
}