AutologoutWarningBlock.php in Automated Logout 8
File
src/Plugin/Block/AutologoutWarningBlock.php
View source
<?php
namespace Drupal\autologout\Plugin\Block;
use Drupal\autologout\AutologoutManager;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\Config;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AutologoutWarningBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $dateFormatter;
protected $autoLogoutSettings;
protected $manager;
protected $builder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, DateFormatterInterface $date_formatter, Config $autologout_settings, AutologoutManager $manager, FormBuilder $builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->dateFormatter = $date_formatter;
$this->autoLogoutSettings = $autologout_settings;
$this->manager = $manager;
$this->builder = $builder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('date.formatter'), $container
->get('config.factory')
->get('autologout.settings'), $container
->get('autologout.manager'), $container
->get('form_builder'));
}
public function defaultConfiguration() {
$return = [];
return $return;
}
public function build() {
$autologout_manager = $this->manager;
if ($autologout_manager
->preventJs()) {
return [];
}
if ($autologout_manager
->refreshOnly()) {
$markup = $this
->t('Autologout does not apply on the current page,
you will be kept logged in whilst this page remains open.');
}
elseif ($this->moduleHandler
->moduleExists('jstimer') && $this->moduleHandler
->moduleExists('jst_timer')) {
return $this->builder
->getForm('Drupal\\autologout\\Form\\AutologoutBlockForm');
}
else {
$timeout = (int) $autologout_manager
->getUserTimeout();
$markup = $this
->t('You will be logged out in @time if this page is not refreshed before then.', [
'@time' => $this->dateFormatter
->formatInterval($timeout),
]);
}
return [
'#type' => 'markup',
'#markup' => $markup,
];
}
}