diff --git a/login_history.info.yml b/login_history.info.yml index 37e2648..9dfba3d 100644 --- a/login_history.info.yml +++ b/login_history.info.yml @@ -1,5 +1,6 @@ name: Login History description: A history of the site's user logins. core: 8.x +core_version_requirement: ^8 || ^9 type: module configure: login_history.report diff --git a/login_history.module b/login_history.module index b21861f..eb0ac4e 100644 --- a/login_history.module +++ b/login_history.module @@ -35,7 +35,7 @@ function login_history_user_login(User $account) { } // Now save the user's current login timestamp to login_history. - db_insert('login_history') + \Drupal::database()->insert('login_history') ->fields([ 'uid' => $account->id(), 'login' => $account->getLastLoginTime(), @@ -63,7 +63,7 @@ function login_history_last_login(AccountInterface $account = NULL) { if ($account->isAnonymous()) { return FALSE; } - $last_login = db_query("SELECT login, hostname, one_time, user_agent + $last_login = \Drupal::database()->query("SELECT login, hostname, one_time, user_agent FROM {login_history} WHERE uid = :uid ORDER BY login DESC diff --git a/src/Controller/LoginHistoryController.php b/src/Controller/LoginHistoryController.php index 79fc3f6..a9df464 100644 --- a/src/Controller/LoginHistoryController.php +++ b/src/Controller/LoginHistoryController.php @@ -4,6 +4,7 @@ namespace Drupal\login_history\Controller; use Drupal\Core\Access\AccessResult; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\user\UserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -14,6 +15,13 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; */ class LoginHistoryController extends ControllerBase { + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + /** * Drupal\Core\Entity\EntityTypeManagerInterface definition. * @@ -28,10 +36,13 @@ class LoginHistoryController extends ControllerBase { * The date formatter service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. + * @param \Drupal\Core\Database\Connection $database + * The database connection. */ - public function __construct(DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(DateFormatterInterface $date_formatter, EntityTypeManagerInterface $entity_type_manager, Connection $database) { $this->dateFormatter = $date_formatter; $this->entityTypeManager = $entity_type_manager; + $this->database = $database; } /** @@ -40,7 +51,8 @@ class LoginHistoryController extends ControllerBase { public static function create(ContainerInterface $container) { return new static( $container->get('date.formatter'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('database') ); } @@ -62,7 +74,7 @@ class LoginHistoryController extends ControllerBase { ['data' => $this->t('User Agent')], ]; - $query = db_select('login_history', 'lh') + $query = $this->database->select('login_history', 'lh') ->extend('Drupal\Core\Database\Query\TableSortExtender') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); @@ -109,7 +121,7 @@ class LoginHistoryController extends ControllerBase { foreach ($history as $entry) { $rows[] = [ $this->dateFormatter->format($entry->login, 'small'), - $users[$entry->uid]->getUsername(), + $users[$entry->uid]->getAccountName(), $entry->hostname, empty($entry->one_time) ? $this->t('Regular login') : $this->t('One-time login'), $entry->user_agent,