2010-06-07 34 views
6

作为更具体采取对这个问题:Drupal的模块,检查是否节点类型

drupal jQuery 1.4 on specific pages

如何检查,一个模块内,一个节点是否是某种类型才能够对节点做某些事情。

感谢

上下文:

我想,这样,而不是“my_page”工作它的工作节点类型,以适应这个代码。

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); 
    } 
    } 
} 

谢谢。

回答

7

里面一个模块,你可以这样做:

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 
    } 

} 

这将给出当前节点页面(如果不可用)加载一个节点对象。由于我不知道你正在使用的代码的上下文,这是一个粗略的例子,但是你可以通过执行node_load(node_id)来查看节点的属性。但是,根据Drupal API函数,它可能已经为您加载。

例如,hook_nodeapi。

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

你可以这样做:

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
    switch ($op) { 
     case 'view': 
     // some code here 
    } 
} 
+0

谢谢我用上下文更新了我的问题。 – Mark 2010-06-07 15:19:31

+0

然后我的第一个代码示例应该让你朝着正确的方向 – Kevin 2010-06-07 16:26:05

1

试试这个: -

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

他想要的模块,而不是一个主题的template.php内检查。 – Kevin 2010-06-07 13:01:57

+0

好点凯文。忽略我上面的答案! – drmonkeyninja 2010-06-07 14:14:13

+0

它也在模块内工作 – wranvaud 2015-07-27 19:49:53

-2
Try this:- 

$node = node_load(arg(1)); 

$node =$node->type; 

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

请给你的代码写一些解释!只是编写代码而不解释它的作用是不恰当的 – MiBrock 2014-07-19 09:54:08