You are here

class SetGroupsForNodeService 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
  2. 8.2 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  3. 8.3 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  4. 8.4 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  5. 8.5 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  6. 8.6 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  7. 8.7 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  8. 8.8 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  9. 10.3.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  10. 10.0.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  11. 10.1.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
  12. 10.2.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService

Class SetGroupsForNodeService.

@package Drupal\social_group

Hierarchy

Expanded class hierarchy of SetGroupsForNodeService

1 string reference to 'SetGroupsForNodeService'
social_group.services.yml in modules/social_features/social_group/social_group.services.yml
modules/social_features/social_group/social_group.services.yml
1 service uses SetGroupsForNodeService
social_group.set_groups_for_node_service in modules/social_features/social_group/social_group.services.yml
Drupal\social_group\SetGroupsForNodeService

File

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

Namespace

Drupal\social_group
View source
class SetGroupsForNodeService {

  /**
   * Constructor.
   */
  public function __construct() {
  }

  /**
   * Save groups for a given node.
   */
  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;
  }

  /**
   * Creates a group content.
   *
   * @param \Drupal\node\NodeInterface $node
   *   Object of a node.
   * @param \Drupal\group\Entity\Group $group
   *   Object of a group.
   */
  public static function addGroupContent(NodeInterface $node, Group $group) {

    // TODO Check if group plugin id exists.
    $plugin_id = 'group_node:' . $node
      ->bundle();
    $group
      ->addContent($node, $plugin_id, [
      'uid' => $node
        ->getOwnerId(),
    ]);
  }

  /**
   * Deletes a group content.
   *
   * @param \Drupal\node\NodeInterface $node
   *   Object of a node.
   * @param \Drupal\group\Entity\Group $group
   *   Object of a group.
   */
  public static function removeGroupContent(NodeInterface $node, Group $group) {

    // Try to load group content from entity.
    if ($group_contents = GroupContent::loadByEntity($node)) {

      /* @var @param \Drupal\group\Entity\GroupContent $group_content */
      foreach ($group_contents as $group_content) {
        if ($group
          ->id() === $group_content
          ->getGroup()
          ->id()) {
          $group_content
            ->delete();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SetGroupsForNodeService::addGroupContent public static function Creates a group content.
SetGroupsForNodeService::removeGroupContent public static function Deletes a group content.
SetGroupsForNodeService::setGroupsForNode public static function Save groups for a given node.
SetGroupsForNodeService::__construct public function Constructor.