1 redirect.module redirect_path_update(array $path)

Implements hook_path_update().

File

core/modules/redirect/redirect.module, line 346

Code

function redirect_path_update(array $path) {
  if (!config_get('redirect.settings', 'auto_redirect')) {
    return;
  }
  elseif (isset($path['redirect']) && !$path['redirect']) {
    return;
  }

  // If the path ID is the same, but the alias has changed, make a redirect.
  if (!empty($path['original']['pid']) && $path['original']['pid'] == $path['pid'] && $path['original']['alias'] != $path['alias']) {
    $redirect = new Redirect(array(
      'source' => $path['original']['alias'],
      'redirect' => $path['source'],
      'langcode' => $path['original']['langcode'],
    ));

    // Check if the redirect already exists before saving a new one.
    $hash = redirect_hash($redirect);
    if (!redirect_load_by_hash($hash)) {
      redirect_save($redirect);
    }

    // Check if the new path was previously a redirect and delete it.
    if ($previous_redirect = redirect_load_by_source($path['alias'], $path['langcode'])) {
      redirect_delete($previous_redirect->rid);
    }
  }
}