X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Flivereload%2Flivereload.module;fp=web%2Fmodules%2Fcontrib%2Flivereload%2Flivereload.module;h=df6b65854e84cdae244c1a082819a4eaf2e7d2c0;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/livereload/livereload.module b/web/modules/contrib/livereload/livereload.module new file mode 100644 index 000000000..df6b65854 --- /dev/null +++ b/web/modules/contrib/livereload/livereload.module @@ -0,0 +1,92 @@ +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. Note: this will only work if css aggregation is disabled.'), + '#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. Note: if this option is enabled, the "%aggregate_css" functionallity of Drupal will not work.', 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()] = []; + } +}