Change record status: 
Project: 
Introduced in branch: 
8.x-5.x
Introduced in version: 
8.x-5.0
Description: 

Multiple modules were trying to overwrite the user display name. The social_profile module wanted to display the user's full name. The social_profile_fields module wanted a nickname to take precedence and finally the social_profile_privacy wanted a say in when the full name would be displayed.

To resolve this we centralise the username resolution in the social_user module and provide other modules the ability to add suggestions using a weight to specify order of preference. An alter hook is implemented to give modules the chance to overwrite or unset suggestions. This can be used for example to implement access checking.

If no suggestions are left then we simply do not alter the name and default to Drupal's built-in behaviour.

New hooks

Two new hooks have been added as part of the social_user module API.

/**
 * Add suggestions for finding a suitable display name for the user.
 *
 * @param \Drupal\Core\Session\AccountInterface $account
 *   The account to add display name suggestions for.
 *
 * @return array[]
 *   An array of suggestions with machine name keys. Each suggestion must have
 *   at least a 'name'. The 'name' entry will be displayed to the user. An
 *   optional 'weight' field can be added to the suggestion to control the
 *   priority of the suggestion. 'weight' defaults to 0 if not specified.
 */
function hook_social_user_name_display_suggestions(AccountInterface $account) {} 
/**
 * Alters the list of display suggestions that determine the user display name.
 *
 * @param array $suggestions
 *   The array of suggestion objects generated by
 *   hook_social_user_name_display_suggestions() implementations.
 * @param \Drupal\Core\Session\AccountInterface $account
 *   The account to modify display name suggestions for.
 */
function hook_social_user_name_display_suggestions_alter(array &$suggestions, AccountInterface $account) {}

Usage examples

To add a nickname that is displayed instead of a user's real name when the corresponding field is filled in.

/**
 * Implements hook_social_user_name_display_suggestions().
 *
 * Adds the nickname as a possible display name.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function social_profile_fields_social_user_name_display_suggestions(AccountInterface $account) {
  $suggestions = [];

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = \Drupal::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')) {
    // Add the nickname with a low weight so it's shown before the full name.
    $suggestions['nickname'] = [
      'weight' => -100,
      'name' => $user_profile->field_profile_nick_name->value,
    ];
  }

  return $suggestions;
}

To remove a certain suggestion when a user is not allowed to view it and let the social_user module select a different name to display:

/**
 * Implements hook_social_user_name_display_suggestions_alter().
 *
 * Removes the full_name from the display suggestions if this module is
 * configured to limit the display and the current user is not allowed to see
 * it.
 */
function social_profile_privacy_social_user_name_display_suggestions_alter(array &$suggestions, AccountInterface $account) {
  $config = \Drupal::config('social_profile_privacy.settings');

  // If full name display is limited and the current user is not allowed to view
  // full names then we remove it from the suggestions.
  if ($config->get('limit_search_and_mention') && !\Drupal::currentUser()->hasPermission('social profile privacy always show full name')) {
    unset($suggestions['full_name']);
  }
}
Impacts: 
Module developers
Site templates, recipes and distribution developers