Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drupal module, check if node type

As a more specific take on this question:

drupal jQuery 1.4 on specific pages

How do I check, inside a module, whether or not a node is a certain type to be able to do certain things to the node.

Thanks

The context:

I'm trying to adapt this code so that rather than working on 'my_page' it works on a node type.

function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {

  // I needed a one hit wonder. Can be altered to use function arguments
  // to increase it's flexibility.
  if(arg($delta) == $arg) {
    $scripts = drupal_add_js();
    $css = drupal_add_css();
    // Only do this for pages that have JavaScript on them.
    if (!empty($variables['scripts'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($scripts['module'][$path . '/admin_menu.js']);
      $variables['scripts'] = drupal_get_js('header', $scripts);
    }
    // Similar process for CSS but there are 2 Css realted variables.
    //  $variables['css'] and $variables['styles'] are both used.
    if (!empty($variables['css'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($css['all']['module'][$path . '/admin_menu.css']);
      unset($css['all']['module'][$path . '/admin_menu.color.css']);
      $variables['styles'] = drupal_get_css($css);
    }
  }
}

Thanks.

like image 839
Mark Avatar asked Jun 07 '10 11:06

Mark


2 Answers

Inside of a module, you can do this:

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
   if (!($node)) {
      $node = node_load(arg(1));
   }

   if ($node->type == 'page') {
     // some code here
   }

}

That will load a node object given the current node page (if not available). Since I don't know the context of code you are working with, this is kind of a rough example, but you can always see properties of a node by doing node_load(node_id). But, depending on the Drupal API function, it may already be loaded for you.

For example, hook_nodeapi.

http://api.drupal.org/api/function/hook_nodeapi

You could do:

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   switch ($op) {
      case 'view': 
         // some code here
   }
}
like image 198
Kevin Avatar answered Sep 20 '22 17:09

Kevin


Try this:-

function MyModule_preprocess_node(&$vars) {
  if ($vars['type'] == 'this_type') {
    // do some stuff
  }
}
like image 35
drmonkeyninja Avatar answered Sep 21 '22 17:09

drmonkeyninja