By jbhangoo on
I call my block module 'meditation' (sorry if I don't use the right terms but I hope my meaning is clear).
Right before ending the hook_block I wrote some code to include Javascript. I got from an online example:
drupal_add_js(drupal_get_path('module', 'meditation') .'/test.js');
$setting = array( 'value' => t('s') );
drupal_add_js($setting, 'setting');
test.js:
if(Drupal.settings.value)
alert(Drupal.settings.value);
else
alert ('nothing');
It pops up the second alert so the file does include ok. But the setting does not work.
I tried a few variations of the $setting syntax and array structure. I also tried to dump out Drupal.settings but it is totally empty. Is there an option needing to be set that the examples didn't make obvious?
I checked many forums and online examples also but this was the simplest, so sorry if there is an obvious answer. Thank you for any helpful information.
Comments
You should namespace your
You should namespace your settings:
This should give you the value you want at Drupal.settings.mediation.value
Contact me to contract me for D7 -> D10/11 migrations.
Didn't help
You might have guessed this would solve it as I did too. I actually had it that way originally.
I changed to the example above, thinking it would simplify the problem. But Drupal.settings is completely empty either way.
Look at the page source. A
Look at the page source. A couple of lines before the closing </head> tag, you should see something that looks something like this:
In there, try to find the reference to your settings. That way you can track whether or not they are properly being added to the page.
Edit: also, read nevets post below - maybe you are executing your code right away when the script is loaded, which will be before the settings have loaded.
Contact me to contract me for D7 -> D10/11 migrations.
Try implementing your code as
Try implementing your code as a behavior
This will insure the settings are "loaded" by the time your code runs.
behavior didn't work, but jQuery.extend did
This is a great suggestion to improve the quality of the coding. I actually did have it written this way in an earlier test, but the Drupal.settings object was still empty.
About jQuery.extend:
I did find the line. Thank you for that insight! I did not even know about that.
That line is very long on my site since I have installed other modules I plan to use later, but it is there.
If I copy it into test.js, now I see the setting works! But stranger than that, even after I undo copying in that line, it still works! Note that all caching and optimization has been disabled for testing purpose.
I saw that I must de-reference the value using Drupal.settings[0].meditation
So, I don't know why, but it is working now. Thanks to both of you for your time and thought.
You can't just copy the
You can't just copy the Drupal settings array into your JS file, that array can be different on every page load.
The following should work fine. If it's not we need to find the real issue rather than resorting to copying the whole settings structure.
In your module's included JS:
Both of those snippets were provided above, but I noticed a naming discrepancy that may have been your problem. In some places 'mediation' was used instead of 'meditation' (note the extra 't' needed).
typo error from commenter
The typo was given by the person who commented, suggesting a different approach. It is not in my code.
I see your point: if I try to access any members of Drupal.settings I get "undefined".
Show us your full js code.
Show us your full js code.
Contact me to contract me for D7 -> D10/11 migrations.
Sample drupal_add_js settings test module
This is a test module to simplify the problem. The javascript references the settings perfectly through Drupal.settings[0] but fails on just Drupal.settings. But if I understand right, the array index may change later, so this is not the right solution.
I hope my jQuery is not the problem. I have installed jQuery Update and check the Status Report.
Thanks for any help this might show.
You've got one too many
You've got one too many arrays in there. It should be:
You had:
Contact me to contract me for D7 -> D10/11 migrations.
Yes! Too many test cases.
That was it! I had done so many variations of this value that I completely lost track of it.
I will add the features from the other full module to this working skeleton.
Thank you for the many helpful reviewers! Very helpful to get multiple insights.