Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Routing / Enhancer / EntityRevisionRouteEnhancer.php
1 <?php
2
3 namespace Drupal\Core\Routing\Enhancer;
4
5 use Drupal\Core\Routing\EnhancerInterface;
6 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Adds _entity_revision to the request attributes, if possible.
12  */
13 class EntityRevisionRouteEnhancer implements EnhancerInterface {
14
15   /**
16    * Returns whether the enhancer runs on the current route.
17    *
18    * @param \Symfony\Component\Routing\Route $route
19    *   The current route.
20    *
21    * @return bool
22    */
23   protected function applies(Route $route) {
24     // Check whether there is any entity revision parameter.
25     $parameters = $route->getOption('parameters') ?: [];
26     foreach ($parameters as $info) {
27       if (isset($info['type']) && strpos($info['type'], 'entity_revision:') === 0) {
28         return TRUE;
29       }
30     }
31     return FALSE;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function enhance(array $defaults, Request $request) {
38     /** @var \Symfony\Component\Routing\Route $route */
39     $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
40     if (!$this->applies($route)) {
41       return $defaults;
42     }
43
44     $options = $route->getOptions();
45     if (isset($options['parameters'])) {
46       foreach ($options['parameters'] as $name => $details) {
47         if (!empty($details['type']) && strpos($details['type'], 'entity_revision:') !== FALSE) {
48           $defaults['_entity_revision'] = $defaults[$name];
49           break;
50         }
51       }
52     }
53
54     return $defaults;
55   }
56
57 }