You are here

public function SetGroupsForNodeService::setGroupsForNode in Open Social 8.4

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 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  3. 8.2 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService::setGroupsForNode()
  4. 8.3 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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

File

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

Class

SetGroupsForNodeService
Class SetGroupsForNodeService.

Namespace

Drupal\social_group

Code

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

  // If we don't have to add or remove groups, we don't need to move anything.
  // the node is just updated for other values.
  if (empty($groups_to_add) && empty($groups_to_remove)) {
    return $node;
  }

  // Remove the notifications related to the node if a group is added or
  // moved.
  if (empty($original_groups) || $original_groups != $groups_to_add) {
    $entity_query = $this->entityTypeManager
      ->getStorage('activity')
      ->getQuery();
    $entity_query
      ->condition('field_activity_entity.target_id', $node
      ->id(), '=');
    $entity_query
      ->condition('field_activity_entity.target_type', 'node', '=');

    // 1. From Group -> Community OR Group.
    // If there are original groups, it means content is removed from
    // inside a group. So we can remove the create_node-bundle_group
    // message from the streams.
    if (!empty($original_groups)) {
      $template = 'create_' . $node
        ->bundle() . '_group';
      $messages = $this->entityTypeManager
        ->getStorage('message')
        ->loadByProperties([
        'template' => $template,
      ]);

      // Make sure we have a message template to work with.
      if ($messages) {
        $entity_query
          ->condition('field_activity_message.target_id', array_keys($messages), 'IN');
      }
      $moved = TRUE;
    }
    elseif (empty($original_groups) && !empty($groups_to_add)) {
      $template = 'create_' . $node
        ->bundle() . '_community';
      $messages = $this->entityTypeManager
        ->getStorage('message')
        ->loadByProperties([
        'template' => $template,
      ]);

      // Make sure we have a message template to work with.
      if ($messages) {
        $entity_query
          ->condition('field_activity_message.target_id', array_keys($messages), 'IN');
      }
      $moved = TRUE;
    }

    // Delete all activity items connected to our query.
    if (!empty($ids = $entity_query
      ->execute())) {
      $controller = $this->entityTypeManager
        ->getStorage('activity');
      $controller
        ->delete($controller
        ->loadMultiple($ids));
    }

    // Make sure to delete all activity items connected to the moved content
    // template.
    if ($moved) {
      $messages = $this->entityTypeManager
        ->getStorage('message')
        ->loadByProperties([
        'template' => 'moved_content_between_groups',
      ]);

      // Make sure we have a message template to work with.
      if ($messages) {
        $entity_query
          ->condition('field_activity_message.target_id', array_keys($messages), 'IN');
      }

      // Delete all activity items connected to our query.
      if (!empty($ids = $entity_query
        ->execute())) {
        $controller = $this->entityTypeManager
          ->getStorage('activity');
        $controller
          ->delete($controller
          ->loadMultiple($ids));
      }
    }
  }

  // Remove all the group content references from the Group as well if we
  // moved it out of the group.
  if (!empty($groups_to_remove)) {
    foreach ($groups_to_remove as $group_id) {
      $group = Group::load($group_id);
      self::removeGroupContent($node, $group);
    }
  }

  // Add the content to the Group if we placed it in a group.
  if (!empty($groups_to_add)) {
    foreach ($groups_to_add as $group_id) {
      $group = Group::load($group_id);
      self::addGroupContent($node, $group);
    }
  }

  // Invoke hook_social_group_move if the content is not new.
  if ($moved && !$is_new) {
    $hook = 'social_group_move';
    foreach ($this->moduleHandler
      ->getImplementations($hook) as $module) {
      $function = $module . '_' . $hook;
      $function($node);
    }
  }
  return $node;
}