-
-
Save visabhishek/e9f4575bd76158ff65d86e61d4fa72fb to your computer and use it in GitHub Desktop.
Passing settings to a javascript library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, Drupal, drupalSettings) { | |
Drupal.behaviors.LotusBehavior = { | |
attach: function (context, settings) { | |
// can access setting from 'drupalSettings'; | |
var lotusHeight = drupalSettings.lotus.lotusJS.lotus_height; | |
$('lotusElement').css('height', lotusHeight); | |
} | |
}; | |
})(jQuery, Drupal, drupalSettings); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the lotus.services.yml file you have to declare a dependency to drupalSettings | |
lotus-js: | |
version: 1.x | |
js: | |
js/lotus.js: {} | |
dependencies: | |
- core/jquery | |
core/drupalSettings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function lotus_preprocess_html(&$variables) { | |
$lotus_height = ‘300px’; | |
//Add a JS library | |
$variables['#attached']['library'][] = 'lotus/lotus-js'; | |
$variables['#attached']['drupalSettings']['lotus']['lotusJS']['lotus_height'] = $lotus_height; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment