Skip to content

Instantly share code, notes, and snippets.

@lincoln-chawora
Created February 26, 2020 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincoln-chawora/79e217e064498e0cfc73a7196b37205a to your computer and use it in GitHub Desktop.
Save lincoln-chawora/79e217e064498e0cfc73a7196b37205a to your computer and use it in GitHub Desktop.
How to pass php values into javascript (js) in drupal 7
function YOUR_THEME_some_preprocess_function(&$variables) {
$actual_value = array(
'my_text' => 'What ever data you wanna pass from php to js',
);
// Note, for this function to work $actual_value must have a key (my_text) and a value (what ever data...)
// the key will be used in javascript to get the value
drupal_add_js(array('nameUsedInJs' => $actual_value), 'setting');
}
(function ($) {
Drupal.behaviors.someBehaviourName = {
attach: function (context, settings) {
var someText = Drupal.settings.nameUsedInJs.my_text;
}
};
})(jQuery);
@lincoln-chawora
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment