PathAliasXtProcessorAlias.php in Extended Path Aliases 8
File
src/PathAliasXtProcessorAlias.php
View source
<?php
namespace Drupal\path_alias_xt;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactory;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\path_alias\PathProcessor\AliasPathProcessor;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class PathAliasXtProcessorAlias extends AliasPathProcessor {
protected $configFactory;
protected $moduleHandler;
protected $entityTypeManager;
public function __construct(AliasManagerInterface $alias_manager, ConfigFactory $config_factory, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($alias_manager);
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
}
public function processInbound($path, Request $request) {
$path_to_process = ltrim($path, '/');
if ($this->moduleHandler
->moduleExists('redirect')) {
$query = $this->entityTypeManager
->getStorage('redirect')
->getQuery();
$query
->condition('redirect_source__path', $path_to_process);
if ($query
->execute()) {
return $path;
}
}
$removed_elements = [];
$path_elements = explode('/', $path_to_process);
foreach ($path_elements as $element) {
$candidate_alias = '/' . implode('/', $path_elements);
$source = $this->aliasManager
->getPathByAlias($candidate_alias);
if ($source != $candidate_alias) {
krsort($removed_elements);
$return_path = $source;
if (!empty($removed_elements)) {
$return_path .= '/' . implode('/', $removed_elements);
}
if (\Drupal::service('path.validator')
->getUrlIfValidWithoutAccessCheck($return_path)) {
return $return_path;
}
}
$removed_elements[] = array_pop($path_elements);
}
return $path;
}
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
$path = parent::processOutbound($path, $options, $request, $bubbleable_metadata);
$config = $this->configFactory
->get('path_alias_xt.settings');
if (preg_match($config
->get('regex_pattern'), mb_substr($path, 1), $matches)) {
$langcode = isset($options['language']) ? $options['language']
->getId() : NULL;
if ($alias = $this->aliasManager
->getAliasByPath("/{$matches[1]}/{$matches[2]}", $langcode)) {
$path = "{$alias}/{$matches[3]}";
}
}
return $path;
}
}