Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (50 sloc) 1.4 KB
<?php
namespace Drupal\custom_events\EventSubscriber;
use Drupal\custom_events\Event\UserLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class UserLoginSubscriber.
*
* @package Drupal\custom_events\EventSubscriber
*/
class UserLoginSubscriber implements EventSubscriberInterface {
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* Date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
// Static class constant => method on this class.
UserLoginEvent::EVENT_NAME => 'onUserLogin',
];
}
/**
* React to the user login event dispatched.
*
* @param \Drupal\custom_events\Event\UserLoginEvent $event
* Dat event object yo.
*/
public function onUserLogin(UserLoginEvent $event) {
$database = \Drupal::database();
$dateFormatter = \Drupal::service('date.formatter');
$account_created = $database->select('users_field_data', 'ud')
->fields('ud', ['created'])
->condition('ud.uid', $event->account->id())
->execute()
->fetchField();
drupal_set_message(t('Welcome, your account was created on %created_date.', [
'%created_date' => $dateFormatter->format($account_created, 'short'),
]));
}
}