RenameAdminPathsProcessor.php in Rename Admin Paths 8
File
src/RenameAdminPathsProcessor.php
View source
<?php
namespace Drupal\rename_admin_paths;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Symfony\Component\HttpFoundation\Request;
class RenameAdminPathsProcessor implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
public function processInbound($path, Request $request) {
$config = \Drupal::config('rename_admin_paths.settings');
if ($config
->get('admin_path')) {
$admin_path_value = $config
->get('admin_path_value');
if (preg_match('|^/admin(?![^/])|i', $path)) {
$path = '/404';
}
elseif (preg_match('|^/' . urlencode($admin_path_value) . '(?![^/])(.*)|', $path, $matches)) {
$path = '/admin' . $matches[1];
}
}
if ($config
->get('user_path')) {
$user_path_value = $config
->get('user_path_value');
if (preg_match('|^/user(?![^/])|i', $path)) {
$path = '/404';
}
elseif (preg_match('|^/' . urlencode($user_path_value) . '(?![^/])(.*)|', $path, $matches)) {
$path = '/user' . $matches[1];
}
}
return $path;
}
public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
$config = \Drupal::config('rename_admin_paths.settings');
if ($request && $config
->get('admin_path')) {
$admin_path_value = $config
->get('admin_path_value');
if (preg_match('|^/admin(?![^/])(.*)|', $path, $matches)) {
$path = '/' . urlencode($admin_path_value) . $matches[1];
}
}
if ($config
->get('user_path')) {
$user_path_value = $config
->get('user_path_value');
if (preg_match('|^/user(?![^/])(.*)|', $path, $matches)) {
$path = '/' . urlencode($user_path_value) . $matches[1];
}
}
return $path;
}
}