You are here

public function SocialGroupHelperService::getGroupFromEntity in Open Social 8.9

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

Returns a group id from a entity (post, node).

Parameters

array $entity: The entity in the form of an entity reference array to get the group for.

bool $read_cache: Whether the per request cache should be used. This should only be disabled if you know that the group for the entity has changed because disabling this can have serious performance implications. Setting this to FALSE will update the cache for subsequent calls.

Return value

\Drupal\group\Entity\GroupInterface|null The group that this entity belongs to or NULL if the entity doesn't belong to any group.

File

modules/social_features/social_group/src/SocialGroupHelperService.php, line 61

Class

SocialGroupHelperService
Class SocialGroupHelperService.

Namespace

Drupal\social_group

Code

public function getGroupFromEntity(array $entity, $read_cache = TRUE) {
  $gid = NULL;

  // Comments can have groups based on what the comment is posted on so the
  // cache type differs from what we later use to fetch the group.
  $cache_type = $entity['target_type'];
  $cache_id = $entity['target_id'];
  if ($read_cache && is_array($this->cache[$cache_type]) && isset($this->cache[$cache_type][$cache_id])) {
    return $this->cache[$cache_type][$cache_id];
  }

  // Special cases for comments.
  // Returns the entity to which the comment is attached.
  if ($entity['target_type'] === 'comment') {
    $comment = \Drupal::entityTypeManager()
      ->getStorage('comment')
      ->load($entity['target_id']);
    $commented_entity = $comment
      ->getCommentedEntity();
    $entity['target_type'] = $commented_entity
      ->getEntityTypeId();
    $entity['target_id'] = $commented_entity
      ->id();
  }
  if ($entity['target_type'] === 'post') {

    /* @var /Drupal/social_post/Entity/Post $post */
    $post = Post::load($entity['target_id']);
    $recipient_group = $post
      ->get('field_recipient_group')
      ->getValue();
    if (!empty($recipient_group)) {
      $gid = $recipient_group['0']['target_id'];
    }
  }
  elseif ($entity['target_type'] === 'node') {

    // Try to load the entity.
    if ($node = Node::load($entity['target_id'])) {

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

        // Potentially there are more than one.
        $groupcontent = reset($groupcontent);

        // Set the group id.
        $gid = $groupcontent
          ->getGroup()
          ->id();
      }
    }
  }

  // Cache the group id for this entity to optimise future calls.
  $this->cache[$cache_type][$cache_id] = $gid;
  return $gid;
}