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.

3 Answers 3

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
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Then my first code example should get you in the right direction
1

Try this:-

function MyModule_preprocess_node(&$vars) {
  if ($vars['type'] == 'this_type') {
    // do some stuff
  }
}

1 Comment

He wants to check within a module, not a theme template.php.
-2
Try this:-

$node = node_load(arg(1));

$node =$node->type;

if($node == 'node_type'){
    //do something
}

1 Comment

Please write some explanation to your Code! Just writing code without explaining what it does isn't appropriate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.