You are here

social_sso.module in Open Social 8.5

Module file for Social SSO.

File

modules/social_features/social_sso/social_sso.module
View source
<?php

/**
 * @file
 * Module file for Social SSO.
 */
use Drupal\user\UserInterface;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\social_auth_extra\AuthManagerInterface;
use Drupal\social_auth_extra\UserManagerInterface;

/**
 * Implements hook_social_auth_extra_profile_presave().
 */
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());
    }
  }
}