Created
April 13, 2017 15:55
-
-
Save camoa/dba6280f3f1f1f8bd418f7ed9d5362e6 to your computer and use it in GitHub Desktop.
Passing settings to a javascript library
This file contains 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 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 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