drupal模块,检查节点类型

发布于 2024-09-04 22:51:52 字数 1293 浏览 8 评论 0原文

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

特定页面上的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);
    }
  }
}

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-09-11 22:51:52

在模块内部,您可以执行以下操作:

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

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
   }
}
一指流沙 2024-09-11 22:51:52

试试这个:-

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

Try this:-

function MyModule_preprocess_node(&$vars) {
  if ($vars['type'] == 'this_type') {
    // do some stuff
  }
}
隱形的亼 2024-09-11 22:51:52
Try this:-

$node = node_load(arg(1));

$node =$node->type;

if($node == 'node_type'){
    //do something
}
Try this:-

$node = node_load(arg(1));

$node =$node->type;

if($node == 'node_type'){
    //do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文