代码之家  ›  专栏  ›  技术社区  ›  Mark

drupal模块,检查节点类型

  •  6
  • Mark  · 技术社区  · 14 年前

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

    drupal jQuery 1.4 on specific pages

    谢谢

    背景:

    我正在尝试修改这段代码,这样它就可以在节点类型上工作,而不是在“我的页面”上工作。

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

    谢谢。

    3 回复  |  直到 7 年前
        1
  •  8
  •   rebello95    10 年前

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

    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\u load(node\u id)查看节点的属性。但是,根据drupalapi函数的不同,它可能已经为您加载了。

    例如,hook\u 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
       }
    }
    
        2
  •  1
  •   drmonkeyninja    14 年前

    尝试this:-

    function MyModule_preprocess_node(&$vars) {
      if ($vars['type'] == 'this_type') {
        // do some stuff
      }
    }
    
        3
  •  -2
  •   Mohit Savaliya    10 年前
    Try this:-
    
    $node = node_load(arg(1));
    
    $node =$node->type;
    
    if($node == 'node_type'){
        //do something
    }