9 * Implements hook_page_top().
11 function session_exists_cache_context_test_page_top(array &$page_top) {
12 // Ensure this hook is invoked on every page load.
13 $page_top['#cache']['max-age'] = 0;
15 $request = \Drupal::request();
16 $session_exists = \Drupal::service('session_configuration')->hasSession($request);
17 $page_top['session_exists_cache_context_test'] = [
19 '#markup' => '<p>' . ($session_exists ? 'Session exists!' : 'Session does not exist!') . '</p>',
21 'cache_context_value' => [
22 '#markup' => '<code>[session.exists]=' . \Drupal::service('cache_context.session.exists')->getContext() . '</code>',
26 if (\Drupal::request()->query->get('trigger_session')) {
27 $_SESSION['session_exists_cache_context_test'] = TRUE;