我在 Drupal 模块中引用的民意调查内容类型是否错误?
我有以下模块,它影响用户如何到达两种不同的内容类型:“问题”和“民意调查”。问题是 CCK 内容类型,民意调查是默认的民意调查内容类型,但它非常适合问题,让用户访问,即
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 中提取它),并将它们用作超级注释(摘自本教程)。我在模块中引用 poll 是否错误?有什么建议吗?
<?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);
}
}
}
I have the following module, which affects how users arrive at two different content types, "question" and "poll". Question is a CCK content type, poll is the default poll content type, but it works perfectly for question, taking a user who goes to, i.e.,
http://sparechangenews.net/content/are-you-seeing-evidence-economic-recovery (which is a "question" content type)
to
http://www.sparechangenews.net/question/270 (with 270 being the NID)
but
http://sparechangenews.net/question/what-would-you-see-more-spare-change-news (which is a poll)
just goes to the normal url. I need that redirect because I have a view that checks the NID (pulling it from the URL) for nodes that references it, and uses them as sort of super comments (ripped from this tutorial). Am I referencing poll wrong in the module? Any tips?
<?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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的代码有什么问题,但您可能想检查的另一种选择是 http:// drupal.org/project/path_redirect 与 https://drupal.org/project/pathauto< 结合使用/a>.您可以为特定内容类型设置自动别名,还可以使用路径重定向来维护旧别名并重定向到新别名。
但是,我觉得您可能应该能够在没有任何重定向的情况下实现您想要做的事情。如果您只是想获取 nid 作为视图参数,为什么不检查“提供默认参数”下的“来自 URL 的节点 ID”,或者检查“PHP”并编写一些自定义代码,如上面所示:
无论如何,我建议通过将您需要的任何内容放入视图而不进行重定向来解决这个问题,因为这会降低复杂性(和开销)。
I'm not sure what is wrong with your code, but an alternative you might want to check out is http://drupal.org/project/path_redirect in conjunction with https://drupal.org/project/pathauto. You can set up automatic aliases for specific content types and also use path redirect to maintain old alias and redirect to new ones.
However, I feel like you probably should be able to achieve what you are trying to do without any redirection. If you are just trying to get the nid as a views argument, why not check under "Provide default argument" the "Node ID from URL", or alternatively check "PHP" and write some custom code like you have above:
Regardless, I would recommend tackling the issue with getting whatever you need into views without redirection as it will cut down on the complexity (and overhead).