function social_profile_fields_social_user_name_display_suggestions in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 8.5 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 8.6 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 8.7 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 8.8 modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 10.0.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 10.1.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
- 10.2.x modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module \social_profile_fields_social_user_name_display_suggestions()
Implements hook_social_user_name_display_suggestions().
Adds the nickname as a possible display name.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
File
- modules/
social_features/ social_profile/ modules/ social_profile_fields/ social_profile_fields.module, line 151 - The social profile fields module file.
Code
function social_profile_fields_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', TRUE))) {
return $suggestions;
}
if (_social_profile_fields_get_setting('profile_profile_field_profile_nick_name') && !$user_profile
->get('field_profile_nick_name')
->isEmpty()) {
/** @var \Drupal\profile\ProfileAccessControlHandler $accessControlHandler */
$accessControlHandler = $entityTypeManager
->getAccessControlHandler('profile');
$nick_name_field = $user_profile
->get('field_profile_nick_name');
$nick_name = $accessControlHandler
->fieldAccess('view', $nick_name_field
->getFieldDefinition(), NULL, $nick_name_field, FALSE) ? $nick_name_field
->getString() : "";
if ($nick_name !== '') {
// Add the nickname with a low weight so it's shown before the full name.
$suggestions['nickname'] = [
'weight' => -100,
'name' => $nick_name,
];
}
}
return $suggestions;
}