View source
<?php
namespace Drupal\session_example\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class SessionExampleForm extends FormBase {
protected $session;
protected $cacheTagInvalidator;
public function __construct(SessionInterface $session, CacheTagsInvalidatorInterface $invalidator) {
$this->session = $session;
$this->cacheTagInvalidator = $invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('session'), $container
->get('cache_tags.invalidator'));
}
public function getFormId() {
return 'session_example_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'item',
'#title' => $this
->t('Session Data Form'),
'#markup' => $this
->t('In this example form, data that you enter into the form will be saved into your session data, which persists until you log out of Drupal.'),
];
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#placeholder' => $this
->t('Your name.'),
'#default_value' => $this->session
->get('session_example.name', ''),
];
$form['email'] = [
'#type' => 'textfield',
'#title' => $this
->t('Email'),
'#placeholder' => $this
->t('Your email address.'),
'#default_value' => $this->session
->get('session_example.email', ''),
];
$form['quest'] = [
'#type' => 'textfield',
'#title' => $this
->t('Quest'),
'#placeholder' => $this
->t('What is your quest?'),
'#default_value' => $this->session
->get('session_example.quest', ''),
];
$form['color'] = [
'#type' => 'select',
'#title' => $this
->t('Favorite Color'),
'#options' => [
'' => $this
->t('--'),
'red' => $this
->t('Red'),
'blue' => $this
->t('Blue'),
'yellow' => $this
->t('Yellow'),
'argggh' => $this
->t('Argggghhh!!'),
],
'#default_value' => $this->session
->get('session_example.color', ''),
'#description' => $this
->t('What is your favorite color?'),
];
$form['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
$form['reset'] = [
'#type' => 'submit',
'#value' => $this
->t('Clear Session'),
'#submit' => [
'::submitClearSession',
],
];
return $form;
}
protected function setSessionValue($key, $value) {
if (empty($value)) {
$this->session
->remove($key);
}
else {
$this->session
->set($key, $value);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->setSessionValue('session_example.name', $form_state
->getValue('name'));
$this
->setSessionValue('session_example.email', $form_state
->getValue('email'));
$this
->setSessionValue('session_example.quest', $form_state
->getValue('quest'));
$this
->setSessionValue('session_example.color', $form_state
->getValue('color'));
$this
->messenger()
->addMessage($this
->t('The session has been saved successfully. @link', [
'@link' => Link::createFromRoute('Check here.', 'session_example.view')
->toString(),
]));
$this
->invalidateCacheTag();
}
public function submitClearSession(array &$form, FormStateInterface $form_state) {
$items = [
'session_example.name',
'session_example.email',
'session_example.quest',
'session_example.color',
];
foreach ($items as $item) {
$this->session
->remove($item);
}
$this
->messenger()
->addMessage($this
->t('Session is cleared.'));
$this
->invalidateCacheTag();
}
protected function invalidateCacheTag() {
$this->cacheTagInvalidator
->invalidateTags([
'session_example:' . $this->session
->getId(),
]);
}
}