You are here

function _social_group_delete_closed_group in Open Social 8

Same name and namespace in other branches
  1. 8.2 modules/social_features/social_group/social_group.module \_social_group_delete_closed_group()
  2. 8.3 modules/social_features/social_group/social_group.module \_social_group_delete_closed_group()

Delete the group and all of its content.

1 string reference to '_social_group_delete_closed_group'
social_group_form_group_closed_group_delete_form_alter in modules/social_features/social_group/social_group.module
Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_group/social_group.module, line 1051
The Social group module.

Code

function _social_group_delete_closed_group() {

  // Get the group.
  $group = _social_group_get_current_group();

  // Make sure its a group of type closed_group.
  if ($group && $group
    ->getGroupType()
    ->id() == 'closed_group') {
    $group_content_types = GroupContentType::loadByEntityTypeId('node');
    $group_content_types = array_keys($group_content_types);

    // Get all the node's related to the current group.
    $query = \Drupal::database()
      ->select('group_content_field_data', 'gcfd');
    $query
      ->addField('gcfd', 'entity_id');
    $query
      ->condition('gcfd.gid', $group
      ->id());
    $query
      ->condition('gcfd.type', $group_content_types, 'IN');
    $query
      ->execute()
      ->fetchAll();
    $entity_ids = $query
      ->execute()
      ->fetchAllAssoc('entity_id');

    // Store all the node ids.
    $nids = array_keys($entity_ids);

    // Get all the posts from this group.
    $query = \Drupal::database()
      ->select('post__field_recipient_group', 'pfrg');
    $query
      ->addField('pfrg', 'entity_id');
    $query
      ->condition('pfrg.field_recipient_group_target_id', $group
      ->id());
    $query
      ->execute()
      ->fetchAll();
    $post_ids = $query
      ->execute()
      ->fetchAllAssoc('entity_id');

    // Store all the post entity ids.
    $posts = array_keys($post_ids);

    // Pass the $nids and $posts as 2 parameters in the operations.
    // See /social_group/src/Controller/DeleteGroup.php for further process.
    $batch = [
      'title' => t('Deleting the group and all the content within the group...'),
      'init_message' => t("Preparing to delete the group and all it\\'s topic\\'s, event\\'s and post\\'s..."),
      'operations' => [
        [
          '\\Drupal\\social_group\\Controller\\DeleteGroup::deleteGroupAndContent',
          [
            $nids,
            $posts,
          ],
        ],
      ],
      'finished' => '\\Drupal\\social_group\\Controller\\DeleteGroup::deleteGroupAndContentFinishedCallback',
    ];
    batch_set($batch);
  }
}