You are here

function redirect_path_update in Redirect 7.2

Same name and namespace in other branches
  1. 7 redirect.module \redirect_path_update()

Implements hook_path_update().

File

./redirect.module, line 377

Code

function redirect_path_update(array $path) {
  if (!variable_get('redirect_auto_redirect', TRUE)) {
    return;
  }
  elseif (isset($path['redirect']) && !$path['redirect']) {
    return;
  }
  if (!empty($path['original']['pid']) && $path['original']['pid'] == $path['pid'] && $path['original']['alias'] != $path['alias']) {

    // Disable all redirects having the same source as this alias.
    redirect_disable_by_path($path['alias'], $path['language']);
    $redirect = new stdClass();
    redirect_object_prepare($redirect);
    $redirect->source = $path['original']['alias'];
    $redirect->redirect = $path['source'];
    $redirect->language = $path['original']['language'];

    // Check if the redirect exists before saving.
    $hash = redirect_hash($redirect);
    $existing = redirect_load_by_hash($hash);
    if (!$existing) {
      redirect_save($redirect);
    }
    elseif (isset($existing->status) && $existing->status == 0) {
      $existing->status = 1;
      redirect_save($existing);
    }
  }
}