RedirectPathSubscriber.php in Tome 8
File
modules/tome_static/src/EventSubscriber/RedirectPathSubscriber.php
View source
<?php
namespace Drupal\tome_static\EventSubscriber;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\redirect\Entity\Redirect;
use Drupal\tome_base\PathTrait;
use Drupal\tome_static\Event\CollectPathsEvent;
use Drupal\tome_static\Event\PathPlaceholderEvent;
use Drupal\tome_static\Event\TomeStaticEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RedirectPathSubscriber implements EventSubscriberInterface {
use PathTrait;
const PLACEHOLDER_PREFIX = '_redirect';
protected $entityTypeManager;
protected $languageManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
public function collectPaths(CollectPathsEvent $event) {
$langcodes = array_keys($this->languageManager
->getLanguages());
$storage = $this->entityTypeManager
->getStorage('redirect');
$langcode_key = $storage
->getEntityType()
->getKey('langcode');
$langcodes[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
foreach ($langcodes as $langcode) {
foreach ($storage
->getQuery()
->condition($langcode_key, $langcode)
->execute() as $entity_id) {
$event
->addPath(implode(':', [
static::PLACEHOLDER_PREFIX,
$entity_id,
]), [
'language_prefix' => $langcode,
]);
}
}
}
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));
}
}
}
public static function getSubscribedEvents() {
$events[TomeStaticEvents::COLLECT_PATHS][] = [
'collectPaths',
];
$events[TomeStaticEvents::PATH_PLACEHOLDER][] = [
'replacePathPlaceholder',
];
return $events;
}
}