class SessionExampleForm in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/session_example/src/Form/SessionExampleForm.php \Drupal\session_example\Form\SessionExampleForm
Form to allow the user to store information in their session.
In this object we'll inject the session service. In the submission form we got the session from a Request object supplied by the routing system. Either of these work, because they're the same object. But we use injection here because the buildForm() method does not have an easy way to derive the Request object or the session.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\session_example\Form\SessionExampleForm
Expanded class hierarchy of SessionExampleForm
Related topics
1 string reference to 'SessionExampleForm'
- session_example.routing.yml in session_example/
session_example.routing.yml - session_example/session_example.routing.yml
File
- session_example/
src/ Form/ SessionExampleForm.php, line 23
Namespace
Drupal\session_example\FormView source
class SessionExampleForm extends FormBase {
/**
* The session object.
*
* We will use this to store information that the user submits, so that it
* persists across requests.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
/**
* The cache tag invalidator service.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagInvalidator;
/**
* Constructs a new SessionExampleForm object.
*
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* The session object.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator
* The cache tag invalidator service.
*/
public function __construct(SessionInterface $session, CacheTagsInvalidatorInterface $invalidator) {
$this->session = $session;
$this->cacheTagInvalidator = $invalidator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('session'), $container
->get('cache_tags.invalidator'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'session_example_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Store a form value in the session.
*
* Form values are always a string. This means an empty string is a valid
* value for when a user wants to remove a value from the session. We have to
* handle this special case for the session object.
*
* @param string $key
* The key.
* @param string $value
* The value.
*/
protected function setSessionValue($key, $value) {
if (empty($value)) {
// If the value is an empty string, remove the key from the session.
$this->session
->remove($key);
}
else {
$this->session
->set($key, $value);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// We get the submitted form information and store it in the session. We use
// key names which include our module name in order to avoid namespace
// collision.
$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'));
// Tell the user what happened here, and that they can look at another page
// to see the result.
$this
->messenger()
->addMessage($this
->t('The session has been saved successfully. @link', [
'@link' => Link::createFromRoute('Check here.', 'session_example.view')
->toString(),
]));
// Since we might have changed the session information, we will invalidate
// the cache tag for this session.
$this
->invalidateCacheTag();
}
/**
* Remove all the session information.
*/
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.'));
// Since we might have changed the session information, we will invalidate
// the cache tag for this session.
$this
->invalidateCacheTag();
}
/**
* Invalidate the cache tag for this session.
*
* The form will use this method to invalidate the cache tag when the user
* updates their information in the submit handlers.
*/
protected function invalidateCacheTag() {
$this->cacheTagInvalidator
->invalidateTags([
'session_example:' . $this->session
->getId(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SessionExampleForm:: |
protected | property | The cache tag invalidator service. | |
SessionExampleForm:: |
protected | property | The session object. | |
SessionExampleForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
SessionExampleForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SessionExampleForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SessionExampleForm:: |
protected | function | Invalidate the cache tag for this session. | |
SessionExampleForm:: |
protected | function | Store a form value in the session. | |
SessionExampleForm:: |
public | function | Remove all the session information. | |
SessionExampleForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
SessionExampleForm:: |
public | function | Constructs a new SessionExampleForm object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |