Index: modules/path/path.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/path/path.admin.inc,v retrieving revision 1.34 diff -u -p -r1.34 path.admin.inc --- modules/path/path.admin.inc 15 Oct 2009 17:53:34 -0000 1.34 +++ modules/path/path.admin.inc 17 Oct 2009 03:27:56 -0000 @@ -8,6 +8,7 @@ /** * Return a listing of all defined URL aliases. + * * When filter key passed, perform a standard search on the given key, * and return the list of matching URL aliases. */ @@ -156,11 +157,11 @@ function path_admin_form_validate($form, $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : ''; $has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array( - ':pid' => $pid, - ':alias' => $alias, - ':language' => $language, - )) - ->fetchField(); + ':pid' => $pid, + ':alias' => $alias, + ':language' => $language, + )) + ->fetchField(); if ($has_alias) { form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias))); @@ -175,17 +176,12 @@ function path_admin_form_validate($form, * Save a URL alias to the database. */ function path_admin_form_submit($form, &$form_state) { - $path = array(); - foreach (array('source', 'alias', 'pid', 'language') as $key) { - if (isset($form_state['values'][$key])) { - $path[$key] = $form_state['values'][$key]; - } - } - path_save($path); + // Remove unnecessary values. + form_state_values_clean($form_state); + path_save($form_state['values']); drupal_set_message(t('The alias has been saved.')); $form_state['redirect'] = 'admin/config/search/path'; - return; } /** @@ -211,7 +207,6 @@ function path_admin_delete_confirm_submi if ($form_state['values']['confirm']) { path_delete($form_state['path']['pid']); $form_state['redirect'] = 'admin/config/search/path'; - return; } } Index: modules/path/path.module =================================================================== RCS file: /cvs/drupal/drupal/modules/path/path.module,v retrieving revision 1.171 diff -u -p -r1.171 path.module --- modules/path/path.module 15 Oct 2009 17:53:34 -0000 1.171 +++ modules/path/path.module 17 Oct 2009 03:27:57 -0000 @@ -83,10 +83,10 @@ function path_load($criteria) { if (is_numeric($criteria)) { $criteria = array('pid' => $criteria); } - else if (is_string($criteria)) { + elseif (is_string($criteria)) { $criteria = array('source' => $criteria); } - else if (!is_array($criteria)) { + elseif (!is_array($criteria)) { return FALSE; } $select = db_select('url_alias'); @@ -163,17 +163,14 @@ function path_delete($criteria) { function path_node_validate($node, $form) { if (user_access('create url aliases') || user_access('administer url aliases')) { if (isset($node->path)) { - if (!is_array($node->path)) { - $node->path = array('alias' => $node->path); - } - $select = db_select('url_alias')->condition('alias', trim($node->path['alias'])); + $select = db_select('url_alias'); $select->addExpression('COUNT(alias)'); + $select->condition('alias', trim($node->path)); + $select->condition('language', isset($node->language) ? $node->language : ''); if ($node->nid) { $select->condition('source', 'node/' . $node->nid, '<>'); } - if (isset($node->language)) { - $select->condition('language', $node->language); - } + if ($select->execute()->fetchField()) { form_set_error('path', t('The path is already in use.')); } @@ -203,9 +200,14 @@ function path_node_load($nodes, $types) function path_node_insert($node) { if ((user_access('create url aliases') || user_access('administer url aliases')) && isset($node->path)) { if (!is_array($node->path)) { - $node->path = array('alias' => $node->path); + $alias = trim($node->path); + // Don't save empty alias. + if (empty($alias)) { + return ; + } + $node->path = array('alias' => $alias); } - + $node->path += array( 'source' => 'node/' . $node->nid, 'language' => isset($node->language) ? $node->language : '', @@ -220,7 +222,16 @@ function path_node_insert($node) { function path_node_update($node) { if ((user_access('create url aliases') || user_access('administer url aliases')) && isset($node->path)) { if (!is_array($node->path)) { - $node->path = array('alias' => $node->path); + $alias = trim($node->path); + if ($alias == '' && $node->pid) { + // Delete old alias if user erase it. + path_delete($node->pid); + } + elseif ($alias == '') { + // Don't save empty alias. + return ; + } + $node->path = array('alias' => $alias); } if (isset($node->pid)) { $node->path['pid'] = $node->pid; @@ -237,13 +248,7 @@ function path_node_update($node) { * Implement hook_node_delete(). */ function path_node_delete($node) { - if (isset($node->path)) { - if (!is_array($node->path)) { - $node->path = path_load(array('alias' => $node->path)); - } - path_delete($node->path['pid']); - unset($node->path); - } + path_delete(array('source' => 'node/' . $node->nid)); } /** @@ -271,12 +276,12 @@ function path_form_alter(&$form, $form_s '#type' => 'fieldset', '#title' => t('URL path settings'), '#collapsible' => TRUE, - '#collapsed' => empty($path), + '#collapsed' => ($path == ''), '#group' => 'additional_settings', '#attached' => array( 'js' => array(drupal_get_path('module', 'path') . '/path.js'), ), - '#access' => user_access('create url aliases'), + '#access' => user_access('create url aliases') || user_access('administer url aliases'), '#weight' => 30, ); $form['path']['path'] = array( @@ -303,21 +308,21 @@ function path_form_alter(&$form, $form_s function path_form_taxonomy_form_term_alter(&$form, $form_state) { // Make sure this does not show up on the delete confirmation form. if (empty($form_state['confirm_delete'])) { - // After a new term is added, populate the path field if it was set. - if (!empty($form['#term']['path'])) { - $path = $form['#term']['path']; - if (!is_array($path)) { - $path = path_load(array('alias' => $path)); - } - } - else { - $alias = path_load('taxonomy/term/' . $form['#term']['tid']); - // Since drupal_get_path_alias() can return the default path, check if we really have an alias. - if ($alias['alias'] != 'taxonomy/term/' . $form['#term']['tid']) { - $path = $alias; - } - else { - $path = NULL; + $access = user_access('create url aliases') || user_access('administer url aliases'); + $alias = ''; + $pid = NULL; + // Take into account multi-step rebuilding. + if (isset($form['#term']['path'])) { + $alias = $form['#term']['path']; + if (isset($form['#term']['pid'])) { + $pid = $form['#term']['pid']; + } + } + elseif (isset($form['#term']['tid'])) { + $path = path_load('taxonomy/term/' . $form['#term']['tid']); + if (isset($path['pid'])) { + $alias = $path['alias']; + $pid = $path['pid']; } } $form['#validate'][] = 'path_taxonomy_term_validate'; @@ -325,18 +330,18 @@ function path_form_taxonomy_form_term_al $form['identification']['path'] = array( '#type' => 'textfield', '#title' => t('URL alias'), - '#default_value' => $path['alias'], + '#default_value' => $alias, '#maxlength' => 255, '#weight' => 0, - '#access' => (user_access('create url aliases') || user_access('administer url aliases')), + '#access' => $access, '#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) { + if ($pid) { // 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')), - '#value' => db_query("SELECT pid FROM {url_alias} WHERE alias = :alias", array(':alias' => $path['alias']))->fetchField(), + '#access' => $access, + '#value' => $pid, ); } } @@ -346,13 +351,16 @@ function path_form_taxonomy_form_term_al * Path validation callback for taxonomy_form_term. */ function path_taxonomy_term_validate($form, &$form_state) { - $path = path_load(array('alias' => $form_state['values']['path'])); - if ($path) { - // If the pid matches the one in use for this term then we are fine. - if (isset($form_state['values']['pid']) && $path['pid'] == $form_state['values']['pid']) { - return; + $alias = trim($form_state['values']['path']); + if ($alias != '') { + $path = path_load(array('alias' => $alias)); + if ($path) { + // If the pid matches the one in use for this term then we are fine. + if (isset($form_state['values']['pid']) && $path['pid'] == $form_state['values']['pid']) { + return; + } + form_set_error('path', t('The URL alias is already in use.')); } - form_set_error('path', t('The URL alias is already in use.')); } } @@ -362,12 +370,19 @@ function path_taxonomy_term_validate($fo function path_taxonomy_term_submit($form, &$form_state) { // Make sure this is not triggered on the delete confirmation form. if (empty($form_state['confirm_delete'])) { - $path = array( - 'source' => 'taxonomy/term/' . $form_state['tid'], - 'alias' => isset($form_state['values']['path']) ? $form_state['values']['path'] : NULL, - 'pid' => isset($form_state['values']['pid']) ? $form_state['values']['pid'] : NULL, - ); - path_save($path); + $alias = trim($form_state['values']['path']); + if ($alias == '' && isset($form_state['values']['pid'])) { + // Delete old alias if user erase it. + path_delete($form_state['values']['pid']); + } + elseif ($alias != '') { + $path = array( + 'source' => 'taxonomy/term/' . $form_state['tid'], + 'alias' => $alias, + 'pid' => isset($form_state['values']['pid']) ? $form_state['values']['pid'] : NULL, + ); + path_save($path); + } } }