0

I want to pass params via Drupal to my JS file. I have no problem fetching them with the code below, the problem is that as the $(document).bind('leaflet.map' function executes FIRST, it is not able to find the user_name parameter

my_module.module

  function my_module_preprocess_html(&$variables) {

  $user_name=$GLOBALS['user']->name;
  drupal_add_js(array('my_module' => array('user_name' => $user_name)), array('type' => 'setting'));
  drupal_add_js(drupal_get_path('module', 'my_module') . '/js/external.js');
  }

external.js

(function($) {

Drupal.behaviors.fewo_map= {

 attach: function (context, settings) {

 alert('get params');
 //we can fetch the data
 user_name=settings.fewo_map.user_name;
}

}

$(document).bind('leaflet.map', function(e, map, lMap) {

//it fires first, so is unable to get user_name data
alert('leaflet')

console.info(user_name)
})

If I do...

(function($) {

 Drupal.behaviors.fewo_map= {

 attach: function (context, settings) {

  $(document).bind('leaflet.map', function(e, map, lMap) {
    alert('leaflet')

    console.info(user_name)
    })
  }
 }
})

the result is the same

Also tried using JS injector, no success neither. I need the user_name as parameter to define the settings inside leaflet 'function' Thanks

1 Answer 1

0

Difficult to separate your php from javascript, but if you set js variable with this php code it should execute first and can be used within leaflet js function later:

<?php render('<script>var username = "'.$GLOBALS['user'].'";</script>'); ?>

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.