IpLoginSettingsForm.php in IP Login 4.x
File
src/Form/IpLoginSettingsForm.php
View source
<?php
namespace Drupal\ip_login\Form;
use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IpLoginSettingsForm extends ConfigFormBase {
protected $cacheFactory;
protected $cacheTagsInvalidator;
public function __construct(ConfigFactoryInterface $config_factory, CacheFactoryInterface $cache_factory, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
parent::__construct($config_factory);
$this->cacheFactory = $cache_factory;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('cache_factory'), $container
->get('cache_tags.invalidator'));
}
public function getFormId() {
return 'ip_login_settings_form';
}
protected function getEditableConfigNames() {
return [
'ip_login.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('ip_login.settings');
$form['auto_login'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Login automatically'),
'#description' => $this
->t('When an anonymous user accesses any page of the site, the module will attempt to log them in automatically.'),
'#default_value' => $config
->get('auto_login'),
];
$form['form_login'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Login form'),
'#description' => $this
->t('An additional action link is added to the user login form.'),
'#default_value' => $config
->get('form_login'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('ip_login.settings');
$form_state
->cleanValues();
$auto_login = $form_state
->getValue('auto_login');
$form_login = $form_state
->getValue('form_login');
$old_auto_login = $config
->get('auto_login');
$config
->set('auto_login', $auto_login);
$old_form_login = $config
->get('form_login');
$config
->set('form_login', $form_login);
if ($auto_login != $old_auto_login || $form_login != $old_form_login) {
$this->cacheFactory
->get('render')
->deleteAll();
$this->cacheTagsInvalidator
->invalidateTags([
'ip_login',
]);
\Drupal::service('kernel')
->invalidateContainer();
}
$config
->save();
}
}