I wrote a module that adds javascript and css to a page with nodes of type 'rower', now I need to add javascript and css to the page only if there is no tag with id == 1503 in any of the nodes. I tried to do it myself but it does not work because the variable $containsOutletTag == TRUE only at last node. Can you please tell me how I can do this? Here's what I have for now:

function countdown_preprocess_node(&$vars)
  if ($vars['type'] === 'rower') {
    $containsOutletTag = false;

    foreach ($vars['field_product_tags']['und'] as $tag) {
      if ($tag['tid'] == 1503) {
        $containsOutletTag = true;

    if (!$containsOutletTag) {
      drupal_add_css(drupal_get_path('module', 'countdown') . '/src/countdown.css');
      drupal_add_js(drupal_get_path('module', 'countdown') . '/src/countdown.js');
  • 1
    Why don't you just set your JS to ignore the node if it has that value?
    – Jaypan
    Aug 23, 2018 at 9:26
  • To add to what @Jaypan said, you should use the Drupal settings mechanism to pass values such as IDs from PHP to JavaScript, so as to keep your JS nice and clean. Aug 23, 2018 at 9:52
  • Remove $containsOutletTag = true; and place the drupal_add_... code there.
    – No Sssweat
    Aug 23, 2018 at 11:21
  • This does not work for me, because I need to add javascript, only if neither node has no tag value $tag['tid'] == 1503 Aug 23, 2018 at 12:12

1 Answer 1


you shouldn't have use hook_preprocess_node as it gets fired on each node separately, so if only one of the node doesn't have that specific tag the css and javascript will be added to the page unless other nodes have, so rather than hook_preprocess_node use hook_preprocess_page and there you can find an array of nodes (use dsm function with devel module enabled to see variable structure) and you can iterate over that array and check for condition

  • Thanks for the answer, I tried hook_preprocess_page, but it does'nt work for me Aug 23, 2018 at 12:12

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.