Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / livereload / livereload.module
1 <?php
2
3 use Drupal\Core\Asset\AttachedAssetsInterface;
4 use Drupal\Core\Form\FormStateInterface;
5
6 /**
7  * Implements hook_page_attachments_alter().
8  *
9  * Add the livereload.js script to the site.
10  */
11 function livereload_page_attachments_alter(array &$page) {
12   if (\Drupal::currentUser()->hasPermission('use livereload') && \Drupal::config('livereload.settings')->get('livereload_js')) {
13     // Checking whether the file exists before including it. Because it can be
14     // external, we have to use @fopen instead of file_exists().
15     if (@fopen(livereload_get_path(), "r")) {
16       $page['#attached']['library'][] = 'livereload/drupal.livereload';
17     }
18     else {
19       drupal_set_message(t('Livereload.js could not be included.'), 'warning');
20     }
21   }
22 }
23
24 /**
25  * Build and prepare the path, we suggest the livereload script to be.
26  *
27  * @return string
28  *   The path, we suggest livereload script to be.
29  */
30 function livereload_get_path() {
31   $http_host = explode(':', $_SERVER['HTTP_HOST']);
32   return 'http://' . reset($http_host) . ':35729/livereload.js?snipver=1';
33 }
34
35 /**
36  * Implements hook_css_alter().
37  *
38  * Force CSS to be added with link tags, rather than @import.
39  */
40 function livereload_css_alter(&$css, AttachedAssetsInterface $assets) {
41   if (\Drupal::currentUser()->hasPermission('use livereload') && \Drupal::config('livereload.settings')->get('livereload_css')) {
42     foreach ($css as $key => $value) {
43       $css[$key]['preprocess'] = FALSE;
44     }
45   }
46 }
47
48 /**
49  * Implements hook_form_FORM_ID_alter().
50  *
51  * Add the possibility to en- and disable the livereload.js.
52  */
53 function livereload_form_system_performance_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
54   $form['livereload'] = array(
55     '#type' => 'fieldset',
56     '#title' => 'LiveReload',
57   );
58   $form['livereload']['livereload_js'] = array(
59     '#type' => 'checkbox',
60     '#title' => t('Add livereload.js. <em>Note: this will only work if css aggregation is disabled.</em>'),
61     '#default_value' => \Drupal::config('livereload.settings')->get('livereload_js'),
62     '#disabled' => FALSE,
63   );
64   $form['livereload']['livereload_css'] = array(
65     '#type' => 'checkbox',
66     '#title' => t('Force CSS to be added with link tags, rather than @import. <em>Note: if this option is enabled, the "%aggregate_css" functionallity of Drupal will not work.</em>', array('%aggregate_css' => t('Aggregate and compress CSS files.'))),
67     '#default_value' => \Drupal::config('livereload.settings')->get('livereload_css'),
68     '#disabled' => FALSE,
69   );
70
71   // Add submit handler to save contact configuration.
72   $form['#submit'][] = 'livereload_form_system_performance_settings_submit';
73 }
74
75 /**
76  * @see livereload_form_system_performance_settings_alter()
77  */
78 function livereload_form_system_performance_settings_submit($form, FormStateInterface &$form_state) {
79   \Drupal::configFactory()->getEditable('livereload.settings')
80     ->set('livereload_js', $form_state->getValue('livereload_js'))
81     ->set('livereload_css', $form_state->getValue('livereload_css'))
82     ->save();
83 }
84
85 /**
86  * Implements hook_library_info_alter().
87  */
88 function livereload_library_info_alter(&$libraries, $extension) {
89   if ($extension === 'livereload' && isset($libraries['drupal.livereload'])) {
90     $libraries['drupal.livereload']['js'][livereload_get_path()] = [];
91   }
92 }