Closed (fixed)
Project:
Drupal core
Version:
6.x-dev
Component:
javascript
Priority:
Critical
Category:
Bug report
Assigned:
Reporter:
Created:
21 Nov 2007 at 02:36 UTC
Updated:
14 Dec 2007 at 15:33 UTC
Jump to comment: Most recent file
Take the situation where no modules added javascript to your theme. So, $scripts has no value. Then, you want to add javascript to your theme via the template.php file. You do something like:
function phptemplate_preprocess_page(&$vars) {
drupal_add_js(path_to_theme() .'/myscript.js', 'theme');
$vars['scripts'] = drupal_get_js();
}
This should add your javascript to the page as well as drupal.js and jquery.js. But, it doesn't
The reason is that in template_proprocess_page $variables['scripts'] is set equal to drupal_get_js(). When drupal_get_js() is called it builds the $javascript array in drupal_add_js() so it isn't empty when your drupal_add_js() is run to add your theme javascript and therefore drupal.js and jquery.js are not added to it.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | drupal_add_js_194026_6.patch | 2.74 KB | mfer |
| #3 | drupal_add_js_194026_3.patch | 2.67 KB | mfer |
| #2 | drupal_add_js_194026_1.patch | 1.66 KB | mfer |
| #1 | drupal_add_js_194026_1.patch | 1.66 KB | mfer |
Comments
Comment #1
mfer commentedMy first pass at a patch for this. Only building the $javascript array if there is data to populate it.
Comment #2
mfer commentedMy first pass at a patch for this. Only building the $javascript array if there is data to populate it.
Comment #3
mfer commentedAnother version with a little less logic.
Comment #4
gábor hojtsyHrm, I looked at this code, and have a few questions / observations:
1. Why isn't the header scope properly initialized? Your first call to drupal_add_js() should trigger the
if (isset($data) && empty($javascript)) {line in drupal_add_js(). This is not changed with your patch either. You now doif (isset($data)) { if (empty($javascript)) {which should do the same. I don't understand this.2. This part could probably be reworked to use the
isset($scope)wrapper, and then doif (isset($javascript[$scope]))inside. But then this function is documented to return an array in all cases, so why do you return an empty string and not an empty array?3. Please use diff -up so we see which functions the changes reside in.
Comment #5
gábor hojtsyOK, no change in logic in the patch as I said, so I don't see this is critical or changes anything in the logic at all.
Comment #6
mfer commented@Gábor - I think this is critical because without a change drupal_add_js won't add jquery.js or drupal.js after template_preprocess_page [1]. This can cause problems for themes wanting to add javascript. Currently, in functions in template.php you would need to either manually add the javascript html to the $scripts (which won't go through javascript preprocessing) or you need to do a drupal_add_js for jquery.js and drupal.js.
This is because drupal_get_js is called in template_preprocess_page. drupal_get_js calls drupal_add_js with a scope but no data causing
to happen. This populates the $javascript array so that when drupal_add_js is called again
doesn't happen.
The attached patch matches the functionality of D5 to add drupal.js and jquery.js any time drupal_add_js is called with data. It, also, checks if a $javascript[$scope] is set and only returns data if it is. This removes some 'Undefined index' messages. $javascript[$scope] is only populated if there is data now where before it was always populated.
Responding to #4
Why isn't the header scope properly initialized? Your first call to drupal_add_js() should trigger the if (isset($data) && empty($javascript)) { line in drupal_add_js(). This is not changed with your patch either. You now do if (isset($data)) { if (empty($javascript)) { which should do the same. I don't understand this.
You are right. What I did ended up removing one isset($data) call. It went from:
to
This part could probably be reworked to use the isset($scope) wrapper, and then do if (isset($javascript[$scope])) inside. But then this function is documented to return an array in all cases, so why do you return an empty string and not an empty array?
Included both of these in this patch.
[1] http://drupal.org/files/theme_flow_6_0.pdf
Comment #7
gábor hojtsyComment #8
dvessel commentedThis is a confusing bug and if left as is would be bad for themes.
mfer is right. Due to the way drupal_add_js get's called without the $data param from drupal_get_js causes the static $javascript to wipe killing jquery and drupal.js.
Blocking them within a "isset($data)" works.
Comment #9
gábor hojtsyWell, I did not see the flow, so thanks for clarifying and reviewing. Also the latest patch fixes my other concerns, so thanks, committed.
Comment #10
(not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.