CustomDataForm.php in Shibboleth Authentication 8
File
src/Form/CustomDataForm.php
View source
<?php
namespace Drupal\shib_auth\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\shib_auth\Login\ShibSessionVars;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomDataForm extends FormBase {
protected $shib_session;
protected $temp_store_factory;
protected $session_manager;
protected $current_user;
protected $custom_data_store;
public function __construct(ShibSessionVars $shib_session, PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user) {
$this->shib_session = $shib_session;
$this->temp_store_factory = $temp_store_factory;
$this->session_manager = $session_manager;
$this->current_user = $current_user;
$this->custom_data_store = $this->temp_store_factory
->get('shib_auth');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('shib_auth.shib_session_vars'), $container
->get('tempstore.private'), $container
->get('session_manager'), $container
->get('current_user'));
}
public function getFormId() {
return 'custom_data_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#cache'] = [
'max-age' => 0,
];
$form['email'] = [
'#type' => 'email',
'#title' => $this
->t('Email'),
'#default_value' => !empty($this->shib_session
->getEmail()) ? $this->shib_session
->getEmail() : '',
'#description' => 'Please enter a valid email address.',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->current_user
->isAnonymous() && !isset($_SESSION['session_started'])) {
$_SESSION['session_started'] = TRUE;
$this->session_manager
->start();
}
$this->custom_data_store
->set('custom_email', $form_state
->getValue('email'));
$form_state
->setRedirectUrl(Url::fromUri(\Drupal::request()
->getSchemeAndHttpHost() . $this->custom_data_store
->get('return_url')));
}
}