You are here

function social_profile_social_user_name_display_suggestions in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  2. 8.5 modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  3. 8.6 modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  4. 8.7 modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  5. 8.8 modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  6. 10.3.x modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  7. 10.1.x modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()
  8. 10.2.x modules/social_features/social_profile/social_profile.module \social_profile_social_user_name_display_suggestions()

Implements hook_social_user_name_display_suggestions().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

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

Code

function social_profile_social_user_name_display_suggestions(AccountInterface $account) : array {
  $suggestions = [];
  $entityTypeManager = \Drupal::entityTypeManager();

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = $entityTypeManager
    ->getStorage('profile');
  if ($user_profile = $storage
    ->loadByUser($account, 'profile')) {

    /** @var \Drupal\profile\ProfileAccessControlHandler $accessControlHandler */
    $accessControlHandler = $entityTypeManager
      ->getAccessControlHandler('profile');
    $first_name_field = $user_profile
      ->get('field_profile_first_name');
    $last_name_field = $user_profile
      ->get('field_profile_last_name');
    $first_name = $accessControlHandler
      ->fieldAccess('view', $first_name_field
      ->getFieldDefinition(), NULL, $first_name_field, FALSE) ? $first_name_field
      ->getString() : "";
    $last_name = $accessControlHandler
      ->fieldAccess('view', $last_name_field
      ->getFieldDefinition(), NULL, $last_name_field, FALSE) ? $last_name_field
      ->getString() : "";
    $account_name = trim($first_name . " " . $last_name);
    if ($account_name !== '') {

      // Add the full name with a default weight.
      $suggestions['full_name'] = [
        'name' => $account_name,
      ];
    }
  }
  return $suggestions;
}