-1

What I am trying to accomplish with this is to have a URL for the API accessible via Drupal.settings so I can get to it from PhP and js with Drupal 7.41.

I have put this at the top of my custombootstrap.module file:

function yourmodule_init(){
    $apiurl="http://dev3.ws.mysite.com/api"
    drupal_add_js(array('yourmodule' => array('myurl' => $apiurl)), array('type' => 'setting'));
    variable_set('myurl',$apiurl);
}   

In a file called mapLayer.js I use the code like this:

var List_Users_Map = function(){
var userId = $('#uid-loged').val();
$('#list-users-gis-map').html('');
//linkedRequests(userId);   // RCD- CAll
$.ajax({
    url: Drupal.settings.yourmodule.myurl+'linked_users_gis/x-api-key/blahhhhhhhhhhTq4SWuCiLONePNfschNft',
    data:{uid:userId},
    type: 'GET',
    ...

Uncaught TypeError: Cannot read property 'myurl' of undefined can be seen here in chrome dev tools. It looks like it gets to 'yourmodule' but no further.

    http://imgur.com/cu4jtw6
  • are you using a drupal behavoir ? otherwise settings won't be populated – rémy Nov 19 '15 at 17:17
  • Per the docs you should not be using hook_init to add JS or CSS as it will not be fired on cached pages. Instead use your theme functions such as theme_preprocess_html(&$vars) here are the docs. – sareed Nov 19 '15 at 19:33
0

your init hook seems to be wrong, change the function name to custombootstrap_init, where the init hook is not the best choice for adding a js setting ..

in drupal it's good practice to use a behavoir, so wrap your js code like:

Drupal.behaviors.myModuleBehavoir = {
  attach: function (context, settings) {
    ..
  }
};

and then just use settings.yourmodule.myurl

  • when I change the function name to custombootstrap_init I get the same error, – Ryan Nov 19 '15 at 17:38
  • try a different hook and make sure it gets hit – rémy Nov 19 '15 at 21: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.