class SetGroupsForNodeService in Open Social 8.5
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.2 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.3 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.4 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.6 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.7 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 8.8 modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 10.3.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 10.0.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 10.1.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
- 10.2.x modules/social_features/social_group/src/SetGroupsForNodeService.php \Drupal\social_group\SetGroupsForNodeService
Class SetGroupsForNodeService.
@package Drupal\social_group
Hierarchy
- class \Drupal\social_group\SetGroupsForNodeService
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 16
Namespace
Drupal\social_groupView source
class SetGroupsForNodeService {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
}
/**
* Save groups for a given node.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
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;
}
/**
* 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SetGroupsForNodeService:: |
protected | property | The entity type manager. | |
SetGroupsForNodeService:: |
protected | property | The Module handler. | |
SetGroupsForNodeService:: |
public static | function | Creates a group content. | |
SetGroupsForNodeService:: |
public static | function | Deletes a group content. | |
SetGroupsForNodeService:: |
public | function | Save groups for a given node. | |
SetGroupsForNodeService:: |
public | function | Constructor. |