View source
<?php
use Drupal\Core\Form\FormStateInterface;
define('BROWSERSYNC_DEFAULT_HOST', 'HOST');
define('BROWSERSYNC_DEFAULT_PORT', '3000');
function browsersync_theme($existing, $type, $theme, $path) {
return [
'browsersync_snippet' => [
'variables' => [
'host' => BROWSERSYNC_DEFAULT_HOST,
'port' => BROWSERSYNC_DEFAULT_PORT,
],
],
];
}
function browsersync_css_alter(&$css) {
$system_css_preprocess = \Drupal::config('system.performance')
->get('css.preprocess');
if (browsersync_get_setting('enabled') && !$system_css_preprocess) {
foreach ($css as $key => $value) {
if (strpos($value['data'], 'core/') !== 0) {
$css[$key]['preprocess'] = FALSE;
}
}
}
}
function browsersync_form_system_theme_settings_alter(&$form, FormStateInterface $form_state) {
$args = $form_state
->getBuildInfo()['args'];
$theme_key = !empty($args[0]) ? $args[0] : NULL;
$form['browsersync'] = [
'#type' => 'details',
'#title' => 'Browsersync settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['browsersync']['browsersync_enabled'] = [
'#title' => 'Enable Browsersync',
'#type' => 'checkbox',
'#default_value' => browsersync_get_setting('enabled', $theme_key),
];
$form['browsersync']['settings'] = [
'#type' => 'container',
'#states' => [
'visible' => [
'input[name="browsersync_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$form['browsersync']['settings']['browsersync_host'] = [
'#title' => 'Host',
'#type' => 'textfield',
'#description' => t('Override host detection if you know the correct IP to use.'),
'#default_value' => browsersync_get_setting('host', $theme_key),
];
$form['browsersync']['settings']['browsersync_port'] = [
'#title' => 'Port',
'#type' => 'textfield',
'#description' => t('Use a specific port (instead of the one auto-detected by Browsersync).'),
'#default_value' => browsersync_get_setting('port', $theme_key),
];
$form['#submit'][] = 'browsersync_theme_settings_form_submit';
}
function browsersync_theme_settings_form_submit($form, FormStateInterface $form_state) {
$args = $form_state
->getBuildInfo()['args'];
$theme_key = !empty($args[0]) ? $args[0] : NULL;
if ($theme_key) {
$config_key = $theme_key . '.settings';
}
else {
$config_key = 'system.theme.global';
}
$user_input = $form_state
->getUserInput();
\Drupal::configFactory()
->getEditable($config_key)
->set('third_party_settings.browsersync.enabled', $user_input['browsersync_enabled'])
->set('third_party_settings.browsersync.host', $user_input['browsersync_host'])
->set('third_party_settings.browsersync.port', $user_input['browsersync_port'])
->save();
}
function browsersync_page_bottom(array &$page_bottom) {
if (browsersync_get_setting('enabled') && \Drupal::currentUser()
->hasPermission('use browsersync')) {
$page_bottom['browsersync'] = [
'#theme' => 'browsersync_snippet',
'#weight' => 100,
];
foreach ([
'host',
'port',
] as $setting) {
if ($value = browsersync_get_setting($setting)) {
$page_bottom['browsersync']['#' . $setting] = $value;
}
}
}
}
function browsersync_get_setting($setting_name, $theme = NULL) {
$cache =& drupal_static('theme_get_setting', []);
if (!isset($theme)) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
}
$setting_name = 'third_party_settings.browsersync.' . $setting_name;
if (empty($cache[$theme])) {
$setting = theme_get_setting($setting_name, $theme);
}
else {
$setting = $cache[$theme]
->get($setting_name);
}
return $setting;
}