Drupal:如何为节点上已经有别名的节点编程创建一个URL别名? [英] Drupal: How do I programatically create a URL alias for a node that already has an alias on node save?

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

问题描述



我有一个自定义模块,它实现钩子nodeapi来在创建或更新节点时执行一些代码。节点保存或更新时自动生成别名。



现在我正在使用一个对path_set_alias的调用,我只想使用特定类型的内容product



这是我的nodeapi调用让我开始

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

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

case'insert':
_create_alternate_url($ node);

break;

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

case'view':
// do nothing
break;

默认值:
break;

}
}

return;
}

然后我有这个功能,我想要保存我的第二个URL别名为我。

  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 /。 $节点 - >路径;
$ default_node_path =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>现在通过< a href ='/进行别名$ alternate_url。''>$ alternate_url。< / a>);
}

这不会设置别名,它只是创建一个产品的原始别名。所以如果我从我的产品开始就是绿色蕨。我会保存它,它将使用pathauto生成产品/绿蕨,然后调用我的模块代码,并使一个别名alt / products / green-fern,仍然指向节点/ nid路径



但是,当我运行这个代码时,数据库中的副本将被创建。所以我节省了绿色蕨一次,突然间我看到数据库中url_alias结尾的两个重复记录。 产品/绿蕨和产品/绿蕨



我觉得我正在以一种非常comlpex的方式思考这个问题。我的客户端知道当多个别名指向同一个节点时,他们得到的SEO命中,他们只是希望这样做。 Halp!

解决方案

JR,在Drupal7中改进代码,db_querys很贵。一个更好的方法可能是使用lookup_path drupal函数:

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

如果urlAlias没有价值,那么这个url没有别名,所以我们可以安全地创建它如果(urlAlias =='')
path_set_alias($ default_node_path,$ alternate_url,0,'')$

  


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.

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".

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

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

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.

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"

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, 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');

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天全站免登陆