drupal module, check if node type
Asked Answered
N

3

6

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.

Nasho answered 7/6, 2010 at 11:11 Comment(0)
F
8

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
   }
}
Fantoccini answered 7/6, 2010 at 13:1 Comment(1)
Then my first code example should get you in the right directionFantoccini
A
1

Try this:-

function MyModule_preprocess_node(&$vars) {
  if ($vars['type'] == 'this_type') {
    // do some stuff
  }
}
Airdrop answered 7/6, 2010 at 11:19 Comment(1)
He wants to check within a module, not a theme template.php.Fantoccini
X
-2
Try this:-

$node = node_load(arg(1));

$node =$node->type;

if($node == 'node_type'){
    //do something
}
Xylem answered 19/7, 2014 at 9:48 Comment(1)
Please write some explanation to your Code! Just writing code without explaining what it does isn't appropriateWreathe

© 2022 - 2024 — McMap. All rights reserved.