DynamicPathProcessor.php in View Mode Page 8.3
File
src/PathProcessor/DynamicPathProcessor.php
View source
<?php
namespace Drupal\view_mode_page\PathProcessor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\Url;
use Drupal\view_mode_page\Repository\ViewmodepagePatternRepository;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
class DynamicPathProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface, ContainerAwareInterface {
use ContainerAwareTrait;
protected $aliasManager;
protected $entityTypeManager;
protected $patternRepository;
protected $languageManager;
public function __construct(AliasManagerInterface $alias_manager, EntityTypeManagerInterface $entity_type_manager, ViewmodepagePatternRepository $pattern_repository, LanguageManagerInterface $language_manager) {
$this->aliasManager = $alias_manager;
$this->entityTypeManager = $entity_type_manager;
$this->patternRepository = $pattern_repository;
$this->languageManager = $language_manager;
}
public function processInbound($path, Request $request) {
$patterns = $this->patternRepository
->findAll();
foreach ($patterns as $pattern) {
if (preg_match($pattern
->getPatternRegex(), $path, $matchesArray)) {
$entityAlias = $matchesArray[1];
$entityUri = $this->aliasManager
->getPathByAlias('/' . $entityAlias);
$url = Url::fromUri('internal:' . $entityUri);
if ($url
->isRouted()) {
$routeParams = $url
->getRouteParameters();
if ($entityType = key($routeParams)) {
$entityId = $routeParams[$entityType];
}
}
if (!empty($entityType) && !empty($entityId) && $entityType == $pattern
->getAliasType()
->getDerivativeId()) {
$entity = $this->entityTypeManager
->getStorage($entityType)
->load($entityId);
if ($entity instanceof EntityInterface) {
$language_interface = $this->languageManager
->getCurrentLanguage();
if ($entity instanceof TranslatableInterface && $entity
->hasTranslation($language_interface
->getId())) {
$entity = $entity
->getTranslation($language_interface
->getId());
}
if ($pattern
->applies($entity)) {
$newPath = '/view_mode_page/' . $pattern
->getViewMode() . '/' . $entityType . '/' . $entityId;
return $newPath;
}
}
}
}
}
return $path;
}
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
$prefix = '/view_mode_page/';
if (substr($path, 0, strlen($prefix)) === $prefix) {
$path_minus_prefix = substr($path, strlen($prefix));
$path_minus_prefix_parts = explode('/', $path_minus_prefix);
if (count($path_minus_prefix_parts) === 3) {
list($view_mode, $entityType, $entityId) = $path_minus_prefix_parts;
$entity = $this->entityTypeManager
->getStorage($entityType)
->load($entityId);
if ($entity) {
if (isset($options['language'])) {
$target_language = $options['language'];
}
else {
$target_language = $this->languageManager
->getCurrentLanguage();
}
if ($target_language instanceof LanguageInterface && $entity instanceof TranslatableInterface && $entity
->hasTranslation($target_language
->getId())) {
$entity = $entity
->getTranslation($target_language
->getId());
}
$patterns = $this->entityTypeManager
->getStorage('view_mode_page_pattern')
->loadByProperties([
'view_mode' => $view_mode,
]);
foreach ($patterns as $pattern) {
if ($pattern
->applies($entity)) {
$url = $entity
->toUrl();
$url_alias = $this->aliasManager
->getAliasByPath("/" . $url
->getInternalPath(), $target_language
->getId());
$path = str_replace('%', $url_alias, $pattern
->getPattern());
break;
}
}
}
}
}
return $path;
}
}