I can't find this addressed elsewhere, so I thought I'd have a stab at it...

I'm adding Javascript using drupal_add_js inline for a block in a module I'm making. In bluemarine/push button this Javascript loads and can execute just fine. In chameleon/marvin the Javascript isn't being loaded. I'm not sure if this is a drupal_get_js() problem or something else, but the themes that work are just printing $scripts while the ones that don't work rely on that function.

If I create a page in the site using the same code that creates the block, the page's Javascript loads and executes just fine in chameleon/marvin. It just seems to be the code defined in the module for the block. Maybe it's just a fluke, but I don't have the time to try and reproduce it with different code atm.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kkaefer’s picture

Version: x.y.z »
Component: theme system » 0
Status: Active » Fixed

First of all, this is not a problem with Drupal but with your very own code - at least you are altering themes.

The $scripts variable is calculated by phpTemplate before the actual template is loaded. That means, you can add as many JavaScript code in your theme - it won’t be reflected in this variable. To solve this problem, replace $scripts with drupal_get_js().

kkaefer’s picture

Version: » x.y.z
Component: 0 » javascript
Status: Fixed » Active

This is actually an issue with Drupal.

kkaefer’s picture

Assigned: Unassigned » kkaefer
Status: Active » Needs review
FileSize
1.63 KB

The problem was: JavaScript that is added in a block is not present in the head in plain PHP (non-phpTemplate) themes. The reason for this is that the blocks are fetched when they are to be inserted in the page, not before. But drupal_get_js() has already been called in the page header. Thus, the added JavaScript file (or other JavaScript code) is not in the header because it is added afterwards.

The patch moves the theme_blocks() call before the drupal_get_js() call to ensure that JavaScript that is added by blocks is added to the page header.

Dries’s picture

Status: Needs review » Needs work

Code looks good but I'd love to see this documented with a comment or two. It is tempted to move this calls back down -- a comment would prevent that.

kkaefer’s picture

Status: Needs work » Needs review
FileSize
1.7 KB

Added a description

kkaefer’s picture

FileSize
2.89 KB

Did the same thing in theme_page() as the issue also occured there.

kkaefer’s picture

Version: x.y.z » 5.x-dev
drumm’s picture

Status: Needs review » Fixed

Committed to HEAD.

Anonymous’s picture

Status: Fixed » Closed (fixed)