Backup of db before drupal security update
[yaffs-website] / web / core / modules / big_pipe / big_pipe.module
1 <?php
2
3 /**
4  * @file
5  * Adds BigPipe no-JS detection.
6  */
7
8 use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * Implements hook_help().
14  */
15 function big_pipe_help($route_name, RouteMatchInterface $route_match) {
16   switch ($route_name) {
17     case 'help.page.big_pipe':
18       $output = '<h3>' . t('About') . '</h3>';
19       $output .= '<p>' . t('The BigPipe module sends pages with dynamic content in a way that allows browsers to show them much faster. For more information, see the <a href=":big_pipe-documentation">online documentation for the BigPipe module</a>.', [':big_pipe-documentation' => 'https://www.drupal.org/documentation/modules/big_pipe']) . '</p>';
20       $output .= '<h3>' . t('Uses') . '</h3>';
21       $output .= '<dl>';
22       $output .= '<dt>' . t('Speeding up your site') . '</dt>';
23       $output .= '<dd>' . t('The module requires no configuration. Every part of the page contains metadata that allows BigPipe to figure this out on its own.') . '</dd>';
24       $output .= '</dl>';
25
26       return $output;
27   }
28 }
29
30 /**
31  * Implements hook_page_attachments().
32  *
33  * @see \Drupal\big_pipe\Controller\BigPipeController::setNoJsCookie()
34  */
35 function big_pipe_page_attachments(array &$page) {
36   // Routes that don't use BigPipe also don't need no-JS detection.
37   if (\Drupal::routeMatch()->getRouteObject()->getOption('_no_big_pipe')) {
38     return;
39   }
40
41   $request = \Drupal::request();
42   // BigPipe is only used when there is an actual session, so only add the no-JS
43   // detection when there actually is a session.
44   // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy.
45   $session_exists = \Drupal::service('session_configuration')->hasSession($request);
46   $page['#cache']['contexts'][] = 'session.exists';
47   // Only do the no-JS detection while we don't know if there's no JS support:
48   // avoid endless redirect loops.
49   $has_big_pipe_nojs_cookie = $request->cookies->has(BigPipeStrategy::NOJS_COOKIE);
50   $page['#cache']['contexts'][] = 'cookies:' . BigPipeStrategy::NOJS_COOKIE;
51   if ($session_exists) {
52     if (!$has_big_pipe_nojs_cookie) {
53       // Let server set the BigPipe no-JS cookie.
54       $page['#attached']['html_head'][] = [
55         [
56           // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
57           '#tag' => 'meta',
58           '#noscript' => TRUE,
59           '#attributes' => [
60             'http-equiv' => 'Refresh',
61             'content' => '0; URL=' . Url::fromRoute('big_pipe.nojs', [], ['query' => \Drupal::service('redirect.destination')->getAsArray()])->toString(),
62           ],
63         ],
64         'big_pipe_detect_nojs',
65       ];
66     }
67     else {
68       // Let client delete the BigPipe no-JS cookie.
69       $page['#attached']['html_head'][] = [
70         [
71           '#tag' => 'script',
72           '#value' => 'document.cookie = "' . BigPipeStrategy::NOJS_COOKIE . '=1; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"',
73         ],
74         'big_pipe_detect_js',
75       ];
76     }
77   }
78 }