[development] php and js in a programmatic block

jeff at ayendesigns.com jeff at ayendesigns.com
Sun Nov 28 17:17:30 UTC 2010


I'm close...but not quite there. I look at the page source, and the 
reference to my js file is there, as is a CDATA block listing all the 
settings contents (don't know if that -should- be there, but it's 
handy), which shows that what I passed in the settings array is there. I 
also have the block being inserted into the page.

What is not there is the content of the .js file.

I've tried the two drupal_add_js calls in both possible order. Here's 
what I have:

mymodule

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
       $block[$delta] = array(
         'info' => t('Mymodule Block'),
           'cache' => FALSE,
       );
       return $block;
       break;

     case 'view':
       $block['subject'] = "Mymodule Block";
       $block['content'] = _make_block();
       return $block;
       break;
   }
}

function _make_block() {
     if (isset($_SESSION['setmyblock'])) {
         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;
}

mymodule.js

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


More information about the development mailing list