You are here

public function BrokenLinkRedirect::getRedirectLink in Broken Link 8

Same name and namespace in other branches
  1. 8.3 src/Entity/BrokenLinkRedirect.php \Drupal\broken_link\Entity\BrokenLinkRedirect::getRedirectLink()
  2. 8.2 src/Entity/BrokenLinkRedirect.php \Drupal\broken_link\Entity\BrokenLinkRedirect::getRedirectLink()

Method to get redirect path based on broken link.

Parameters

string $broken_link: Broken link.

Return value

string Redirect path for the broken link.

File

src/Entity/BrokenLinkRedirect.php, line 98

Class

BrokenLinkRedirect
Defines the Broken link redirect entity entity.

Namespace

Drupal\broken_link\Entity

Code

public function getRedirectLink($broken_link) {
  $db = \Drupal::database();
  $broken_link_redirect = $db
    ->select('broken_link_redirect', 'blr')
    ->fields('blr', array(
    'pattern',
    'redirect_path',
  ))
    ->condition('enabled', 1)
    ->orderBy('weight')
    ->execute();
  foreach ($broken_link_redirect as $value) {
    $pattern = ltrim($value->pattern, '\\/');
    if (preg_match("/{$pattern}/", $broken_link)) {
      return $value->redirect_path;
    }
  }
  return NULL;
}