Skip to content
Snippets Groups Projects
Commit 01be4d7b authored by Loredan Szocs's avatar Loredan Szocs Committed by Joao Ventura
Browse files

Issue #3197734: Configure pages where klaro is not enabled

parent 1be346a1
Branches
Tags 1.3.0
No related merge requests found
......@@ -2,3 +2,4 @@ config: "{\n \"elementID\":\"klaro\",\n \"storageMethod\":\"localStorage\",\
preferences: 'Cookie preferences'
library: klaro
enabled: true
exclude_paths: ''
\ No newline at end of file
......@@ -14,3 +14,6 @@ simple_klaro:
enabled:
type: boolean
label: 'Enable Simple Klaro'
exclude_paths:
type: string
label: 'Exclude paths'
......@@ -36,3 +36,13 @@ function simple_klaro_update_8002() {
$config->set('enabled', TRUE);
$config->save();
}
/**
* Update simple klaro settings with new exclude_paths option.
*/
function simple_klaro_update_8101() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('simple_klaro.settings');
$config->set('exclude_paths', '');
$config->save();
}
......@@ -14,13 +14,27 @@ function simple_klaro_page_attachments(array &$page) {
if (\Drupal::currentUser()->hasPermission('bypass simple klaro')) {
return FALSE;
}
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config */
$config = \Drupal::config('simple_klaro.settings');
$klaro_enabled = $config->get('enabled');
$klaro_config = Json::decode($config->get('config'));
$klaro_library = $config->get('library');
$excluded_paths = $config->get('exclude_paths');
$path_match = FALSE;
if ($klaro_enabled && !empty($klaro_config) && !empty($klaro_library)) {
$page['#attached']['drupalSettings']['klaroConfig'] = $klaro_config;
$page['#attached']['library'][] = "simple_klaro/$klaro_library";
if (!empty($excluded_paths)) {
// Check both the URL path and the URL alias against the list to exclude.
$path = \Drupal::service('path.current')->getPath();
$url_alias_path = \Drupal::service('path_alias.manager')->getAliasByPath($path);
$path_match = \Drupal::service('path.matcher')->matchPath($path, $excluded_paths);
$path_match_url_alias = \Drupal::service('path.matcher')->matchPath($url_alias_path, $excluded_paths);
$path_match = $path_match || $path_match_url_alias;
}
if (!$path_match) {
$page['#attached']['drupalSettings']['klaroConfig'] = $klaro_config;
$page['#attached']['library'][] = "simple_klaro/$klaro_library";
}
}
}
......@@ -83,6 +83,17 @@ class SettingsForm extends ConfigFormBase {
'#required' => TRUE,
];
$form['exclude_paths'] = [
'#type' => 'textarea',
'#title' => $this->t('Exclude paths'),
'#default_value' => !empty($config->get('exclude_paths')) ? $config->get('exclude_paths') : '',
'#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
'%blog' => '/blog',
'%blog-wildcard' => '/blog/*',
'%front' => '<front>',
]),
];
$form['#attached']['library'][] = 'simple_klaro/klaro_editor';
return parent::buildForm($form, $form_state);
......@@ -107,6 +118,7 @@ class SettingsForm extends ConfigFormBase {
->set('preferences', $form_state->getValue('preferences'))
->set('library', $form_state->getValue('library'))
->set('enabled', $form_state->getValue('enabled'))
->set('exclude_paths', $form_state->getValue('exclude_paths'))
->save();
parent::submitForm($form, $form_state);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment