【问题标题】:drupal module, check if node typedrupal模块,检查节点类型
【发布时间】:2010-06-07 11:11:06
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: drupal drupal-modules


    【解决方案1】:

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

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

    【讨论】:

    • 那么我的第一个代码示例应该让您朝着正确的方向前进
    【解决方案2】:

    试试这个:-

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

    【讨论】:

    • 他想在一个模块中检查,而不是一个主题template.php。
    【解决方案3】:
    Try this:-
    
    $node = node_load(arg(1));
    
    $node =$node->type;
    
    if($node == 'node_type'){
        //do something
    }
    

    【讨论】:

    • 请给你的代码写一些解释!只写代码而不解释它的作用是不合适的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多