You are here

function social_auth_extra_form_user_register_form_submit in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  2. 8 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  3. 8.2 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  4. 8.3 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  5. 8.4 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  6. 8.5 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  7. 8.6 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()
  8. 8.7 modules/custom/social_auth_extra/social_auth_extra.module \social_auth_extra_form_user_register_form_submit()

Custom submit function for user_register_form.

This function is using for registration via social network.

1 string reference to 'social_auth_extra_form_user_register_form_submit'
social_auth_extra_form_user_register_form_alter in modules/custom/social_auth_extra/social_auth_extra.module
Implements hook_form_FORM_ID_alter().

File

modules/custom/social_auth_extra/social_auth_extra.module, line 148
Contains social_auth_extra.module.

Code

function social_auth_extra_form_user_register_form_submit($form, FormStateInterface $form_state) {

  /* @var $instance NetworkBase */
  if (($instance = $form_state
    ->get('network_instance')) && $instance instanceof NetworkBase) {
    $id = $instance
      ->getPluginId();
    $user_manager = \Drupal::service($id . '.user_manager');
    $account = $user_manager
      ->createAccount([
      'name' => $form_state
        ->getValue('name'),
      'mail' => $form_state
        ->getValue('mail'),
      'init' => $form_state
        ->getValue('mail'),
      'status' => $form_state
        ->getValue('status'),
      'preferred_langcode' => $form_state
        ->getValue('preferred_langcode'),
    ]);
    try {
      $network_manager = \Drupal::service('plugin.network.manager');
      if (($sdk = $network_manager
        ->createInstance($id)
        ->getSdk()) && ($access_token = $instance
        ->getDataHandler()
        ->get('access_token'))) {
        $auth_manager = \Drupal::service($id . '.auth_manager');
        $auth_manager
          ->setSdk($sdk);
        $auth_manager
          ->setAccessToken($access_token);
        if ($auth_manager
          ->getProfile()) {

          /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
          $module_handler = \Drupal::service('module_handler');

          // Set id of social profile to account and save.
          $account_id = $auth_manager
            ->getAccountId();
          $user_manager
            ->setAccountId($account_id);
          $module_handler
            ->invokeAll('social_auth_extra_user_presave', [
            $account,
            $auth_manager,
            $user_manager,
          ]);
          $account
            ->save();
          $form_state
            ->getFormObject()
            ->setEntity($account);
          if ($module_handler
            ->moduleExists('profile')) {

            // Create profile if it doesn't exists.

            /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
            $profile_storage = \Drupal::entityTypeManager()
              ->getStorage('profile');
            if (!($profile = $profile_storage
              ->loadByUser($account, 'profile', TRUE))) {
              $profile = $user_manager
                ->createProfile();
            }
            else {
              $user_manager
                ->setProfile($profile);
            }
            $module_handler
              ->invokeAll('social_auth_extra_profile_presave', [
              $account,
              $profile,
              $auth_manager,
              $user_manager,
            ]);
            $profile
              ->save();

            // Redirect to profile.
            $form_state
              ->setRedirect('profile.user_page.single', [
              'user' => $account
                ->id(),
              'profile_type' => 'profile',
              [],
            ]);
          }
          else {

            // If the profile module does not exist redirect to account form.
            $form_state
              ->setRedirect('entity.user.edit_form', [
              'user' => $account
                ->id(),
            ]);
          }
        }
      }
      if (!$account
        ->id()) {
        $module_handler
          ->invokeAll('social_auth_extra_user_presave', [
          $account,
          $auth_manager,
          $user_manager,
        ]);
        $account
          ->save();
        $form_state
          ->getFormObject()
          ->setEntity($account);
      }
      if ($account
        ->get('status')->value === 0) {
        _user_mail_notify('register_pending_approval', $account);
        drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your email address.'));
        $form_state
          ->setRedirect('<front>');
      }
      else {
        user_login_finalize($account);
        drupal_set_message(t('Your account is created. We have sent an email with your account details. You can now start exploring @community_name.', [
          '@community_name' => \Drupal::config('system.site')
            ->get('name'),
        ]));

        // Send email notification.
        $params['account'] = $account;
        $langcode = $account
          ->getPreferredLangcode();
        $site_mail = \Drupal::config('system.site')
          ->get('mail_notification');
        if (empty($site_mail)) {
          $site_mail = \Drupal::config('system.site')
            ->get('mail');
        }
        if (empty($site_mail)) {
          $site_mail = ini_get('sendmail_from');
        }
        \Drupal::service('plugin.manager.mail')
          ->mail('social_auth_extra', 'email_social_login', $account
          ->getEmail(), $langcode, $params, $site_mail);
      }

      // Save user consent.
      if (\Drupal::hasService('data_policy.manager')) {

        /** @var \Drupal\data_policy\DataPolicyManagerInterface $data_policy_manager */
        $data_policy_manager = \Drupal::service('data_policy.manager');
        if ($data_policy_manager
          ->isDataPolicy() && !empty($form_state
          ->getValue('data_policy'))) {
          $data_policy_manager
            ->saveConsent($account
            ->id(), TRUE);
        }
      }
    } catch (EntityStorageException $e) {
      drupal_set_message(t('Creation of user account failed. Please contact site administrator.'), 'error');
      $logger_factory = \Drupal::service('logger.fatory');
      $logger_factory
        ->get($id)
        ->error('Could not create new user. Exception: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
    }
  }
}