2010-06-07 78 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