diff --git a/session_example/session_example.routing.yml b/session_example/session_example.routing.yml index a67fde4..da22c65 100644 --- a/session_example/session_example.routing.yml +++ b/session_example/session_example.routing.yml @@ -11,6 +11,7 @@ session_example.view: path: '/examples/session_example/view' defaults: _controller: '\Drupal\session_example\Controller\SessionExampleController::showSession' + _title: 'Session Example: Current Values' requirements: _access: 'TRUE' options: diff --git a/session_example/src/Controller/SessionExampleController.php b/session_example/src/Controller/SessionExampleController.php index 83da33a..3bb4b28 100644 --- a/session_example/src/Controller/SessionExampleController.php +++ b/session_example/src/Controller/SessionExampleController.php @@ -25,8 +25,17 @@ class SessionExampleController extends ControllerBase { $session = $request->getSession(); $name = $session->get('session_example.name', $this->t('No Name')); $email = $session->get('session_example.email', $this->t('No EMail')); + $quest = $session->get('session_example.quest', $this->t('No Quest')); + $color = $session->get('session_example.color', $this->t('No Color')); return [ - '#markup' => $this->t('Name: @name, EMail: @email', ['@name' => $name, '@email' => $email]), + 'description' => [ + '#type' => 'item', + '#title' => $this->t('Saved Session Keys'), + '#markup' => $this->t('The example form lets you set some session keys. This page lists what their current values are.'), + ], + 'session_status' => [ + '#markup' => $this->t('Name: @name, EMail: @email, Quest: @quest, Color: @color', ['@name' => $name, '@email' => $email, '@quest' => $quest, '@color' => $color]), + ], '#cache' => [ 'max-age' => 0, ] diff --git a/session_example/src/Form/SessionExampleForm.php b/session_example/src/Form/SessionExampleForm.php index 7403d37..022f68f 100644 --- a/session_example/src/Form/SessionExampleForm.php +++ b/session_example/src/Form/SessionExampleForm.php @@ -76,15 +76,41 @@ class SessionExampleForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state) { $session = $this->getExampleSession(); + $form['description'] = [ + '#type' => 'item', + '#title' => $this->t('Session Data Test 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'), - '#default_value' => $session->get('session_example.name', 'My Name'), + '#placeholder' => $this->t('Your name.'), + '#default_value' => $session->get('session_example.name', ''), ]; $form['email'] = [ '#type' => 'textfield', '#title' => $this->t('Email'), - '#default_value' => $session->get('session_example.email', 'My Email'), + '#placeholder' => $this->t('Your email address.'), + '#default_value' => $session->get('session_example.email', ''), + ]; + $form['quest'] = [ + '#type' => 'textfield', + '#title' => $this->t('Quest'), + '#placeholder' => $this->t('What is your quest?'), + '#default_value' => $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' => $session->get('session_example.color', ''), + '#description' => $this->t('What is your favorite color?'), ]; $form['save'] = [ '#type' => 'submit', @@ -102,20 +128,34 @@ class SessionExampleForm extends FormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $session = $this->getExampleSession(); - $session->set('session_example.name', $form_state->getValue('name')); - $session->set('session_example.email', $form_state->getValue('email')); + $this->setSessionKeyIfChanged('session_example.name', $form_state->getValue('name')); + $this->setSessionKeyIfChanged('session_example.email', $form_state->getValue('email')); + $this->setSessionKeyIfChanged('session_example.quest', $form_state->getValue('quest')); + $this->setSessionKeyIfChanged('session_example.color', $form_state->getValue('color')); $link = $this->link_generator->generate($this->t('Check here'), Url::fromRoute('session_example.view')); drupal_set_message($this->t('The session has been saved successfully. @link', ['@link' => $link])); } /** + * Since we're using placeholders, do not write over an empty session key for an empty field. + */ + protected function setSessionKeyIfChanged($key, $value) { + $session = $this->getExampleSession(); + $old = $session->get($key, ''); + if ($value !== $old) { + $session->set($key, $value); + } + } + + /** * Clears the name and email session. */ public function submitClearSession(array &$form, FormStateInterface $form_state) { $session = $this->getExampleSession(); $session->remove('session_example.name'); $session->remove('session_example.email'); + $session->remove('session_example.quest'); + $session->remove('session_example.color'); drupal_set_message($this->t('Session is cleared.')); } } diff --git a/session_example/src/Tests/SessionExampleTest.php b/session_example/src/Tests/SessionExampleTest.php index f69a49b..36d0a4f 100644 --- a/session_example/src/Tests/SessionExampleTest.php +++ b/session_example/src/Tests/SessionExampleTest.php @@ -8,6 +8,7 @@ namespace Drupal\session_example\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\block\Tests\BlockTestBase; /** * Tests the basic functions of the Session Example module. @@ -27,17 +28,22 @@ class SessionExampleTest extends WebTestBase { */ public function testSessionExample() { + $this->drupalPlaceBlock('local_tasks_block', array('region' => 'content')); $this->drupalPlaceBlock('system_menu_block:tools', array()); $this->drupalGet(''); $this->assertLink(t('Session Example')); + $this->clickLink(t('Session Example')); - $this->assertFieldByName('name', 'My Name', 'Name Field contains My Name'); - $this->assertFieldByName('email', 'My Email', 'Email Field contains My Email'); + $this->assertRaw('placeholder="Your name."', 'Name placeholder contains Your name'); + $this->assertRaw('placeholder="Your email address."', 'Email placeholder contains Your email address.'); + $this->assertRaw('placeholder="What is your quest?"', 'Name placeholder contains What is your quest?'); $this->clickLink(t('View')); $this->assertText('No Name'); $this->assertText('No EMail'); + $this->assertText('No Quest'); + $this->assertText('No Color'); $this->clickLink(t('Form')); $this->drupalPostForm(NULL, array(), t('Save')); @@ -45,8 +51,23 @@ class SessionExampleTest extends WebTestBase { $this->assertLink(t('Check here')); $this->clickLink(t('Check here')); - $this->assertText('My Name'); - $this->assertText('My Email'); + $this->assertText('No Name'); + $this->assertText('No EMail'); + $this->assertText('No Quest'); + $this->assertText('No Color'); + + $form_data = [ + 'name' => 'Sir Lancelot', + // 'quest ' => 'To seek the Grail', + 'color' => 'blue', + ]; + $this->clickLink(t('Form')); + $this->drupalPostForm(NULL, $form_data, t('Save')); + + $this->clickLink(t('Check here')); + $this->assertText('Sir Lancelot'); + // $this->assertText('To seek the Grail'); + $this->assertText('blue'); $this->clickLink(t('Form')); $this->drupalPostForm(NULL, array(), t('Clear Session')); @@ -57,4 +78,5 @@ class SessionExampleTest extends WebTestBase { $this->assertText('No EMail'); } + }