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) {
// Delete based on path
db_query("DELETE FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language);
drupal_clear_path_cache();
}
else if (!$path && $alias) {
// Delete based on alias
db_query("DELETE FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language);
drupal_clear_path_cache();
}
else if ($path && $alias) {
$path = urldecode($path); $path = urldecode($path);
$path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s' AND language = '%s'", $path, $language));
$alias = urldecode($alias); $alias = urldecode($alias);
// Alias count can only be 0 or 1. // First we check if we deal with an existing alias and delete or modify it based on pid.
$alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $alias, $language));
if ($alias_count == 0) {
if ($pid) { if ($pid) {
// Existing path changed data // An existing alias.
if (!$path || !$alias) {
// Delete the alias based on pid.
db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
}
else {
// Update the existing alias.
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid); db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
} }
}
else if ($path && $alias) {
// Check for existing aliases.
if ($alias == drupal_get_path_alias($path, $language)) {
// There is already such an alias, neutral or in this language.
// Update the alias based on alias; setting the language if not yet done.
db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias);
}
else { else {
// No such alias yet in this language // A new alias. Add it to the database.
db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $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. // Delete the alias.
if ($path_count == 0) { if ($alias) {
db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s' AND language = '%s'", $path, $alias, $language); db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
} }
else { else {
// This will delete the path that alias was originally pointing to. db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
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(); 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