You are here

public function SetGroupsForNodeService::setGroupsForNode in Open Social 8.9

Same name and namespace in other branches
  1. 8 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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

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

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)) {
    $groups = Group::loadMultiple($groups_to_remove);
    foreach ($groups as $group) {
      self::removeGroupContent($node, $group);
    }
  }

  // Add the content to the Group if we placed it in a group.
  if (!empty($groups_to_add)) {
    $groups = Group::loadMultiple($groups_to_add);
    foreach ($groups as $group) {
      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;
}