0

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' => t('Page URL'),
    'description' => 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' => array(
      'user' => $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 => $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']->language;

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

    return $pageurl;
  }

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

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

  //low priority pageurl token - user:name
  $pageurl=$data['user']->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...

1

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.

  • this helped me fine! i think module real name has special function for pathauto transformation. this is why i need only to type real name token and it works. so in my module i need this line as you type. thank you – tlito Aug 16 '15 at 16:03
  • You're welcome - glad it helped. – Aram Boyajyan Aug 17 '15 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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