Further modules included.
[yaffs-website] / web / modules / contrib / livereload / livereload.module
diff --git a/web/modules/contrib/livereload/livereload.module b/web/modules/contrib/livereload/livereload.module
new file mode 100644 (file)
index 0000000..df6b658
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+use Drupal\Core\Asset\AttachedAssetsInterface;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Implements hook_page_attachments_alter().
+ *
+ * Add the livereload.js script to the site.
+ */
+function livereload_page_attachments_alter(array &$page) {
+  if (\Drupal::currentUser()->hasPermission('use livereload') && \Drupal::config('livereload.settings')->get('livereload_js')) {
+    // Checking whether the file exists before including it. Because it can be
+    // external, we have to use @fopen instead of file_exists().
+    if (@fopen(livereload_get_path(), "r")) {
+      $page['#attached']['library'][] = 'livereload/drupal.livereload';
+    }
+    else {
+      drupal_set_message(t('Livereload.js could not be included.'), 'warning');
+    }
+  }
+}
+
+/**
+ * Build and prepare the path, we suggest the livereload script to be.
+ *
+ * @return string
+ *   The path, we suggest livereload script to be.
+ */
+function livereload_get_path() {
+  $http_host = explode(':', $_SERVER['HTTP_HOST']);
+  return 'http://' . reset($http_host) . ':35729/livereload.js?snipver=1';
+}
+
+/**
+ * Implements hook_css_alter().
+ *
+ * Force CSS to be added with link tags, rather than @import.
+ */
+function livereload_css_alter(&$css, AttachedAssetsInterface $assets) {
+  if (\Drupal::currentUser()->hasPermission('use livereload') && \Drupal::config('livereload.settings')->get('livereload_css')) {
+    foreach ($css as $key => $value) {
+      $css[$key]['preprocess'] = FALSE;
+    }
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Add the possibility to en- and disable the livereload.js.
+ */
+function livereload_form_system_performance_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
+  $form['livereload'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'LiveReload',
+  );
+  $form['livereload']['livereload_js'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Add livereload.js. <em>Note: this will only work if css aggregation is disabled.</em>'),
+    '#default_value' => \Drupal::config('livereload.settings')->get('livereload_js'),
+    '#disabled' => FALSE,
+  );
+  $form['livereload']['livereload_css'] = array(
+    '#type' => 'checkbox',
+    '#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.'))),
+    '#default_value' => \Drupal::config('livereload.settings')->get('livereload_css'),
+    '#disabled' => FALSE,
+  );
+
+  // Add submit handler to save contact configuration.
+  $form['#submit'][] = 'livereload_form_system_performance_settings_submit';
+}
+
+/**
+ * @see livereload_form_system_performance_settings_alter()
+ */
+function livereload_form_system_performance_settings_submit($form, FormStateInterface &$form_state) {
+  \Drupal::configFactory()->getEditable('livereload.settings')
+    ->set('livereload_js', $form_state->getValue('livereload_js'))
+    ->set('livereload_css', $form_state->getValue('livereload_css'))
+    ->save();
+}
+
+/**
+ * Implements hook_library_info_alter().
+ */
+function livereload_library_info_alter(&$libraries, $extension) {
+  if ($extension === 'livereload' && isset($libraries['drupal.livereload'])) {
+    $libraries['drupal.livereload']['js'][livereload_get_path()] = [];
+  }
+}