You are here

function redirect_delete_by_path in Redirect 8

Same name and namespace in other branches
  1. 7.2 redirect.module \redirect_delete_by_path()
  2. 7 redirect.module \redirect_delete_by_path()

Delete any redirects associated with a path or any of its sub-paths.

Given a source like 'node/1' this function will delete any redirects that have that specific source or any sources that match 'node/1/%'.

Parameters

string $path: An string with an internal Drupal path.

string $langcode: (optional) If specified, limits deletion to redirects for the given language. Defaults to all languages.

bool $match_subpaths_and_redirect: (optional) Whether redirects with a destination to the given path and sub-paths should also be deleted.

3 calls to redirect_delete_by_path()
redirect_entity_delete in ./redirect.module
Implements hook_entity_delete().
redirect_path_alias_insert in ./redirect.module
Implements hook_ENTITY_TYPE_insert() for path_alias.
redirect_path_alias_update in ./redirect.module
Implements hook_ENTITY_TYPE_update() for path_alias.

File

./redirect.module, line 179
The redirect module.

Code

function redirect_delete_by_path($path, $langcode = NULL, $match_subpaths_and_redirect = TRUE) {
  $path = ltrim($path, '/');
  $database = \Drupal::database();
  $query = $database
    ->select('redirect');
  $query
    ->addField('redirect', 'rid');
  $query_or = new Condition('OR');
  $query_or
    ->condition('redirect_source__path', $database
    ->escapeLike($path), 'LIKE');
  if ($match_subpaths_and_redirect) {
    $query_or
      ->condition('redirect_source__path', $database
      ->escapeLike($path . '/') . '%', 'LIKE');
    $query_or
      ->condition('redirect_redirect__uri', $database
      ->escapeLike($path), 'LIKE');
    $query_or
      ->condition('redirect_redirect__uri', $database
      ->escapeLike($path . '/') . '%', 'LIKE');
  }
  if ($langcode) {
    $query
      ->condition('language', $langcode);
  }
  $query
    ->condition($query_or);
  $rids = $query
    ->execute()
    ->fetchCol();
  if ($rids) {
    foreach (redirect_repository()
      ->loadMultiple($rids) as $redirect) {
      $redirect
        ->delete();
    }
  }
}