In Drupal, how do I change the values ​​passed to Pathauto?

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

The first comment in this post suggests using hook_token_values, but I couldn't figure out how to use it even after reading the docs . In my tests, when I implement this hook, the generated alias is always an “array”, which means that I am missing something.

Any help? Thanks.

+4
source share
2 answers

You may have missed the implementation of hook_token_list . Providing a new token is a two-step process:

  • Add a hook_token_list to declare the tokens you are about to provide. It will be just the name of the tokens, as well as a brief explanation and information about what types of objects the tokens will use (for example, node, user, taxonomy, ...)
  • hook_token_value to actually create token content. This will be triggered when the tokens must be replaced by the contents on which they should stand.

Since you just want to provide an alternative version of the header token already provided by the token module, it is probably best to simply copy the corresponding parts from token_node.inc, trim it to the appropriate cases, and adjust it for use 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; } } 

It just says that yourModule provides a token for node objects named yourModule-title along with a brief description. The main work is done in another 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 tokens are needed for node objects, and the node question is passed as a parameter to $object (for a user token, $type will be a “user” and $object will be a user object, etc. for other types). What he does is create an array of values ​​defined by the name of the token, replacing this token as the value. The source code from token_node.inc just launches the header through check_plain() , so this will be the place to insert your own logic.

+5
source

In Drupal 7, marker functionality has been ported to the kernel. Tokens are implemented by hook_tokens and hook_token_info methods. For usage examples, follow the links provided and find links to functions that implement hook_tokens and hook_token_info & hellip; I found the statistics_tokens and statistics_token_info functions are useful in understanding how this hook works.

It may also be worth noting that this hook should be implemented by the & hellip; my first attempt I dumped my test functions into the template.php theme, just so that nothing would happen: -p

+1
source

Source: https://habr.com/ru/post/1309344/


All Articles