drupal模块,检查节点类型

Mar*_*ark 6 drupal drupal-modules

作为对这个问题的更具体的看法:

特定页面上的drupal jQuery 1.4

如何在模块内检查节点是否是某种类型,以便能够对节点执行某些操作.

谢谢

上下文:

我正在尝试调整此代码,以便它不是在"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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Kev*_*vin 8

在模块内部,您可以这样做:

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
   }

}
Run Code Online (Sandbox Code Playgroud)

这将加载给定当前节点页面的节点对象(如果不可用).由于我不知道您正在使用的代码的上下文,这是一个粗略的示例,但您始终可以通过执行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
   }
}
Run Code Online (Sandbox Code Playgroud)