[development] php and js in a programmatic block

Larry Garfield larry at garfieldtech.com
Sun Nov 28 21:22:11 UTC 2010

On Sunday, November 28, 2010 1:53:01 pm jeff at ayendesigns.com wrote:
> Makes sense (timing...the 4th dimension how it all is non-intuitive).
> So, my Drupal function is now
> function _make_block() {
>      if (isset($_SESSION['test'])) {
>          drupal_set_message('setting');
>          $settings = array(
>              'mymodule' => array(
>                  'name' => $_SESSION['mymodule_name'],
>                  'total' => $_SESSION['mymodule_total']
>               )
>            );
>            drupal_add_js($settings, 'setting');
>          drupal_add_js(drupal_get_path('module', 'mymodule)
> .'/mymodule.js', 'module', 'header', FALSE, FALSE);
>          $block = 'test';
>      }
>      return $block;
> }

This part looks correct, I think.  Basically, I consider the "inline" property 
of drupal_add_js() to be almost a bug, not a feature.  Don't use it if you can 
possibly avoid it.

> Not sure whether the second drupal_add_js call should be of type module
> or inline at this point. The contents of my js file are

'module' is correct, because you're adding a .js file from a module.

> //<script type="text/javascript"
> src="http://www.mydomain.com/mystuff.js"></script>
> $(function() {
>    var settings = Drupal.settings.mymodule;
>    myfunction(settings.name,settings.total);
> });

You don't want that first line at all.  That's only if you're putting it in 
the HTML directly.  If it's just in a JS file you do not want it.

> I commented out the reference to the other js file within my .js file
> for now. I guess I'll need to handle it with another drupal_add_js call.

Correct.  <script> tags don't work within JS files.  They're an HTML thing.

> However, I'm still getting only
> <script type="text/javascript"
> src="/sites/all/modules/_custom/mymodule/mymodule.js?1290973249"></script>

That's all drupal_add_js() will give you in the page itself.  That's good.

> but nothing that's in it appears on the page. I simply get the divs for
> the test block and the word 'test' as its content.

Sounds like it's time to fire up Firebug and see where the Javascript is 

--Larry Garfield

More information about the development mailing list