Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / modules / url_alter_test / src / PathProcessorTest.php
1 <?php
2
3 namespace Drupal\url_alter_test;
4
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;
10
11 /**
12  * Path processor for url_alter_test.
13  */
14 class PathProcessorTest implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
15
16   /**
17    * {@inheritdoc}
18    */
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];
25       }
26     }
27
28     // Rewrite community/ to forum/.
29     $path = preg_replace('@^/community(.*)@', '/forum$1', $path);
30
31     if ($path == '/url-alter-test/bar') {
32       $path = '/url-alter-test/foo';
33     }
34     return $path;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
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());
48         }
49       }
50     }
51
52     // Verify that $options are alterable.
53     if ($path == '/user/login') {
54       $options['query']['foo'] = 'bar';
55     }
56
57     // Rewrite forum/ to community/.
58     return preg_replace('@^/forum(.*)@', '/community$1', $path);
59   }
60
61 }