From b69364e49d18171262a33a764f7a1bb60ea60eb9 Mon Sep 17 00:00:00 2001
From: Matthew Slater <matslats@fastmail.com>
Date: Sun, 6 Oct 2024 20:27:20 +0200
Subject: [PATCH] better calling of logger channel and fixed warning

---
 alt_login.module | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/alt_login.module b/alt_login.module
index 10b65b5..9bd7e9f 100644
--- a/alt_login.module
+++ b/alt_login.module
@@ -49,7 +49,7 @@ function alt_login_user_format_name_alter(&$name, AccountInterface $account) {
       );
     }
     // If the above didn't produce a string, fall back to the username.
-    if (empty(trim($names[$uid]))) {
+    if (!isset($names[$uid]) or empty(trim($names[$uid]))) {
       $names[$uid] = $account->getAccountName();
     }
   }
@@ -212,6 +212,7 @@ function alt_login_tokens_alter(&$replacements, array $context, $bubbleable_meta
   if ($context['type'] == 'entity' and isset($context['data']['entity']) and isset($replacements['[user:name]'])) {
     $replacements['[user:name]'] = implode(t(' OR '), alt_login_get_aliases($context['data']['user']));
   }
+      //\Drupal::entityQuery('user')->condition('name', "$new_name%", 'LIKE')->accessCheck(FALSE)->execute();
 }
 
 /**
@@ -229,7 +230,6 @@ function alt_login_user_presave(UserInterface $account) {
     $login_aliases = \Drupal::Config('alt_login.settings')->get('aliases');
     if (!in_array('username', $login_aliases)) {
       $new_name = substr($account->getEmail(), 0, strpos($account->getEmail(), '@'));
-      //\Drupal::entityQuery('user')->condition('name', "$new_name%", 'LIKE')->accessCheck(FALSE)->execute();
       $lastname = \Drupal::database()->select('users_field_data', 'u')
         ->fields('u', ['name'])
         ->condition('name', "$new_name%", 'LIKE')
@@ -245,7 +245,7 @@ function alt_login_user_presave(UserInterface $account) {
       }
       $account->setUsername($new_name);
       // Note that the the User RegisterForm logs user creation with another name.
-      \Drupal::logger('alt_login')->notice('New user name set to '.$new_name);
+      \Drupal::service('logger.channel.alt_login')->notice('New user name set to '.$new_name);
     }
 
     // Construct the user->name from the address given and last names
@@ -253,12 +253,12 @@ function alt_login_user_presave(UserInterface $account) {
     if (empty($login_aliases['username']) and $login_aliases['address_name'] and $address = $account->address->getValue()) {
       $givenname = isset($address[0]['given_name']) ? trim($address[0]['given_name']) : '';
       $familyname = isset($address[0]['family_name']) ? trim($address[0]['family_name']) : '';
-      $name = trim("$givenname .' '.$familyname");
+      $name = trim($givenname .' '. $familyname);
       if ($name) {
         $account->setUsername($name);
       }
       else {
-        \Drupal::logger('alt_login')->error(t("Address name is required to set username."));
+        \Drupal::service('logger.channel.alt_login')->error(t("Address name is required to set username."));
       }
     }
   }
-- 
GitLab