Created
February 26, 2020 11:22
-
-
Save lincoln-chawora/79e217e064498e0cfc73a7196b37205a to your computer and use it in GitHub Desktop.
How to pass php values into javascript (js) in drupal 7
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 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'); | |
} |
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.behaviors.someBehaviourName = { | |
attach: function (context, settings) { | |
var someText = Drupal.settings.nameUsedInJs.my_text; | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some official documentation for more details: https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_js/7.x