3 use Drupal\Core\Asset\AttachedAssetsInterface;
4 use Drupal\Core\Form\FormStateInterface;
7 * Implements hook_page_attachments_alter().
9 * Add the livereload.js script to the site.
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';
19 drupal_set_message(t('Livereload.js could not be included.'), 'warning');
25 * Build and prepare the path, we suggest the livereload script to be.
28 * The path, we suggest livereload script to be.
30 function livereload_get_path() {
31 $http_host = explode(':', $_SERVER['HTTP_HOST']);
32 return 'http://' . reset($http_host) . ':35729/livereload.js?snipver=1';
36 * Implements hook_css_alter().
38 * Force CSS to be added with link tags, rather than @import.
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;
49 * Implements hook_form_FORM_ID_alter().
51 * Add the possibility to en- and disable the livereload.js.
53 function livereload_form_system_performance_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
54 $form['livereload'] = array(
55 '#type' => 'fieldset',
56 '#title' => 'LiveReload',
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'),
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'),
71 // Add submit handler to save contact configuration.
72 $form['#submit'][] = 'livereload_form_system_performance_settings_submit';
76 * @see livereload_form_system_performance_settings_alter()
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'))
86 * Implements hook_library_info_alter().
88 function livereload_library_info_alter(&$libraries, $extension) {
89 if ($extension === 'livereload' && isset($libraries['drupal.livereload'])) {
90 $libraries['drupal.livereload']['js'][livereload_get_path()] = [];