3

We have a custom field that is being used to generate the URL alias.

One issue we have found is that when we set the page to "Autogenerate URL Alias" and set the URL pattern to "[node:field_custom_url]", we are still getting "%20" where the space should be.

We have done the following, but nothing seems to work. We needed assistance to see what else needs to be configured. Ideally, any spaces should be replaced with a "-" instead of "%20". Any thoughts?

Current settings (found in admin/config/search/path/settings):

  • Path pattern = "[node:field-custom-url]" (we even tried [node:field_custom_url], but it did not work)
  • Create a new alias. Delete the old alias. = Enabled
  • Separator = "-"
  • Transliterate prior to creating alias = Enabled
  • Reduce strings to letters and numbers

3 Answers 3

1

Your field token is failing the condition inside pathauto_clean_token_values, specifically as the token name has the reserved word url].

See function pathauto_clean_token_values:

function pathauto_clean_token_values(&$replacements, $data = array(), $options = array()) {
  foreach ($replacements as $token => $value) {
    // Only clean non-path tokens.
    if (!preg_match('/(path|alias|url|url-brief)\]$/', $token)) {
      // Convert language object into language code before pathauto_cleanstring().
      if (isset($options['language']->language)) {
        $options['langcode'] = $options['language']->language;
      }
      $replacements[$token] = pathauto_cleanstring($value, $options);
    }
  }
}

There's a preg_match which looks for:

  • path]
  • alias]
  • url]
  • url-brief]

in the token names, and your [node:field_custom_url] token will fail the 3rd pattern: url]. That's why your spaces are never replaced by the separator. You need to change your field machine name to be something not ending with the list above.

1
  • thank you for that information. When we created a new field with a different machine name it was working. Jul 15, 2016 at 19:52
3

Have a look at the Transliteration module. Some details about it (from its project page, with some bold markup added to it):

Provides one-way string transliteration (romanization) and cleans file names during upload by replacing unwanted characters.

Generally spoken, it takes Unicode text and tries to represent it in US-ASCII characters (universally displayable, unaccented characters) by attempting to transliterate the pronunciation expressed by the text in some other writing system to Roman letters.

Whether you use transliteration for URLs (when using Pathauto 2.x), however, is a matter of personal taste. ...

3

It is proved that I wrote wrongly in the beginning, thanks @Beebee.

I have read through the code of pathauto module. The module implements the token replacement after replacing the space with defined separator. So there is no clue by configuration. I would consider it is a design fault of the module.

pathauto module clean the url only when the token name does not match with the pattern /(path|alias|url|url-brief)\]$/. You can find it in the function pathauto_clean_token_values(). Obviously, the module author would like to prevent the url alias to be clean twice. As your field name is custom_url, so it is escaped from the checking.

You can fix this by changing your field name or writing your own module as I stated before.


If you know how to create custom module. I would recommend you to implement hook_pathauto_alias_alter for the purpose.

/**
 * Implements hook_pathauto_alias_alter().
 */
function mymodule_pathauto_alias_alter(&$alias, array &$context) {
  $separator = variable_get('pathauto_separator', '-');
  $alias = preg_replace('/\s+/', $separator, $alias);
}
2
  • I don't think we're reading the same code... pathauto replaces spaces with the separator during token_replace using a callback pathauto_clean_token_values, specifically by calling pathauto_cleanstring on the token value.
    – Beebee
    Jun 28, 2016 at 15:51
  • You are right, I missed some part of the code. I will edit this answer. Thanks for your sharing.
    – Jimmy Ko
    Jun 28, 2016 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.