rc-1.0.x-dev/modules/rc_group/rc_group.module
modules/rc_group/rc_group.module
<?php
/**
* @file
*/
/**
* Implements hook_help().
*/
use Drupal\group\Entity\GroupContent;
use Drupal\group\Entity\Form\GroupForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\GroupType;
use Drupal\group\Entity\Group;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
*
* @return string|void
*/
function rc_group_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the rc_group module.
case 'help.page.rc_group':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provide integration with Group module.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function rc_group_theme($existing, $type, $theme, $path): array {
$templates = [];
$templates['rc_group_block'] = [
'render element' => 'children',
'template' => 'rc-group-block',
'path' => $path . '/templates',
'variables' => rc_theme_base_variables() + [
'group' => NULL,
],
];
return $templates;
}
/**
* Implements HOOK_ENTITY_TYPE_presave().
*
* This is used to manage the creation of chat channels when new group is added
* according to the Groups Chat settings.
*
* @param \Drupal\group\Entity\Group $group
*/
function rc_group_group_presave(Group $group) {
$rcSettings = \Drupal::config('rc_group.settings');
if (!$rcSettings->get('group.auto_create_room') || !$group->hasField('field_rc_room_id')) {
return;
}
// Group is new.
$rcGroup = \Drupal::service('rc_group.group');
if ($group->isNew()) {
// Check if group type has the Room ID field.
if ($group->hasField('')) {
$rcGroup->createGroup($group);
}
}
// Group is being updated.
else {
// Get RC channel info if exists.
$rcGroupInfo = $rcGroup->groupInfo($group);
// Get Group owner.
$groupOwner = $group->getOwner();
// Check if the group entity has the room ID.
// This will be handled differently from the user as Group names are not
// unique on Drupal but are on Rocket chat, because of that, we depend on
// the Room ID field from the Group entity to determine if the group exists
// on Rocket chat.
if (!$rcGroupInfo) {
// Create group if it does not exist.
$rcGroup->createGroup($group);
}
else {
// If group name is changed, update the RC channel name.
if ($group->label() != $group->original->label()) {
$rcGroup->renameGroup($group);
}
}
// Add members to room.
$members = $group->getMembers();
foreach ($members as $member) {
$user = $member->getUser();
$rcGroup->groupInviteUser($groupOwner, $user, $group);
}
}
}
/**
* Implements HOOK_ENTITY_TYPE_insert().
*
* This is used to manage the creation of room id field when new group type is
* added according to the "Groups Chat settings".
*
* @param \Drupal\group\Entity\GroupType $groupType
*/
function rc_group_group_type_insert(GroupType $groupType) {
$type = $groupType->getEntityTypeId();
// Get the groups with chat options from configurations.
$rcSettings = \Drupal::config('rc_group.settings');
// Check the selected group types from "Chat channels per group type options".
if ($rcSettings->get('group_chat_types.types')) {
$bundle = $groupType->id();
$rcGroup = \Drupal::service('rc_group.group');
// Create Rocket Chat group.
$rcGroup
->createRcRoomIdField(
'group',
$bundle,
'field_rc_room_id',
'Room ID'
);
}
}
/**
* Implements hook_form_alter().
*/
function rc_group_form_alter(&$form, FormStateInterface $form_state) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof GroupForm) {
$rcGroupSettings = \Drupal::config('rc_group.settings');
$roomIdVisibility = $rcGroupSettings->get('group.visibility_room_id_form');
if (!$roomIdVisibility) {
unset($form['field_rc_room_id']);
}
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function rc_group_group_delete(Group $group) {
$rcGroup = \Drupal::service('rc_group.group');
$rcGroup->deleteGroup($group);
}
/**
* Implements hook_entity_insert().
*
* Add new user to Rocket chat and update roles.
*/
function rc_group_group_content_insert(GroupContent $groupContent) {
if ($groupContent->getContentPlugin()->getPluginId() == 'group_membership') {
$group = $groupContent->getGroup();
if (!$group->hasField('field_rc_room_id')) {
return;
}
$groupRoomId = $group->field_rc_room_id->value;
if (isset($groupRoomId)) {
$rcGroup = \Drupal::service('rc_group.group');
$rcGroup->groupInviteUser($group->getOwner(),
$groupContent->getEntity(),
$group);
}
}
}
/**
* Implements hook_entity_update().
*
* Add new member to Rocket chat and update roles.
*
* @param \Drupal\group\Entity\GroupContent $groupContent
*/
function rc_group_group_content_update(GroupContent $groupContent) {
// Adding/Removing Rocket Chat roles.
if ($groupContent->getContentPlugin()->getPluginId() == 'group_membership') {
$group = $groupContent->getGroup();
$groupRoomId = $group->field_rc_room_id->value;
if (isset($groupRoomId)) {
$rcGroup = \Drupal::service('rc_group.group');
$rcGroup->groupUpdateUserRoles($groupContent);
}
}
}
/**
* Implements hook_ENTITY_TYPE_predelete().
*
* Kick user from Rocket Chat group.
*/
function rc_group_group_content_predelete(GroupContent $groupContent) {
if ($groupContent->getContentPlugin()->getPluginId() == 'group_membership') {
$group = $groupContent->getGroup();
$groupRoomId = $group->field_rc_room_id->value;
if (isset($groupRoomId)) {
$rcGroup = \Drupal::service('rc_group.group');
$rcGroup->groupKickUser($groupContent->getOwner(),
$groupContent->getEntity(),
$groupContent->getGroup());
}
}
}
// TODO: add filter to the rc_group view to show only the group types with rooms.
///**
// * Implements hook_views_query_alter().
// */
//function rc_group_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// if ($view->name == 'rc_groups') {
// $config = \Drupal::config('rc_group.settings');
// $bundlesConfig = $config->get('group_chat_types.types');
//
// $query->add_where(1,'taxonomy_term_data_node.tid', $tids, 'NOT IN');
// }
//}
/**
* Implements hook_cron().
*/
function rc_group_cron() {
// @todo create chat rooms and add members when cron is run.
// Create chat account for users who do not have chat accounts.
// $rcSettings = \Drupal::config('rc_group.settings');
//
// if ($rcSettings->get('user.create_group_cron') || $rcSettings->get('user.update_group_cron')) {
// $rcGroup = \Drupal::service('rc_group.group');
// } else {
// return;
// }
//
// $gIdsToCreate = [];
// if ($rcSettings->get('user.create_group_cron')) {
//
// // Query users to get the ones that do not have Rocket Chat ID field value.
// $query = \Drupal::entityQuery('group')
// ->notExists('field_rc_room_id.0.value');
//
//
// $uIdsToCreate = $query->execute();
//
// $groupsToCreate = \Drupal::entityTypeManager()->getStorage('group')
// ->loadMultiple($gIdsToCreate);
//
// foreach ($groupsToCreate as $group) {
// $members = $group->getMembers();
// // Create Rocket Chat user.
// $rcGroup->createSaveGroup($group);
// $members = $group->getMembers();
// }
//
// }
//
// if ($rcSettings->get('user.update_group_cron')) {
// // Update all users who has rocket chat ID. This will help to change the
// // password and refresh the accounts.
//
// // Query users to get the ones that have Rocket Chat ID field value.
// $query = \Drupal::entityQuery('group')
// ->exists('field_rc_room_id.0.value');
//
// // Get the User IDs that match the condition.
// $gIdsToUpdate = $query->execute();
//
// // Exclude the users created in the previous step.
// $uIdsToUpdate = array_diff($gIdsToUpdate, $gIdsToCreate);
//
// // Load the user entities to perform the Rocket Chat accounts update.
// $groupsToUpdate = \Drupal::entityTypeManager()->getStorage('group')
// ->loadMultiple($uIdsToUpdate);
//
// foreach ($groupsToUpdate as $group) {
// // update Rocket Chat user.
// $rcGroup->updateSaveGroup($group);
// }
// }
}
