You are here

function social_profile_user_insert in Open Social 10.1.x

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

Implements hook_ENTITY_TYPE_insert().

In order to save a new default profile on user creation.

File

modules/social_features/social_profile/social_profile.module, line 503
The Social profile module.

Code

function social_profile_user_insert(UserInterface $account) {

  // If the new account has a UID, we can create a default profile.
  // Default image is added through the field settings.
  if (!empty($account
    ->id())) {

    /** @var \Drupal\profile\Entity\ProfileType $profile_type */
    $profile_type = ProfileType::load('profile');

    // Sometimes profile fields are already requested during registration.
    // In those cases, the profile will already be created from that.
    if ($profile_type !== NULL && $profile_type
      ->getRegistration() === FALSE) {
      $default_values = [
        'type' => $profile_type
          ->id(),
        'uid' => $account
          ->id(),
      ];

      // Get all field instances for the profile entity and check if the address
      // field exists.
      $instances = \Drupal::service('entity_field.manager')
        ->getFieldDefinitions('profile', $profile_type
        ->id());
      if (array_key_exists('field_profile_address', $instances)) {

        // Set the users default country to the site default country.
        $default_values['field_profile_address'][0]['country_code'] = \Drupal::config('system.date')
          ->get('country.default');
      }

      // Create a profile.
      $profile = Profile::create($default_values);
      $profile
        ->save();
    }
  }
}