Skip to content

Instantly share code, notes, and snippets.

@camoa
Created April 13, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save camoa/dba6280f3f1f1f8bd418f7ed9d5362e6 to your computer and use it in GitHub Desktop.
Save camoa/dba6280f3f1f1f8bd418f7ed9d5362e6 to your computer and use it in GitHub Desktop.
Passing settings to a javascript library
(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);
// 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
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