From 62448565d3f51a38e73bd5e7fe9d2dc1d8c2e640 Mon Sep 17 00:00:00 2001
From: Matthew Slater <matslats@fastmail.com>
Date: Wed, 14 Nov 2018 15:01:01 +1100
Subject: [PATCH] improved handling of name field on user profile form;

---
 alt_login.module                          | 31 ++++++++++++++++-------
 src/AltLoginMethodInterface.php           |  2 +-
 src/AltLoginMethodManager.php             |  3 ---
 src/Plugin/AltLoginMethod/AddressName.php | 18 ++++++-------
 src/Plugin/AltLoginMethod/Email.php       |  5 +---
 5 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/alt_login.module b/alt_login.module
index 4a456d6..ecb247b 100644
--- a/alt_login.module
+++ b/alt_login.module
@@ -35,7 +35,7 @@ function alt_login_user_format_name_alter(&$name, $account) {
         ->replace(\Drupal::config('alt_login.settings')->get('display_anon'), ['user' => $account]);
     }
     elseif ($template = \Drupal::config('alt_login.settings')->get('display')) {
-      if (!$account instanceof User) {
+      if (!$account instanceof UserInterface) {
         $account = User::load($account->id());
       }
       $names[$uid] = \Drupal::token()->replace($template, ['user' => $account]);
@@ -65,7 +65,8 @@ function alt_login_module_implements_alter(&$implementations, $hook) {
  */
 function alt_login_form_alter(&$form, $form_state, $form_id) {
   // Show all the login options on the user form.
-  if($form_id == 'user_form' or $form_id == 'user_create_form' or $form_id == 'user_register_form')  {
+  $user_forms = ['user_form', 'user_create_form', 'user_register_form', 'user_admin_form'];
+  if (in_array($form_id, $user_forms)) {
     // Rewrite the username field to be clearer.
     $user = $form_state->getFormObject()->getEntity();
     $aliases = alt_login_get_aliases($user);
@@ -76,9 +77,12 @@ function alt_login_form_alter(&$form, $form_state, $form_id) {
         $namefield['#value'] = user_password(8);
       }
       elseif (count($aliases) == 1) {
-        $namefield['#title'] = t('Login using');
-        $namefield['#default_value'] = reset($aliases);
-        $namefield['#disabled'] = TRUE;
+        $namefield = [
+          '#type' => 'markup',
+          '#markup' => t("Login using '%name'", ['%name' => reset($aliases)]),
+          '#description' => t('This is determined by @descriptions.', ['@descriptions' => implode(', ', alt_login_active_descriptions())]),
+          '#weight' => -1
+        ];
       }
       else {
         $namefield['#access'] = FALSE;
@@ -127,7 +131,7 @@ function alt_login_login_name_element_validate(&$element, $form_state) {
  * @return string | NULL
  */
 function alt_login_convert_alias($alias) {
-  foreach (activePlugins() as $plugin) {
+  foreach (alt_login_active_plugins() as $plugin) {
     if ($plugin->applies($alias)) {
       if ($name = $plugin->getUsernameFromAlias($alias)) {
         return $name;
@@ -145,7 +149,7 @@ function alt_login_convert_alias($alias) {
  */
 function alt_login_get_aliases(UserInterface $user) {
   $alts = [];
-  foreach (activePlugins() as $plugin_id => $plugin) {
+  foreach (alt_login_active_plugins() as $plugin_id => $plugin) {
     $alts[$plugin_id] = $plugin->getAlias($user);
   }
   return $alts;
@@ -195,7 +199,7 @@ function alt_login_user_presave(UserInterface $account) {
  */
 function alt_login_validate_dedupe_aliases($form, $form_state) {
   $user = $form_state->getFormObject()->buildEntity($form, $form_state);
-  foreach (activePlugins() as $plugin) {
+  foreach (alt_login_active_plugins() as $plugin) {
     if ($field_name = $plugin->dedupeAlias($user)) {
       $form_state->setErrorByName($field_name, t('This alias is already taken.'));
     }
@@ -225,7 +229,7 @@ function alt_login_get_alias(UserInterface $user, $plugin_id) {
  *
  * @return AltLoginMethodInterface[]
  */
-function activePlugins() {
+function alt_login_active_plugins() {
   $plugin_manager = \Drupal::service('alt_login.method_manager');
   foreach (\Drupal::Config('alt_login.settings')->get('aliases') as $plugin_id) {
     $plugins[$plugin_id] = $plugin_manager->createInstance($plugin_id);
@@ -233,6 +237,15 @@ function activePlugins() {
   return $plugins;
 }
 
+function alt_login_active_descriptions() {
+  $plugin_manager = \Drupal::service('alt_login.method_manager');
+  foreach (\Drupal::Config('alt_login.settings')->get('aliases') as $plugin_id) {
+    $def = $plugin_manager->getDefinition($plugin_id);
+    $descriptions[] = $def['description'];
+  }
+  return $descriptions;
+}
+
 /**
  * Implements hook_alt_login_info_alter().
  *
diff --git a/src/AltLoginMethodInterface.php b/src/AltLoginMethodInterface.php
index 1fd4510..5254094 100644
--- a/src/AltLoginMethodInterface.php
+++ b/src/AltLoginMethodInterface.php
@@ -21,7 +21,7 @@ interface AltLoginMethodInterface {
 
   /**
    * Check wheteher the given string is likely to be an alias generated by this
-   * plugin.
+   * plugin. i.e. whether it is worth testing.
    *
    * @param string $alias
    *
diff --git a/src/AltLoginMethodManager.php b/src/AltLoginMethodManager.php
index 360718f..b5d4a7b 100644
--- a/src/AltLoginMethodManager.php
+++ b/src/AltLoginMethodManager.php
@@ -39,8 +39,5 @@ class AltLoginMethodManager extends DefaultPluginManager{
     }
     return $names;
   }
-
-
-
 }
 
diff --git a/src/Plugin/AltLoginMethod/AddressName.php b/src/Plugin/AltLoginMethod/AddressName.php
index d5022e3..ce8c1b4 100644
--- a/src/Plugin/AltLoginMethod/AddressName.php
+++ b/src/Plugin/AltLoginMethod/AddressName.php
@@ -2,12 +2,15 @@
 
 namespace Drupal\alt_login\Plugin\AltLoginMethod;
 
+use Drupal\user\Entity\User;
 use Drupal\alt_login\AltLoginMethodInterface;
 use Drupal\user\UserInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\Database\Database;
+use Drupal\Core\Database\Connection;
 use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Plugin implementation for logging in with the user name as an alias.
@@ -15,10 +18,10 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
  * @AltLoginMethod(
  *   id = "address_name",
  *   label = @Translation("Full name"),
- *   description = @Translation("Given name + Family name from address field")
+ *   description = @Translation("the given name and family name from the address field")
  * )
  */
-class AddressName implements AltLoginMethodInterface {
+class AddressName implements AltLoginMethodInterface, ContainerFactoryPluginInterface {
 
   use StringTranslationTrait;
 
@@ -29,7 +32,7 @@ class AddressName implements AltLoginMethodInterface {
   private $fieldName;
 
   /**
-   * @var Database
+   * @var Connection
    */
   private $database;
 
@@ -40,10 +43,10 @@ class AddressName implements AltLoginMethodInterface {
 
   /**
    * @param EmailValidator $entity_field_manager
-   * @param Database $database
+   * @param Connection $database
    * @param MessengerInterface $messenger
    */
-  function __construct($configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, Database $database, MessengerInterface $messenger) {
+  function __construct(EntityFieldManagerInterface $entity_field_manager, Connection $database, MessengerInterface $messenger) {
     $this->entityFieldManager = $entity_field_manager;
     $this->database = $database;
     $this->messenger = $messenger;
@@ -59,9 +62,6 @@ class AddressName implements AltLoginMethodInterface {
    */
   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
     return new static (
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
       $container->get('entity_field.manager'),
       $container->get('database'),
       $container->get('messenger')
diff --git a/src/Plugin/AltLoginMethod/Email.php b/src/Plugin/AltLoginMethod/Email.php
index 0696734..0a883ff 100644
--- a/src/Plugin/AltLoginMethod/Email.php
+++ b/src/Plugin/AltLoginMethod/Email.php
@@ -33,7 +33,7 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
    * @param EntityTypeManagerInterface $entity_type_manager
    * @param AltLoginMethod\EmailValidator $email_validator
    */
-  function __construct($configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EmailValidatorInterface $email_validator) {
+  function __construct(EntityTypeManagerInterface $entity_type_manager, EmailValidatorInterface $email_validator) {
     $this->entityTypeManager = $entity_type_manager;
     $this->emailValidator = $email_validator;
   }
@@ -48,9 +48,6 @@ class Email extends Username implements AltLoginMethodInterface, ContainerFactor
    */
   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
     return new static (
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
       $container->get('entity_type.manager'),
       $container->get('email.validator')
     );
-- 
GitLab