You are here

function social_profile_fields_censor_full_name_suggestion in Open Social 8.8

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

Implements hook_social_user_name_display_suggestions_alter().

Ensures that the full name suggestion doesn't contain disabled fields.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to social_profile_fields_censor_full_name_suggestion()
social_profile_fields_social_user_name_display_suggestions_alter in modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module
Implements hook_social_user_name_display_suggestions_alter().

File

modules/social_features/social_profile/modules/social_profile_fields/social_profile_fields.module, line 95
The social profile fields module file.

Code

function social_profile_fields_censor_full_name_suggestion(array &$suggestions, AccountInterface $account) {

  // If there's no full name for us to censor then we're done.
  if (empty($suggestions['full_name'])) {
    return;
  }
  $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 both fields are enabled then there is nothing for us to censor.
  if ($use_first_name && $use_last_name) {
    return;
  }

  // The field that is enabled must be loaded and checked to be non-empty.

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

  // If we don't have a user profile then someone else has probably found data
  // from elsewhere and we have no fields to use.
  if (!$user_profile) {
    return;
  }

  // If both fields are disabled then we can unset the suggestion and be done.
  // We do this only if there's a profile because otherwise we may throw away
  // someone else's full_name pulled from elsewhere.
  if (!$use_first_name && !$use_last_name) {
    unset($suggestions['full_name']);
    return;
  }

  // We know that only one field is active.
  // So check just the first name.
  if ($use_first_name) {
    $first_name = $user_profile
      ->get('field_profile_first_name')->value;

    // If the only field we're allowed to use is empty then we remove the
    // suggestion altogether and fall back to something else.
    if (empty($first_name)) {
      unset($suggestions['full_name']);
    }
    else {
      $suggestions['full_name']['name'] = $first_name;
    }
  }
  else {
    $last_name = $user_profile
      ->get('field_profile_last_name')->value;

    // If the only field we're allowed to use is empty then we remove the
    // suggestion altogether and fall back to something else.
    if (empty($last_name)) {
      unset($suggestions['full_name']);
    }
    else {
      $suggestions['full_name']['name'] = $last_name;
    }
  }
}