SocialGroupHelperService.php in Open Social 8.3
Same filename and directory in other branches
- 8.9 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.2 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.4 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.5 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.6 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.7 modules/social_features/social_group/src/SocialGroupHelperService.php
- 8.8 modules/social_features/social_group/src/SocialGroupHelperService.php
- 10.3.x modules/social_features/social_group/src/SocialGroupHelperService.php
- 10.0.x modules/social_features/social_group/src/SocialGroupHelperService.php
- 10.1.x modules/social_features/social_group/src/SocialGroupHelperService.php
- 10.2.x modules/social_features/social_group/src/SocialGroupHelperService.php
Namespace
Drupal\social_groupFile
modules/social_features/social_group/src/SocialGroupHelperService.phpView source
<?php
namespace Drupal\social_group;
use Drupal\group\Entity\GroupContent;
use Drupal\node\Entity\Node;
use Drupal\social_post\Entity\Post;
/**
* Class SocialGroupHelperService.
*
* @package Drupal\social_group
*/
class SocialGroupHelperService {
/**
* Constructor.
*/
public function __construct() {
}
/**
* Returns a group id from a entity (post, node).
*/
public static function getGroupFromEntity($entity) {
$gid = NULL;
// 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();
}
}
}
return $gid;
}
}
Classes
Name | Description |
---|---|
SocialGroupHelperService | Class SocialGroupHelperService. |