3 namespace Drupal\url_alter_test;
5 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
6 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
7 use Drupal\Core\Render\BubbleableMetadata;
8 use Symfony\Component\HttpFoundation\Request;
9 use Drupal\user\Entity\User;
12 * Path processor for url_alter_test.
14 class PathProcessorTest implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
19 public function processInbound($path, Request $request) {
20 // Rewrite user/username to user/uid.
21 if (preg_match('!^/user/([^/]+)(/.*)?!', $path, $matches)) {
22 if ($account = user_load_by_name($matches[1])) {
23 $matches += [2 => ''];
24 $path = '/user/' . $account->id() . $matches[2];
28 // Rewrite community/ to forum/.
29 $path = preg_replace('@^/community(.*)@', '/forum$1', $path);
31 if ($path == '/url-alter-test/bar') {
32 $path = '/url-alter-test/foo';
40 public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
41 // Rewrite user/uid to user/username.
42 if (preg_match('!^/user/([0-9]+)(/.*)?!', $path, $matches)) {
43 if ($account = User::load($matches[1])) {
44 $matches += [2 => ''];
45 $path = '/user/' . $account->getUsername() . $matches[2];
46 if ($bubbleable_metadata) {
47 $bubbleable_metadata->addCacheTags($account->getCacheTags());
52 // Verify that $options are alterable.
53 if ($path == '/user/login') {
54 $options['query']['foo'] = 'bar';
57 // Rewrite forum/ to community/.
58 return preg_replace('@^/forum(.*)@', '/community$1', $path);