1

I am working with JSON to theme Google Maps in the api v3.

I have my custom JSON script as an external JS file, and I am currently referencing it in html.tpl.php after the other scripts are loaded. This works great, and maps are themed, but on any pages that don't have a GMAP on them, i get the following error in Dev Tools...

"Uncaught TypeError: Cannot call method 'addHandler' of undefined"

So I am trying to add the JS file to just the pages that have a GMAP, in template.php I use the following under the MY_THEME_preprocess_page function.

if (isset($vars['node']) && $vars['node']->type == 'venue') { drupal_add_js(drupal_get_path('theme', 'MY_THEME') . '/js/mapstyle.js'); }

This loads the JS for all pages that are of the content type "Venue". The problem I am having is that the JS file is loading BEFORE the gmap scripts, and the style is not being rendered.

How do i get a JS file to load after all of the other scripts? I also need to load the JS file on a views page mysite.com/venues , but as a views page doesnt have a "node/id" alias, I am struggling to figure out how to only load the JS on that page using template.php

4
  • 1
    Adding js in the tpl.php is a recipe for disaster. Read this. Then make your code a behavior, that's a must have. Include them in info file, or turn them into a library (or two, with dependency between them). Then we'll have a chance o actually think about making it work. But first thing first, make it a behavior and show us how it looks now (may and even should be a trimmed version, we don't need to know your magic ;) only parts relevant to the issue).
    – Mołot
    Dec 2, 2013 at 14:16
  • Hi Molot, what is the big No No with adding drupal_add_js to a template file? On that page you sent me, it looks like adding an array with weight associated with it would solve my problem?
    – Collins
    Dec 2, 2013 at 14:32
  • 1
    Well if you add js in tpl file, you can't know if it will be added to a page, as templates are heavily cached. So it's a bit of lottery what gets stored into cache, which list of js will really get used. Only logic you can safely put inside tpl is "if row is odd, add a class" and similar stuff. Everything even a tad more advanced about display logic, should be done in template.php. For the js themselves, most supported method is to use '[#attached'] in renderable array or info files. drupal_add_js is only a "fallback" for situations where you can't use any of these safer ways.
    – Mołot
    Dec 2, 2013 at 14:40
  • Ah ok, yeah that makes sense. Thank you for explaining it to me!
    – Collins
    Dec 2, 2013 at 14:49

1 Answer 1

2

Using the following in my template.php file for my theme, I achieved what I wanted to do.

function MY_THEME_preprocess_page(&$vars) {
  if (isset($vars['node']) && $vars['node']->type == 'venue') {
    drupal_add_js(drupal_get_path('theme', 'MY_THEME') . '/js/mapstyle.js',
    array('weight' => 15));
  }
}

I couldn't add the JS to my .info file as it would add to all pages, so I used the drupal_add_js fallback in template.php, and the now the error no longer shows on a page that doesn't have a GMAP on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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