Commit e2479f23 authored by Gábor Hojtsy's avatar Gábor Hojtsy
Browse files

#154517 follow up by Desbeers: fix saving and updating language aware path aliases

parent 5120c8a8
...@@ -84,53 +84,45 @@ function path_admin_delete($pid = 0) { ...@@ -84,53 +84,45 @@ function path_admin_delete($pid = 0) {
* Set an aliased path for a given Drupal path, preventing duplicates. * Set an aliased path for a given Drupal path, preventing duplicates.
*/ */
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') { function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
if ($path && !$alias) { $path = urldecode($path);
// Delete based on path $alias = urldecode($alias);
db_query("DELETE FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language); // First we check if we deal with an existing alias and delete or modify it based on pid.
drupal_clear_path_cache(); if ($pid) {
} // An existing alias.
else if (!$path && $alias) { if (!$path || !$alias) {
// Delete based on alias // Delete the alias based on pid.
db_query("DELETE FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language); db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
drupal_clear_path_cache(); }
else {
// Update the existing alias.
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
}
} }
else if ($path && $alias) { else if ($path && $alias) {
$path = urldecode($path); // Check for existing aliases.
$path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language)); if ($alias == drupal_get_path_alias($path, $language)) {
$alias = urldecode($alias); // There is already such an alias, neutral or in this language.
// Alias count can only be 0 or 1. // Update the alias based on alias; setting the language if not yet done.
$alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language)); db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
if ($alias_count == 0) {
if ($pid) {
// Existing path changed data
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
}
else {
// No such alias yet in this language
db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
}
} }
// The alias exists.
else { else {
// This path has no alias yet, so we redirect the alias here. // A new alias. Add it to the database.
if ($path_count == 0) { db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s' AND language = '%s'", $path, $alias, $language);
}
else {
// This will delete the path that alias was originally pointing to.
path_set_alias(NULL, $alias, NULL, $language);
// This will remove the current aliases of the path.
path_set_alias($path, NULL, NULL, $language);
path_set_alias($path, $alias, NULL, $language);
}
} }
if ($alias_count == 0 || $path_count == 0) { }
drupal_clear_path_cache(); else {
// Delete the alias.
if ($alias) {
db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
} }
else {
db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
}
} }
drupal_clear_path_cache();
} }
/** /**
* Implementation of hook_nodeapi(). * Implementation of hook_nodeapi().
* *
...@@ -160,7 +152,7 @@ function path_nodeapi(&$node, $op, $arg) { ...@@ -160,7 +152,7 @@ function path_nodeapi(&$node, $op, $arg) {
// Don't try to insert if path is NULL. We may have already set // Don't try to insert if path is NULL. We may have already set
// the alias ahead of time. // the alias ahead of time.
if (isset($node->path)) { if (isset($node->path)) {
path_set_alias('node/'. $node->nid, $node->path); path_set_alias('node/'. $node->nid, $node->path, NULL, $language);
} }
break; break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment