Custom tokens and/or text filters for consistent link look? I have a site with a range of products. In order to ensure an easy input for the content editor and a consistent look throughout the site, I thought I'd make an input filter that would replace any occurrence of

[prod:product-name]


with a link to the node presenting the product (product-name == product code here). On output, this would be changed into, say:

&lt;a href="products/product-name" class="product product-product-name"&gt;Real [product-name]&lt;/a&gt;


However, I have doubts as per the efficiency side of such a solution, namely because - product-name would have to be a value of a field on the content type. Thus, for each replacement, Drupal would have to comb through all nodes, load each of them and perform a test to find a match. Quite database-intensive as I imagine.

So another thought was to provide a custom token. But would that change anything? Would it work within text fields, or would I have to use hook_node_view_alter to invoke the replacements?

What would be your strategy to provide this kind of tokens?
You could implement an input filter, or implement hook_node_view_alter().

Keep in mind that an input filter is not only used for node's body, but also for comments, user's signature, some part of a view, and theoretically with any module that uses check_markup() and that also allows the user to select an input format to use. As consequence of this, input filters should not take assumptions about when it is invoked.

There isn't normally any performance issue for accessing a node's fields; Drupal uses caches to avoid executing queries when not necessary.

If you are talking about the tokens provided from Drupal 7, or from the Token module, you could also implement a token, but you should consider that a token is accessible not only in the context you are expecting it to use it.
You should still need code to replace the token in text, using an input filter (for which the previous notes are still valid), or hook_node_view_alter().

If you need to provide a token that should be accessible in a specific context (e.g. to be replaced in the body of nodes), you could use the code used by Drupal, which (in _user_mail_text()) uses the following code.

  if ($replace) {
    // We do not sanitize the token replacement, since the output of this
    // replacement is intended for an e-mail message, not a web browser.
    return token_replace($text, $variables, array('language' =&gt; $language, 'callback' =&gt; 'user_mail_tokens', 'sanitize' =&gt; FALSE, 'clear' =&gt; TRUE));
  }


user_mail_tokens() is the function that provides the custom token that should not be available in other contexts.

function user_mail_tokens(&amp;$replacements, $data, $options) {
  if (isset($data['user'])) {
    $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user']);
    $replacements['[user:cancel-url]'] = user_cancel_url($data['user']);
  }
}