You are here

class IpLoginSettingsForm in IP Login 4.x

Configure ip_login settings.

Hierarchy

Expanded class hierarchy of IpLoginSettingsForm

1 string reference to 'IpLoginSettingsForm'
ip_login.routing.yml in ./ip_login.routing.yml
ip_login.routing.yml

File

src/Form/IpLoginSettingsForm.php, line 15

Namespace

Drupal\ip_login\Form
View source
class IpLoginSettingsForm extends ConfigFormBase {

  /**
   * The cache factory.
   *
   * @var \Drupal\Core\Cache\CacheFactoryInterface
   */
  protected $cacheFactory;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * Constructs a PathautoSettingsForm.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Defines the configuration object factory.
   * @param \Drupal\Core\Cache\CacheFactoryInterface $cache_factory
   *   The cache factory.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('cache_factory'), $container
      ->get('cache_tags.invalidator'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ip_login_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'ip_login.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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 the login mode is changed, we need to clear the render cache so the
    // auto-login can be attempted on subsequent page loads, if configured.
    if ($auto_login != $old_auto_login || $form_login != $old_form_login) {
      $this->cacheFactory
        ->get('render')
        ->deleteAll();
      $this->cacheTagsInvalidator
        ->invalidateTags([
        'ip_login',
      ]);

      // We need to invalidate the container so the middleware services are
      // registered based on the settings above.
      // @see \Drupal\ip_login\IpLoginServiceProvider::register()
      \Drupal::service('kernel')
        ->invalidateContainer();
    }
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
IpLoginSettingsForm::$cacheFactory protected property The cache factory.
IpLoginSettingsForm::$cacheTagsInvalidator protected property The cache tags invalidator.
IpLoginSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
IpLoginSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
IpLoginSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
IpLoginSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
IpLoginSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
IpLoginSettingsForm::__construct public function Constructs a PathautoSettingsForm. Overrides ConfigFormBase::__construct
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.