Pathauto alias programmatically? My custom module enables my custom token that allows me to create user paths from one of 3 fields: user:name, or if exists first name + last name, or if exists pageurl field.

this token really gives me the strings like:

Katherine Resette - if page url field is not filled 

Mike Nukle - if page url field is not filled

mybeautypage - if page url field is filled

This custom token is: [user:user_pageurl]

and I use this token in Pathauto Patterns on User Paths: [user:user_pageurl]

it does not create strings like:

example.com/katherine-resette and

example.com/mike-nukle

it use just this line as it is:

example.com/Katerine Resette

Please help me to fine my code with pathauto alias create ?

the code from my module user_pageurl.module is this:

     /**
 * Implements hook_token_info().
 */
function user_pageurl_token_info() {

  // Add tokens.
  $user['user_pageurl'] = array(
    'name' =&gt; t('Page URL'),
    'description' =&gt; t('Defines string for user page address based with priorities 
                        (if filled, 3 - high priority, 1 - low priority) : 1. login, 2.fields First name and Last name, 3. field Page URL'),
  );

  return array(
    'tokens' =&gt; array(
      'user' =&gt; $user,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function user_pageurl_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

 if ($type == 'user') {
    foreach ($tokens as $name =&gt; $original) {
      switch ($name) {
        case 'user_pageurl':
          $user_pageurl = user_pageurl_token_replace($data, $options);
          $replacements[$original] = $sanitize ? check_plain($user_pageurl) : $user_pageurl;

          // pathauto clean
          module_load_include('inc', 'pathauto', 'pathauto');
          $replacements[$original] = pathauto_clean_alias($replacements[$original]);
          break;
      }
    }
  }

  // Return the replacements.
  return $replacements;
}

 /**
 * Implements hook_token_info().
 * user_pageurl token might contain 1 of 3 user fields
 * 1: user:name - login
 * 2: first name + last name - if exists any
 * 3: pageurl field - if exists - highest priority
 */
function user_pageurl_token_replace($data, $options) {
  //dpm($options);
  $language = $options['language']-&gt;language;

  //high priority pageurl token - user:field_pageurl
  //dpm($data['user']-&gt;field_pageurl);
  if (isset($data['user']-&gt;field_pageurl[$language][0]['safe_value'])) {
    $pageurl = $data['user']-&gt;field_pageurl[$language][0]['safe_value'];

    return $pageurl;
  }

  //medium priority pageurl token - user:field_first_name user:field_last_name
  if (isset($data['user']-&gt;field_first_name[$language][0]['safe_value'])) {
    $first_name = $data['user']-&gt;field_first_name[$language][0]['safe_value'];
    $pageurl = $first_name;
  }
  if (isset($data['user']-&gt;field_last_name[$language][0]['safe_value'])) {
    $last_name = $data['user']-&gt;field_last_name[$language][0]['safe_value'];
    $pageurl = $last_name;
  }

  if (isset($first_name) &amp;&amp;  isset($last_name)) $pageurl = $first_name . ' '. $last_name;

  //low priority pageurl token - user:name
  $pageurl=$data['user']-&gt;name;

  return $pageurl;
}


I do not understand why pathauto takes the string as it is and do not transform it replacing uppercases, spaces and so on...
You have to prepare the string yourself, manually. You can use the pathauto_cleanstring() helper function that will remove all characters you wouldn't want in the URL (e.g. spaces).

Here's an example:

// The function we need is in the pathauto.inc file.
module_load_include('inc', 'pathauto');
$clean_title = pathauto_cleanstring($string_you_want_to_clear);


Hope this is what you were asking about.