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

问题描述

我已将 Pathauto 配置为基于节点标题为特定内容类型生成别名.问题是我想在 Pathauto 使用它生成别名之前对这个标题做一些小的更改.

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.

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

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.

有什么帮助吗?谢谢.

推荐答案

您可能也错过了实施 hook_token_list.提供新令牌需要两个步骤:

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

  1. 实施 hook_token_list 以声明您要提供的令牌.这只是令牌的名称,以及简短的说明,以及令牌将应用的对象类型的信息(例如节点、用户、分类法等)
  2. 实施 hook_token_value 以实际生成令牌的内容.当令牌将被替换为它们应代表的内容时,将调用此方法.
  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.

由于您只想提供令牌模块已提供的标题令牌的替代版本,因此最好仅从 token_node.inc 复制相关部分,精简为相关案例并调整以用于另一个模块:

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

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

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

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

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 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:02