代码之家  ›  专栏  ›  技术社区  ›  Michael Morisy

我在Drupal模块中引用的内容类型是否错误?

  •  0
  • Michael Morisy  · 技术社区  · 16 年前

    http://sparechangenews.net/content/are-you-seeing-evidence-economic-recovery

    到

    http://www.sparechangenews.net/question/270 (以270为NID)

    但是

    http://sparechangenews.net/question/what-would-you-see-more-spare-change-news (投票表决)

    只是转到正常的网址。我需要这个重定向,因为我有一个视图可以检查NID(从URL中提取)中引用它的节点,并将它们用作某种超级注释( ripped from this tutorial ). 我在模块中引用的投票错误吗?有什么建议吗?

    <?php
    /**
     * @file
     * An example module to redirect the path from a node view for at
     * specific type to a view.
     */
    
    /**
     * Implementation of hook_init().
     */
    function example_init() {
      // Using arg() instead of menu_get_item(). Rumor has it menu_get_item
      // can occassionally cause WSOD.
      // We make sure arg(2) is empty so we do not redirect on edit or other
      // node sub pages.
      if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
        $node = node_load(arg(1));
        if ($node->type == 'answer') {
          drupal_goto('question/'. $node->field_answer[0]['nid']);
        }
        elseif ($node->type == 'poll' || $node->type == 'question') {
          drupal_goto('question/'. $node->nid);
        }
      }
    }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   bkildow    16 年前

    我不确定你的代码出了什么问题,但你可能想检查的另一个选择是 http://drupal.org/project/path_redirect 结合 https://drupal.org/project/pathauto

    不过,我觉得你应该能够实现你想做的事情没有任何重定向。如果您只是想将nid作为views参数,为什么不在“Provide default argument”下选中“Node ID from URL”,或者也可以选中“PHP”并编写一些像上面那样的自定义代码:

    $nid = arg(1);
    

    不管怎样,我建议您在不重定向的情况下将所需的内容导入到视图中来解决这个问题,因为这样可以降低复杂性(和开销)。