class RedirectPathSubscriber in Tome 8
Adds redirect placeholder paths to the list of paths to export.
@internal
Hierarchy
- class \Drupal\tome_static\EventSubscriber\RedirectPathSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses PathTrait
Expanded class hierarchy of RedirectPathSubscriber
1 file declares its use of RedirectPathSubscriber
- TomeStaticServiceProvider.php in modules/
tome_static/ src/ TomeStaticServiceProvider.php
File
- modules/
tome_static/ src/ EventSubscriber/ RedirectPathSubscriber.php, line 21
Namespace
Drupal\tome_static\EventSubscriberView source
class RedirectPathSubscriber implements EventSubscriberInterface {
use PathTrait;
/**
* The path placeholder prefix.
*
* @var string
*/
const PLACEHOLDER_PREFIX = '_redirect';
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs the RedirectPathSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
/**
* Reacts to a collect paths event.
*
* @param \Drupal\tome_static\Event\CollectPathsEvent $event
* The collect paths event.
*/
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,
]);
}
}
}
/**
* Reacts to a path placeholder event.
*
* @param \Drupal\tome_static\Event\PathPlaceholderEvent $event
* The path placeholder event.
*/
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));
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[TomeStaticEvents::COLLECT_PATHS][] = [
'collectPaths',
];
$events[TomeStaticEvents::PATH_PLACEHOLDER][] = [
'replacePathPlaceholder',
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PathTrait:: |
protected | function | Joins multiple paths. | |
RedirectPathSubscriber:: |
protected | property | The entity type manager. | |
RedirectPathSubscriber:: |
protected | property | The language manager. | |
RedirectPathSubscriber:: |
public | function | Reacts to a collect paths event. | |
RedirectPathSubscriber:: |
public static | function | ||
RedirectPathSubscriber:: |
constant | The path placeholder prefix. | ||
RedirectPathSubscriber:: |
public | function | Reacts to a path placeholder event. | |
RedirectPathSubscriber:: |
public | function | Constructs the RedirectPathSubscriber object. |