The blog contains tips for drupal development using the Drupal content management system/framework.
Dominique De Cooman is a freelance drupal developer/consultant doing drupal development in the whole of Europe.
Feel free to contact me to hire me for your drupal development
hi dominique (suggestion: add
hi dominique
(suggestion: add the datetime to your article posts)
great article. was exactly what i was looking for. my issue was around being able to create drupal links on the server side, and pass them to my javascript. needed to be able to have ajax working, whether the webserver was rewriting urls or not.
so, you gave me the technique i needed. one small adjustment i would add, is to do a path check with a static var, to keep from inserting this javascript where you don't need it, or inserting it multiple times on a page.
<?phpfunction module_name_init() {
// Only add on specific pages
if (arg(1) === 'module_name' && arg(2) === 'mypath' ) {
static $complete = FALSE;
// Only need to do once per page.
if (!$complete) {
drupal_add_js(array(
array(
'module_name' => array(
'apikey' => variable_get('module_name_apikey', ''),
)
),
'setting'
);
$complete = TRUE;
}
}
?>
Good suggestion craig, you
Good suggestion craig, you should indeed add checks when using hook_init() because its run on every page load. Also note that hook_init is not called when aggresive caching is on.
Even better is to include the drupal_add_js only where you script needs it and not in hook init.
Btw I also use the same technique you say to construct paths for the ajax calls :)