diff --git a/redirect.module b/redirect.module index 8c47b75..051f464 100644 --- a/redirect.module +++ b/redirect.module @@ -393,6 +393,13 @@ function redirect_path_update(array $path) { return; } + if (!empty($path['alias'])) { + // !An alias can't be used as a source, it has to be the original. Since an alias is + // preferable to a redirect and there's no hook_path_validate() to stop them anyway, + // we defer to the alias. + redirect_free_source($path['alias']); + } + if (!empty($path['original']['pid']) && $path['original']['pid'] == $path['pid'] && $path['original']['alias'] != $path['alias']) { $redirect = new stdClass(); redirect_object_prepare($redirect); @@ -408,6 +415,41 @@ function redirect_path_update(array $path) { } /** + * Implements hook_path_insert(). + */ +function redirect_path_insert(array $path) { + if (!empty($path['alias'])) { + //! No hook_path_validate(), and alias beats redirect + redirect_free_source($path['alias']); + } +} + +/** + * Ensures that a source path is available. Warns the user if it deletes an existing record. + * + * Similar to redirect_delete_by_path(), but doesn't recurse sub-paths. This is called on + * redirect_path_*() to make sure we don't duplicate the work of an alias (which would cause + * an internal loop). + * + * @param $source + * The redirect source path. + * + * @return + * The deleted redirect object, or FALSE if nothing found. + * + * @ingroup redirect_api + */ +function redirect_free_source($source) { + $redirect_record = redirect_load_by_source($source); + if ($redirect_record) { + redirect_delete($redirect_record->rid); + $warning = t('A @type record using the path @source (pointing to @redirect) has been removed.', array('@type' => $redirect_record->type, '@source' => $redirect_record->source, '@redirect' => $redirect_record->redirect)); + drupal_set_message($warning,'warning'); + } + return $redirect_record; +} + +/** * Implements hook_path_delete(). */ function redirect_path_delete($path) {