You are here

function name_user_load in Name Field 8

Same name and namespace in other branches
  1. 7 name.module \name_user_load()

Implements hook_user_load().

File

./name.module, line 156
Defines an API for displaying and inputing names.

Code

function name_user_load(array $users) {

  // In the event there are a lot of user_load() calls, cache the results.
  $names =& drupal_static('name_user_realname_cache', []);
  $field =& drupal_static(__FUNCTION__, NULL);
  if (!isset($field)) {
    $field_name = Drupal::config('name.settings')
      ->get('user_preferred');
    $field = FieldConfig::loadByName('user', 'user', $field_name);
  }
  if ($field) {
    foreach ($users as $account) {
      $uid = $account
        ->id();
      if (isset($names[$uid])) {
        $users[$uid]->realname = $names[$uid];
      }
      else {
        if ($account
          ->hasField($field
          ->getName()) && !$account
          ->get($field
          ->getName())
          ->isEmpty()) {
          $manager = \Drupal::service('entity_type.manager');
          $renderer = \Drupal::service('renderer');
          $components = $account
            ->get($field
            ->getName())
            ->get(0)
            ->getValue();
          foreach ([
            'preferred',
            'alternative',
          ] as $key) {
            if ($key_value = $field
              ->getSetting($key . '_field_reference')) {
              $sep_value = $field
                ->getSetting($key . '_field_reference_separator');
              if ($value = name_get_additional_component($manager, $renderer, $account
                ->get($field
                ->getName()), $key_value, $sep_value)) {
                $components[$key] = $value;
              }
            }
          }
          $names[$uid] = \Drupal::service('name.formatter')
            ->format($components, $field
            ->getSetting('override_format'));
          $users[$uid]->realname = $names[$uid];
        }
      }
    }
  }
}