You are here

function social_sso_social_auth_extra_profile_presave in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  2. 8 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  3. 8.2 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  4. 8.3 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  5. 8.5 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  6. 8.6 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  7. 8.7 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()
  8. 8.8 modules/social_features/social_sso/social_sso.module \social_sso_social_auth_extra_profile_presave()

Implements hook_social_auth_extra_profile_presave().

File

modules/social_features/social_sso/social_sso.module, line 16
Module file for Social SSO.

Code

function social_sso_social_auth_extra_profile_presave(UserInterface $account, ProfileInterface $profile, AuthManagerInterface $auth_manager, UserManagerInterface $user_manager) {

  // By default we always use First and Last name fields.
  $use_first_name = TRUE;
  $use_last_name = TRUE;
  if (\Drupal::moduleHandler()
    ->moduleExists('social_profile_fields')) {

    // If nickname is used populate it, instead of First and Last name.
    if (_social_profile_fields_get_setting('profile_profile_field_profile_nick_name')) {
      $nick_name = $auth_manager
        ->getFirstName() . ' ' . $auth_manager
        ->getLastName();
      $profile
        ->get('field_profile_nick_name')
        ->setValue($nick_name);
    }

    // Check if First and Last names were not disabled.
    $use_first_name = _social_profile_fields_get_setting('profile_profile_field_profile_first_name');
    $use_last_name = _social_profile_fields_get_setting('profile_profile_field_profile_last_name');
  }
  if ($use_first_name && $use_last_name) {
    $profile
      ->get('field_profile_first_name')
      ->setValue($auth_manager
      ->getFirstName());
    $profile
      ->get('field_profile_last_name')
      ->setValue($auth_manager
      ->getLastName());
  }
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $field_definitions = $entity_field_manager
    ->getFieldDefinitions('profile', 'profile');
  if (isset($field_definitions['field_profile_image'])) {
    $auth_manager
      ->setFieldPicture($field_definitions['field_profile_image']);
    $user_manager
      ->setFieldPicture($field_definitions['field_profile_image']);
    if ($url = $auth_manager
      ->getProfilePicture()) {
      $user_manager
        ->setProfilePicture($url, $user_manager
        ->getAccountId());
    }
  }
}