Skip to content
Snippets Groups Projects
Commit d6fb235b authored by Bryan Sharpe's avatar Bryan Sharpe
Browse files

Issue #3341662 by b_sharpe: Add options to manually add libs

parent 7e67804d
No related branches found
No related tags found
1 merge request!1Issue #3341662: Add options to manually add libs.
include_gsap: false
include_libs: false
libs: { }
......@@ -3,4 +3,3 @@ type: module
description: "This module integrates Greensock GSAP into Drupal"
package: User interface
core_version_requirement: ^9.2 || ^10
......@@ -26,12 +26,20 @@ function gsap_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_page_attachments().
*/
function gsap_page_attachments(array &$attachments) {
$settings = \Drupal::config('gsap.settings');
$attachments['#cache']['tags'][] = 'config:gsap.settings';
// Add main lib.
$attachments['#attached']['library'][] = 'gsap/gsap';
if ($settings->get('include_gsap')) {
$attachments['#attached']['library'][] = 'gsap/gsap';
// Optional plugins.
$libraries = \Drupal::config('gsap.settings')->get('libs');
foreach ($libraries as $lib) {
$attachments['#attached']['library'][] = 'gsap/' . $lib;
// Add defined optional plugins.
if ($settings->get('include_libs')) {
$libraries = \Drupal::config('gsap.settings')->get('libs');
foreach ($libraries as $lib) {
$attachments['#attached']['library'][] = 'gsap/' . $lib;
}
}
}
}
......@@ -33,11 +33,27 @@ class Admin extends ConfigFormBase {
$config = $this->config('gsap.settings');
$form = parent::buildForm($form, $form_state);
$form['include_gsap'] = [
'#title' => $this->t('Include GSAP on every page?'),
'#type' => 'checkbox',
'#default_value' => $config->get('include_gsap') ?? FALSE,
];
$form['include_libs'] = [
'#title' => $this->t('Include additional libs on every page?'),
'#type' => 'checkbox',
'#default_value' => $config->get('include_libs') ?? FALSE,
'#states' => [
'disabled' => [
':input[name="include_gsap"]' => ['checked' => FALSE],
],
],
];
$form['libs'] = [
'#title' => $this->t('Libraries to Include.'),
'#title' => $this->t('Available CDN Libraries'),
'#description' => $this->t('Select the plugins to include as libraries. These will be added to every page.'),
'#type' => 'checkboxes',
'#required' => TRUE,
'#default_value' => $config->get('libs') ?? [],
'#options' => [
'flip' => $this->t('Flip'),
......@@ -50,6 +66,14 @@ class Admin extends ConfigFormBase {
'pixi' => $this->t('Pixi'),
'text' => $this->t('Text'),
],
'#states' => [
'invisible' => [
':input[name="include_libs"]' => ['enabled' => FALSE],
],
'disabled' => [
':input[name="include_libs"]' => ['checked' => FALSE],
],
],
];
return $form;
......@@ -62,6 +86,8 @@ class Admin extends ConfigFormBase {
$libraries = array_values(array_filter($form_state->getValue('libs')));
$this->configFactory->getEditable('gsap.settings')
->set('libs', $libraries)
->set('include_gsap', $form_state->getValue('include_gsap'))
->set('include_libs', $form_state->getValue('include_libs'))
->save();
parent::submitForm($form, $form_state);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment