You are here

function _social_group_action_form_submit in Open Social 8.9

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

Form submit for group join / leave form.

1 string reference to '_social_group_action_form_submit'
social_group_form_alter in modules/social_features/social_group/social_group.module
Implements hook_form_alter().

File

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

Code

function _social_group_action_form_submit($form, FormStateInterface $form_state) {
  $group = _social_group_get_current_group();
  if (is_object($group)) {

    // Invalidate cache tags.
    $cache_tags = _social_group_cache_tags($group);
    foreach ($cache_tags as $cache_tag) {
      Cache::invalidateTags([
        $cache_tag,
      ]);
    }

    // Get form that was submitted.
    $complete_form = $form_state
      ->getCompleteForm();
    if (in_array($complete_form['#form_id'], [
      'group_content_' . $group
        ->bundle() . '-group_membership_group-join_form',
      'group_content_' . $group
        ->bundle() . '-group_membership_add_form',
    ])) {

      // Set redirect to group home page for people joining the group.
      $form_state
        ->setRedirect('entity.group.canonical', [
        'group' => $group
          ->id(),
        [],
      ]);

      // For adding people we redirect to the manage members page.
      if ($complete_form['#form_id'] == 'group_content_' . $group
        ->bundle() . '-group_membership_add_form') {

        // We passed the validation.
        // Let's create the Group Content for all our members just like
        // Group does it for creating a Group and adding its creator as member.
        $values = [
          'group_roles' => $form_state
            ->getValue('group_roles'),
        ];
        $count = 0;
        if (!empty($form_state
          ->getValue('entity_id_new'))) {

          // For multiple Group Members that are valid we add multiple entities.
          foreach ($form_state
            ->getValue('entity_id_new') as $key => $uid) {
            $group
              ->addMember(User::load($uid['target_id']), $values);
            $count++;
          }
        }

        // Add nice messages.
        if (!empty($count)) {
          $message = \Drupal::translation()
            ->formatPlural($count, '@count new member joined the group.', '@count new members joined the group.');
          \Drupal::messenger()
            ->addMessage($message, 'status');
        }
        $form_state
          ->setRedirect('view.group_manage_members.page_group_manage_members', [
          'group' => $group
            ->id(),
          [],
        ]);
      }
    }
    else {

      // Set redirect to the Group overview page
      // when a user saves their profile.
      $route = _social_group_get_overview_route($group);
      $form_state
        ->setRedirect($route['name'], $route['parameters']);
    }
  }
}