在Drupal中,如何更改传递给Pathauto的值? [英] In Drupal, how to change the values passed to Pathauto?

查看:138
本文介绍了在Drupal中,如何更改传递给Pathauto的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Pathauto配置为基于特定内容类型的节点标题生成别名。问题是我想在Pathauto使用它来生成别名之前对此标题进行小的更改。



这篇文章建议使用hook_token_values,但我即使在阅读了文档之后,也无法真正了解如何使用它。在我的测试中,当我实现这个钩子时,生成的别名总是数组,这意味着我缺少一些东西。



任何帮助?谢谢。

解决方案

可能是你错过了实现 hook_token_list 好。提供新的令牌是一个两步的过程:


  1. 实现 hook_token_list 声明你要提供的令牌这将只是令牌的名称,以及简短的解释,以及令牌将应用什么类型的对象的信息(例如,节点,用户,分类法...)

  2. 实现 hook_token_value 来实际生成令牌的内容。当令牌被替换为应该支持的内容时,这将被调用。

因为你只想提供一个替代版本的令牌模块已经提供了标题令牌,最好只是将相关部分从token_node.inc复制到相关案例中,并调整为在另一个模块中使用:

  / ** 
*执行hook_token_list()。
* /
函数yourModule_token_list($ type ='all'){
if($ type =='node'|| $ type =='all'){
$ tokens ['node'] ['yourModule-title'] = t('Node title(custom version by yourModule)');

return $ tokens;
}
}

这简单地说 yourModule 为节点对象提供一个名为 yourModule-title 的标记,以及一个简短的描述。主要工作在另一个钩子中完成:

  / ** 
*执行hook_token_values()。
* /
函数yourModule_token_values($ type,$ object = NULL,$ options = array()){
$ values = array();
switch($ type){
case'node':
$ node = $ object;
// TODO:用你自己的令牌值创建逻辑替换check_plain()调用!
$ values ['yourModule-title'] = check_plain($ node-> title);
break;
}

返回$值;
}

每当需要节点对象的令牌时,都会调用这个节点,节点有问题是作为 $ object 参数传递(对于用户令牌, $ type 将是'user'而 $ object 将是用户对象,其他类型也是如此)。它的作用是创建一系列值,由令牌名称键入,替换该令牌作为值。来自token_node.inc的原始代码通过 check_plain()运行标题,所以这将是插入自己的逻辑的地方。


I have Pathauto configured to generate an alias based on the title of a node, for a specific content type. The problem is that I want to make small changes in this title before Pathauto uses it to generate the alias.

The first comment in this post suggests the use of hook_token_values, but I couldn't really understand how to use it, even after reading the docs. In my tests, when I implement this hook, the alias generated is always "array", which means I'm missing something.

Any help? Thanks.

解决方案

It might be that you missed to implement hook_token_list as well. Providing a new token is a two step process:

  1. Implement hook_token_list to declare the tokens you are going to provide. This will just be the name of the tokens, along with a short explanation, and the information to what type of objects the tokens will apply (e.g. node, user, taxonomy, ...)
  2. Implement hook_token_value to actually generate the content of the tokens. This will be called when the tokens are to be replaced with the content they should stand for.

As you just want to provide an alternative version of the title token already provided by the token module, it is probably best to just copy the relevant portions from token_node.inc, stripped down to the relevant cases and adjusted to be used in another module:

/**
 * Implementation of hook_token_list().
 */
function yourModule_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['yourModule-title'] = t('Node title (customized version by yourModule)');

    return $tokens;
  }
}

This simply says that yourModule provides a token for node objects, named yourModule-title, along with a short description. The main work gets done in the other hook:

/**
 * Implementation of hook_token_values().
 */
function yourModule_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'node':
      $node = $object;
      // TODO: Replace the check_plain() call with your own token value creation logic!
      $values['yourModule-title'] = check_plain($node->title);  
      break;
  }

  return $values;
}

This will be called whenever the tokens for node objects are needed, with the node in question being passed as the $object parameter (for a user token, the $type would be 'user', and $object would be the user object, and so on for other types). What it does is creating an array of values, keyed by the token name, with the replacement for that token as the value. The original code from token_node.inc just runs the title through check_plain(), so this would be the place to insert your own logic.

这篇关于在Drupal中,如何更改传递给Pathauto的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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