You are here

protected function Repair301::doHandle in Link checker 8

Handles a status code of link.

Parameters

\Drupal\linkchecker\LinkCheckerLinkInterface $link: The link.

\Psr\Http\Message\ResponseInterface $response: The response of link checking.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity with proper translation loaded.

Overrides LinkStatusHandlerBase::doHandle

File

src/Plugin/LinkStatusHandler/Repair301.php, line 86

Class

Repair301
Repairs 301 links.

Namespace

Drupal\linkchecker\Plugin\LinkStatusHandler

Code

protected function doHandle(LinkCheckerLinkInterface $link, ResponseInterface $response, FieldableEntityInterface $entity) {
  $autoRepair301 = $this->linkcheckerSetting
    ->get('error.action_status_code_301');
  $redirectUrl = $response
    ->getHeaderLine('Location');
  if ($autoRepair301 && $autoRepair301 <= $link
    ->getFailCount() && UrlHelper::isValid($redirectUrl, TRUE) && $link
    ->isExists()) {
    $values = $entity
      ->get($link
      ->getParentEntityFieldName())
      ->getValue();
    foreach ($values as $key => $value) {
      if (isset($value['value'])) {
        $values[$key]['value'] = $this
          ->linkReplace($value['value'], $link
          ->getUrl(), $redirectUrl);
      }
    }
    $entity
      ->set($link
      ->getParentEntityFieldName(), $values);
    $entity
      ->save();
  }
}