Drupal:如何以编程方式为节点保存已具有别名的节点创建 URL 别名? [英] Drupal: How do I programatically create a URL alias for a node that already has an alias on node save?

查看:19
本文介绍了Drupal:如何以编程方式为节点保存已具有别名的节点创建 URL 别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模块,它实现了钩子 nodeapi,以便在创建或更新节点时执行一些代码.

I have a custom module that implements hook nodeapi to execute some code when the node is created or updated.

基本上我想根据节点保存或更新时自动生成的别名创建别名.

Basically I want to create an alias based off of the automatically generated alias on node save or update.

现在我正在使用对 path_set_alias 的调用,我只想对特定类型的内容产品"执行此操作.

Right now I'm using a call to path_set_alias and I only want to do this with a specific type of content, "product".

这是让我开始使用 nodeapi 的调用

Here is my nodeapi call to get me started

function product_url_helper_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

 if($node->type == 'product'){
  switch($op){

     case 'insert':
      _create_alternate_url($node);

     break;

     case 'update':
      _create_alternate_url($node);
     break;

     case 'view':
       //do nothing
     break;

  default:
  break;

  }
 }

 return;
}

然后我就有了这个功能,我想用这个功能来为我保存我的第二个 URL 别名.

Then I have this function, the one I'm trying to get to save my second URL alias for me.

function _create_alternate_url($node){
$aliasExists = db_fetch_object(db_query("SELECT count(dst) as total FROM {url_alias} WHERE dst = 'alternate/".$node->path."'"));
if($aliasExists->total == 0){

    $product_url = $node->path;
 $alternate_url = "alt/" . $node->path;
 $default_node_path = "node/" . $node->nid;

    path_set_alias($default_node_path, $alternate_url, 0, '');

  drupal_set_message("Created Alternate path for Product: " . $node->title . " <br/> Path <a href='/" . $default_node_path ."'>" . $default_node_path . "</a> is now aliased by <a href='/" . $alternate_url . "'>". $alternate_url ."</a>");
 }

虽然这不会设置别名,但它只会创建产品原始别名的副本.所以如果我一开始我的产品是绿色蕨类植物".我会保存它,它会使用 pathauto 生成 products/green-fern 然后在调用我的模块代码并创建一个别名alt/products/green-fern"并仍然让它指向node/nid"路径.

This doesn't set the alias though, it just creates a duplicate of the product's original alias. So If i started off with my product being "Green Fern". I would save it, and it would use pathauto to generate products/green-fern then after call my module code and make an alias "alt/products/green-fern" and still make it point back to the "node/nid" path.

但是,当我运行此代码时,会在数据库中创建一个副本.因此,我保存了 Green Fern 一次,突然间我在数据库中的 url_alias 末尾看到了两条重复记录.产品/绿蕨"和产品/绿蕨"

However, when I run this code a duplicate in the database is created. So I save Green Fern one time and all of a sudden I see two duplicate records at the end of the url_alias in the database. "products/green-fern" and "products/green-fern"

我觉得我在以一种过于复杂的方式思考这个问题.我的客户知道在将多个别名指向同一个节点时他们得到的 SEO 命中,他们只是希望它这样做.哈!

I feel like I'm thinking about this in a much too comlpex way. My client is aware of the SEO hit they get when making more than one alias point to the same node, they just want it to do this. Halp!

推荐答案

JR,在 Drupal7 中改进您的代码,db_querys 很昂贵.更好的方法是使用 lookup_path drupal 函数:

JR, improving your code in Drupal7, the db_querys are expensive. A better way could be using the lookup_path drupal function:

$urlAlias = drupal_lookup_path('alias',"node/".$node->nid . '/prices');

如果 urlAlias 没有值,那么这个 url 就没有别名,所以我们可以安全地创建它:

if urlAlias has no value, then there is no alias for this url, so we can safely create it:

if( urlAlias == '' )
path_set_alias($default_node_path, $alternate_url, 0, '');

这篇关于Drupal:如何以编程方式为节点保存已具有别名的节点创建 URL 别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆