2

I am working on a theme for a client, and have run into an instance where I would like to add to Drupal.settings via drupal_add_js() from the theme.

In this particular case, it is really most appropriate to do it from the theme itself and not a module hook, as the setting is related to the theme itself.

Doing it from template_preprocess(), or one of its relatives, is too late because $vars["scripts"] has already been built. I suppose I could do something like the following code, but this doesn't seem right.

$settings = array(
  "bar" => "bar",
);

drupal_add_js(array("foo" => $settings), "setting");

$vars["scripts"] = drupal_get_js();

Is doing it from hook_theme() really the only option, or is the above approach kosher?

4

I have seen that pattern multiple times before, and it seems to be a standard practice.

1

Just for the record I had a similar problem today and solved it using hook_preprocess_hook() to intercept my theme function, no messy $vars['scripts'] exists yet

  • 2
    Looking back, I am not sure why my question got tagged for Drupal 7. It was actually about Drupal 6, where the options are more limited. Your solution is a good one for Drupal 7. – mpdonadio Jan 10 '12 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.