2

I'm looking for a way for my module to evaluate the content type in a preprocess hook and conditionally add some javascript.

Theme_preprocess_page() makes the content type accessible plus some other useful things (I could differentiate between nodes & views & such), but I can't use it because it is likely to have already been declared by the theme.

*My_module*_preprocess_page() doesn't appear to provide the content type anywhere.

What is a good option here?

1 Answer 1

4

Here's how to add content type specific javascript to a node using hook_node_view and #attached

/**
 * Implements hook_node_view().
 */
function MYMODULE_node_view($node, $view_mode, $langcode) {

  // Add content type specific JS on full page node view
  if($view_mode == 'full'){
    switch($node->type){
      case 'article':
        $node->content['#attached']['js'][] = array('data' => drupal_get_path('theme', 'MYTHEME') .'/js/article.js');
      break;
      case 'page':
        $node->content['#attached']['js'][] = array('data' => drupal_get_path('theme', 'MYTHEME') .'/js/page.js');    
      break;
    }
  }

}
1
  • cool. I'm not familiar w/ hook_node_view; I'll look it up tomorrow.
    – doub1ejack
    Commented Dec 11, 2013 at 22:42

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.