You are here

public function RedirectPathSubscriber::replacePathPlaceholder in Tome 8

Reacts to a path placeholder event.

Parameters

\Drupal\tome_static\Event\PathPlaceholderEvent $event: The path placeholder event.

File

modules/tome_static/src/EventSubscriber/RedirectPathSubscriber.php, line 88

Class

RedirectPathSubscriber
Adds redirect placeholder paths to the list of paths to export.

Namespace

Drupal\tome_static\EventSubscriber

Code

public function replacePathPlaceholder(PathPlaceholderEvent $event) {
  $path = $event
    ->getPath();
  $pattern = '/' . static::PLACEHOLDER_PREFIX . ':[^\\/:]+/';
  if (preg_match($pattern, $path, $matches)) {
    list(, $entity_id) = explode(':', $matches[0]);
    $entity = $this->entityTypeManager
      ->getStorage('redirect')
      ->load($entity_id);
    if (!$entity || !$entity instanceof Redirect) {
      $event
        ->setInvalid();
      return;
    }
    if (!empty($entity->redirect_source->path) && !UrlHelper::isExternal($entity->redirect_source->path)) {
      $event
        ->setPath(preg_replace($pattern, $entity->redirect_source->path, $path));
    }
  }
}