Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Is there a definitive, universal way to do this? For example using an 'environment' variable that comes with Drupal.behaviours?

Have done some research and there doesn't seem to be a common answer.

share|improve this question
No there's no environment variable like that...what exactly do you mean by 'get the drupal node id'? What are you classing as 'the drupal node id' in this context? The ID of a node whose full page you're on? – Clive Jul 8 at 12:26
yes, the ID of a node whose full page I'm on, e.g. as in mysite.com/node/6 I would like the jquery I execute from this page to be able to obtain the node id and put it into a variable, in the example, this would have the value of 6. – therobyouknow Jul 8 at 12:29
If you want a robust method you'll have to pass the node ID from the server side manually using drupal_add_js(); the nid isn't ever used on the client-side by core so there's no need for it to be added as a setting usually – Clive Jul 8 at 12:32
add comment (requires an account with 50 reputation)

2 Answers

up vote 2 down vote accepted

As mentioned above, Drupal core has no use for the node ID on the client side so it doesn't pass it through. if you want access to it you'll need to add it manually using drupal_add_js(), e.g.

function MYMODULE_node_view($node, $view_mode, $langcode) {
  if ($view_mode == 'full') {
    $setting = array('MYMODULE' => array('currentNid' => $node->nid));
    drupal_add_js($setting, 'setting');
  }
} 

Then on the client side you'll have access to it through Drupal.settings:

var currentNid = Drupal.settings.MYMODULE.currentNid;
share|improve this answer
Thanks @Clive - very happy with this solution! I will try it out. It will help me with my attempt to fix pop_links module issue: Version 7 Not Recording Links. I will credit you with the help, feel free to chip in too. My findings so far with this module are that the javascript they provide is not "firing" and a colleague has provided an interim fix based on a class attribute from the bootstrap theme. Ultimately we are aiming towards a theme independent solution. – therobyouknow Jul 8 at 12:40
May I mention a working correction to your code: it should read: $setting = array('MYMODULE' => array('currentNid' => $node->nid)); the = is extraneous. That did the job - tacked your code onto this function that already existed in pop_links and added your javascript in pop_links.js. – therobyouknow Jul 8 at 13:27
Oops, typo - sorry about that :) – Clive Jul 8 at 13:41
1  
+1 by the way for your answer. It works and has enabled me to provide an interim fix for pop_links issue here: drupal.org/node/1269290#comment-7625019 – therobyouknow Jul 8 at 13:48
1  
P.S. credited you on the solution. – therobyouknow Jul 8 at 14:14
show 1 more commentadd comment (requires an account with 50 reputation)

The default template_preprocess_html() has this bit of code in in

if ($suggestions = theme_get_suggestions(arg(), 'page', '-')) {
  foreach ($suggestions as $suggestion) {
    if ($suggestion != 'page-front') {
      // Add current suggestion to page classes to make it possible to theme
      // the page depending on the current page type (e.g. node, admin, user,
      // etc.) as well as more specific data like node-12 or node-edit.
      $variables['classes_array'][] = drupal_html_class($suggestion);
    }
  }
}

This will stick on classes to the <body> element line page-node-123. If you don't want to use your own code in a custom module, you can get the classes via jQuery, find the one that matches page-node-, and then parse out the nid.

share|improve this answer
+1 Thanks for your input @MPD worth knowing. In my case adding stuff to a module suits me as I am fixing a bug in pop_links module - see my comment below Clive's answer. Your answer will, no doubt, be useful in other scenarios. – therobyouknow Jul 8 at 13:28
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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