? .project
Index: modules/path/path.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/path/path.module,v
retrieving revision 1.164
diff -u -p -r1.164 path.module
--- modules/path/path.module	15 Aug 2009 15:45:07 -0000	1.164
+++ modules/path/path.module	18 Aug 2009 15:27:51 -0000
@@ -83,7 +83,8 @@ function path_admin_delete($pid = 0) {
 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
   $path = urldecode($path);
   $alias = urldecode($alias);
-  // First we check if we deal with an existing alias and delete or modify it based on pid.
+  // First we check if we deal with an existing alias and delete or modify it
+  // based on pid.
   if ($pid) {
     // An existing alias.
     if (!$path || !$alias) {
@@ -270,7 +271,8 @@ function path_form_taxonomy_form_term_al
       $url = 'taxonomy/term/' . $form['#term']['tid'];
       $alias = drupal_get_path_alias($url);
 	  
-      // Since drupal_get_path_alias() can return the default path, check if we really have an alias.
+      // Since drupal_get_path_alias() can return the default path, check if
+      // we really have an alias.
       if ($alias != $url) {
         $path = $alias;
       }
@@ -290,7 +292,8 @@ function path_form_taxonomy_form_term_al
       '#description' => t("Optionally specify an alternative URL by which this term can be accessed. Use a relative path and don't add a trailing slash or the URL alias won't work."),
     );
     if ($path) {
-      // Populate with pid so we can update existing path entry instead of creating a new one.
+      // Populate with pid so we can update existing path entry instead of
+      // creating a new one.
       $form['identification']['path']['pid'] = array(
         '#type' => 'value',
         '#access' => (user_access('create url aliases') || user_access('administer url aliases')),
@@ -328,6 +331,113 @@ function path_taxonomy_term_submit($form
 }
 
 /**
+ * Implement hook_form_FORM_ID_alter().
+ *
+ * Add path settings to user edit form.
+ */
+function path_form_user_profile_form_alter(&$form, &$form_alter) {
+  $path = isset($form['_account']['#value']->path) ? $form['_account']['#value']->path : NULL;
+  // Add path settings to profile form.
+  _path_user_form_inject_path($form);
+  if ($path) {
+    // If we already have a path add its id so we can update the path rather
+    // than creating a new one after saving the profile.
+    $form['path']['pid'] = array(
+      '#type' => 'value',
+      '#value' => db_query("SELECT pid FROM {url_alias} WHERE dst = :dst", array(
+        ':dst' => $path,
+      ))
+      ->fetchField(),
+    );
+  }
+}
+
+/**
+ * Implement hook_form_FORM_ID_alter().
+ *
+ * Add path settings to user register form.
+ */
+function path_form_user_register_alter(&$form, &$form_alter) {
+  // Add path settings to registration form.
+  _path_user_form_inject_path($form);
+  // Add custom submit handler to the user register form because there
+  // is no hook.
+  $form['#submit'][] = 'path_user_register_submit';
+}
+
+/**
+ * Add fieldset with url alias textfield to given form.
+ *
+ * @param $form
+ *   The form to inject the url alias textfield.
+ */
+function _path_user_form_inject_path(&$form) {
+  $path = isset($form['_account']['#value']->path) ? $form['_account']['#value']->path : NULL;
+  $form['path'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('URL path settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => empty($path),
+    '#access' => user_access('create url aliases'),
+    '#weight' => 1,
+  );
+  $form['path']['path'] = array(
+    '#type' => 'textfield',
+    '#title' => t('URL alias'),
+    '#default_value' => $path,
+    '#maxlength' => 255,
+    '#description' => t('Optionally specify an alternative URL by which the user\'s profile can be accessed.'),
+  );
+}
+
+/**
+ * Custom submit handler for the user registration form.
+ */
+function path_user_register_submit($form, &$form_state) {
+  if (isset($form_state['user']->uid)) {
+    path_set_alias('user/' . $form_state['user']->uid, $form_state['user']->path);
+  }
+}
+
+/**
+ * Implement hook_user_load().
+ *
+ * Get the optionally entered url alias for the loaded users.
+ */
+function path_user_load($users) {
+  $userpaths = array();
+  foreach (array_keys($users) as $uid) {
+    $userpaths[] = 'user/' . $uid;
+  }
+  $result = db_query('SELECT * FROM {url_alias} WHERE src IN (:userpaths)', array(':userpaths' => $userpaths));
+  $uid = 0;
+  foreach ($result as $record) {
+    $uid = str_replace('user/', '', $record->src);
+    $users[$uid]->path = $record->dst;
+  }
+}
+
+/**
+ * Implement hook_user().
+ */
+function path_user_submit(&$edit, &$account, $category = NULL) {
+  $path = isset($edit['path']) ? $edit['path'] : NULL;
+  $pid = isset($edit['pid']) ? $edit['pid'] : NULL;
+  path_set_alias('user/' . $account->uid, $path, $pid);
+}
+
+/**
+ * Implement hook_user_cancel().
+ */
+function path_user_cancel($edit, $account, $method) {
+  switch ($method) {
+    case 'user_cancel_delete':
+      path_set_alias('user/' . $account->uid);
+      break;
+  }
+}
+
+/**
  * Implement hook_permission().
  */
 function path_permission() {
