[development] php and js in a programmatic block

jeff at ayendesigns.com jeff at ayendesigns.com
Sun Nov 28 19:53:01 UTC 2010


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;
}

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

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

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. 
However, I'm still getting only

<script type="text/javascript" 
src="/sites/all/modules/_custom/mymodule/mymodule.js?1290973249"></script>

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.



On 11/28/2010 02:00 PM, Earl Miles wrote:
>
> The problem is likely order of code running. Settings won't have been
> read in yet. You need to use jquery's s ready() function. IMO you should
> put this directly into your test.js file and not embed inline javascript
> at all. If your javascript is going to do a .write() or something, you
> are best off using a<div>  that will be replaced. So at the bottom of
> your test.js, something like this:
>
> // $(function() { }) is a synonym for $.ready().
> // This ensures that the code is not executed until the page is rendered.
> $(function() {
>    var settings = Drupal.settings.mymodule;
>    myfunction(settings.name,settings.total);
> });
>
>    


More information about the development mailing list