I've just started my way on Drupal Module development and I have already run across a situation.

I have these two functions:

Note: "mymodule" is a generic name for this post.

/**
 * Implementatin of hook_theme
 */
function mymodule_theme() {
  return array(
    'mymodule_notes' => array(
      'template' => 'mymodule_notes',
      'arguments' => array(
        'title' => NULL,
        'content' => NULL
      )
    )
  );
}

In the above function, as you can see, I've passed 'template' for using 'mymodule_notes.tpl.php' in order to make it more flexible for designers. But using it, the below function doesn't show the javascript file.

And if I comment the line:

'template' => 'mymodule_notes',

Drupal adds the javascript file!
I don't know how to fix it, but I believe there is a way to do that!

/**
 * Theme function for theming notes
 *
 * @param $title
 *  The title of the note
 * @param $content
 *  The text for the note
 * @return
 *  An HTML themed string.   
 */
 function theme_mymodule_notes($title, $content) {
  $module_path = drupal_get_path('module', 'mymodule');
  drupal_add_css($module_path . '/mymodule.css');
  drupal_add_js($module_path . '/mymodule.js'); 
  $output = sprintf('
    <div id="mymodule-title">%s</div>
    <div id="mymodule-content">%s</div>',
    t($title),
    t($content)
  );
  return $output;
 }

Well, hope someone could help me.

Thanks in advance!

Comments

nevets’s picture

How are you invoking the template?

gilbertoalbino’s picture

I created a file "mymodule_notes.tpl.php" and this file is invoked inside:

function mymodule_theme() {
  return array(
    'mymodule_notes' => array(
      // HERE I INVOKE THE TEMPLATE
      'template' => 'mymodule_notes',
        'arguments' => array(
        'title' => NULL,
        'content' => NULL
      )
    )
  );
}
nevets’s picture

That does not invoke the template, it registers it. At some point you need code like

print  theme('mymodule_notes', $title, $content);

Depending on context you might set a variable or return the value instead of calling print.

gilbertoalbino’s picture

Thank you nevets, after this note of yours I figured out the solution, I guess.

The theme function is being called inside the mymodule_block function,
and I moved the drupal_add_js() from theme_mymodule_notes to this function and I got it working!

If it is correct this topic is solved!