You are here

function uma_UserName in User Menu Avatar (User Image in Menu) 8.5

Same name and namespace in other branches
  1. 8.4 user_menu_avatar.module \uma_UserName()

Set $name value.

2 calls to uma_UserName()
uma_Markup in ./user_menu_avatar.module
Build our new replacement markup.
user_menu_avatar_preprocess_menu in ./user_menu_avatar.module
Implements hook_preprocess_hook().

File

./user_menu_avatar.module, line 94
Display user picture and/or user name in menu.

Code

function uma_UserName() {

  // Instantiate $name to empty string.
  $name = NULL;

  // Check if we should show user name.
  if (\Drupal::currentUser()
    ->isAnonymous() && uma_CFV()['showAnonymousName'] === 'yes') {
    if (uma_CFV()['customAnonymousText'] != '') {

      // Get our anonymous text.
      $name = uma_CFV()['customAnonymousText'];
    }
    else {

      // Set $name as default username.
      $name = uma_getUser()['currentUser']
        ->getDisplayName();
    }
  }
  if (!\Drupal::currentUser()
    ->isAnonymous() && uma_CFV()['showName'] === 'yes') {

    // Get our custom field name.
    $customNameField = uma_CFV()['customNameField'];

    // Check that custom field exists.
    if (!empty(FieldConfig::loadByName('user', 'user', $customNameField)) && uma_getUser()['currentUser']->{$customNameField}->value != '') {

      // Set $name as the value custom name field.
      $name = uma_getUser()['currentUser']->{$customNameField}->value;
    }
    else {

      // Set $name as default username.
      $name = uma_getUser()['currentUser']
        ->getDisplayName();
    }
  }
  return $name;
}