πŸ‡¨πŸ‡΄Colombia @julianmancera

Account created on 11 May 2010, about 14 years ago
  • Drupal Architect at FFWΒ  …
#

Recent comments

πŸ‡¨πŸ‡΄Colombia julianmancera

To accomplish this you can add an event subscriber to AUTHMAP_ALTER event like:

<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Database\Connection;
use Drupal\externalauth\Event\ExternalAuthAuthmapAlterEvent;
use Drupal\externalauth\Event\ExternalAuthEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class ExternalAuthSubscriber.
 *
 * @package Drupal\my_module\EventSubscriber
 */
class ExternalAuthSubscriber implements EventSubscriberInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected Connection $database;

  /**
   * Constructor.
   *
   * We use dependency injection.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      // Static class constant => method on this class.
      ExternalAuthEvents::AUTHMAP_ALTER => 'onUserRegisterMapAlter',
    ];
  }

  /**
   * Subscribe to the external auth map  event dispatched.
   *
   * @param \Drupal\externalauth\Event\ExternalAuthAuthmapAlterEvent $event
   *   Mapping event object.
   */
  public function onUserRegisterMapAlter(ExternalAuthAuthmapAlterEvent $event) {
    $account = $event->getAuthname();

 

    // Looking for user email.
    $query = $this->database->select('users_field_data', 'ufs')
      ->fields('ufs', ['name'])
      ->range(0, 1)
      ->condition('mail', $email)
      ->execute();

    $user_account = $query->fetchObject();

    if ($user_account) {
      $event->setUsername($user_account->name);
    }
  }

}

Though some times you have different username that the authname, for such case you can use the patch attached

πŸ‡¨πŸ‡΄Colombia julianmancera

@borisson_ did you find how to integrate it?

Production build 0.69.0 2024