|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\session_example\Form; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormBase; |
| 6 | +use Drupal\Core\Form\FormStateInterface; |
| 7 | +use Drupal\Core\Link; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 | +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; |
| 10 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Form to allow the user to store information in their session. |
| 14 | + * |
| 15 | + * In this object we'll inject the session service. In the submission form we |
| 16 | + * got the session from a Request object supplied by the routing system. Either |
| 17 | + * of these work, because they're the same object. But we use injection here |
| 18 | + * because the buildForm() method does not have an easy way to derive the |
| 19 | + * Request object or the session. |
| 20 | + * |
| 21 | + * @ingroup session_example |
| 22 | + */ |
| 23 | +class SessionExampleForm extends FormBase { |
| 24 | + |
| 25 | + /** |
| 26 | + * The session object. |
| 27 | + * |
| 28 | + * We will use this to store information that the user submits, so that it |
| 29 | + * persists across requests. |
| 30 | + * |
| 31 | + * @var \Symfony\Component\HttpFoundation\Session\SessionInterface |
| 32 | + */ |
| 33 | + protected $session; |
| 34 | + |
| 35 | + /** |
| 36 | + * The cache tag invalidator service. |
| 37 | + * |
| 38 | + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface |
| 39 | + */ |
| 40 | + protected $cache_tag_invalidator; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructs a new SessionExampleForm object. |
| 44 | + * |
| 45 | + * @param \Symfony\Component\HttpFoundation\Session\SessionInterface |
| 46 | + * The session object. |
| 47 | + */ |
| 48 | + public function __construct(SessionInterface $session, CacheTagsInvalidatorInterface $invalidator) { |
| 49 | + $this->session = $session; |
| 50 | + $this->cache_tag_invalidator = $invalidator; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public static function create(ContainerInterface $container) { |
| 57 | + return new static( |
| 58 | + $container->get('session'), |
| 59 | + $container->get('cache_tags.invalidator') |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function getFormId() { |
| 67 | + return 'session_example_form'; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 74 | + $form['description'] = [ |
| 75 | + '#type' => 'item', |
| 76 | + '#title' => $this->t('Session Data Form'), |
| 77 | + '#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.'), |
| 78 | + ]; |
| 79 | + $form['name'] = [ |
| 80 | + '#type' => 'textfield', |
| 81 | + '#title' => $this->t('Name'), |
| 82 | + '#placeholder' => $this->t('Your name.'), |
| 83 | + '#default_value' => $this->session->get('session_example.name', ''), |
| 84 | + ]; |
| 85 | + $form['email'] = [ |
| 86 | + '#type' => 'textfield', |
| 87 | + '#title' => $this->t('Email'), |
| 88 | + '#placeholder' => $this->t('Your email address.'), |
| 89 | + '#default_value' => $this->session->get('session_example.email', ''), |
| 90 | + ]; |
| 91 | + $form['quest'] = [ |
| 92 | + '#type' => 'textfield', |
| 93 | + '#title' => $this->t('Quest'), |
| 94 | + '#placeholder' => $this->t('What is your quest?'), |
| 95 | + '#default_value' => $this->session->get('session_example.quest', ''), |
| 96 | + ]; |
| 97 | + $form['color'] = [ |
| 98 | + '#type' => 'select', |
| 99 | + '#title' => $this->t('Favorite Color'), |
| 100 | + '#options' => [ |
| 101 | + '' => $this->t('--'), |
| 102 | + 'red' => $this->t('Red'), |
| 103 | + 'blue' => $this->t('Blue'), |
| 104 | + 'yellow' => $this->t('Yellow'), |
| 105 | + 'argggh' => $this->t('Argggghhh!!'), |
| 106 | + ], |
| 107 | + '#default_value' => $this->session->get('session_example.color', ''), |
| 108 | + '#description' => $this->t('What is your favorite color?'), |
| 109 | + ]; |
| 110 | + $form['save'] = [ |
| 111 | + '#type' => 'submit', |
| 112 | + '#value' => $this->t('Save'), |
| 113 | + ]; |
| 114 | + $form['reset'] = [ |
| 115 | + '#type' => 'submit', |
| 116 | + '#value' => $this->t('Clear Session'), |
| 117 | + '#submit' => ['::submitClearSession'], |
| 118 | + ]; |
| 119 | + return $form; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Store a form value in the session. |
| 124 | + * |
| 125 | + * Form values are always a string. This means an empty string is a valid |
| 126 | + * value for when a user wants to remove a value from the session. We have to |
| 127 | + * handle this special case for the session object. |
| 128 | + * |
| 129 | + * @param string $key |
| 130 | + * The key. |
| 131 | + * @param string $value |
| 132 | + * The value. |
| 133 | + */ |
| 134 | + protected function setSessionValue($key, $value) { |
| 135 | + if (empty($value)) { |
| 136 | + // If the value is an empty string, remove the key from the session. |
| 137 | + $this->session->remove($key); |
| 138 | + } |
| 139 | + else { |
| 140 | + $this->session->set($key, $value); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * {@inheritdoc} |
| 146 | + */ |
| 147 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 148 | + // We get the submitted form information and store it in the session. We use |
| 149 | + // key names which include our module name in order to avoid namespace |
| 150 | + // collision. |
| 151 | + $this->setSessionValue('session_example.name', $form_state->getValue('name')); |
| 152 | + $this->setSessionValue('session_example.email', $form_state->getValue('email')); |
| 153 | + $this->setSessionValue('session_example.quest', $form_state->getValue('quest')); |
| 154 | + $this->setSessionValue('session_example.color', $form_state->getValue('color')); |
| 155 | + // Tell the user what happened here, and that they can look at another page |
| 156 | + // to see the result. |
| 157 | + $this->messenger()->addMessage($this->t('The session has been saved successfully. @link', [ |
| 158 | + '@link' => Link::createFromRoute('Check here.', 'session_example.view')->toString() |
| 159 | + ])); |
| 160 | + // Since we might have changed the session information, we will invalidate |
| 161 | + // the cache tag for this session. |
| 162 | + $this->invalidateCacheTag(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Remove all the session information. |
| 167 | + */ |
| 168 | + public function submitClearSession(array &$form, FormStateInterface $form_state) { |
| 169 | + $items = [ |
| 170 | + 'session_example.name', |
| 171 | + 'session_example.email', |
| 172 | + 'session_example.quest', |
| 173 | + 'session_example.color', |
| 174 | + ]; |
| 175 | + foreach ($items as $item) { |
| 176 | + $this->session->remove($item); |
| 177 | + } |
| 178 | + $this->messenger()->addMessage($this->t('Session is cleared.')); |
| 179 | + // Since we might have changed the session information, we will invalidate |
| 180 | + // the cache tag for this session. |
| 181 | + $this->invalidateCacheTag(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Invalidate the cache tag for this session. |
| 186 | + * |
| 187 | + * The form will use this method to invalidate the cache tag when the user |
| 188 | + * updates their information in the submit handlers. |
| 189 | + */ |
| 190 | + protected function invalidateCacheTag() { |
| 191 | + $this->cache_tag_invalidator->invalidateTags(['session_example:' . $this->session->getId()]); |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments