Pull merge.
[yaffs-website] / web / core / modules / field_ui / src / Routing / FieldUiRouteEnhancer.php
1 <?php
2
3 namespace Drupal\field_ui\Routing;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Routing\EnhancerInterface;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Routing\Route;
10
11 /**
12  * Enhances Field UI routes by adding proper information about the bundle name.
13  */
14 class FieldUiRouteEnhancer implements EnhancerInterface {
15
16   /**
17    * The entity manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * Constructs a FieldUiRouteEnhancer object.
25    *
26    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
27    *   The entity manager.
28    */
29   public function __construct(EntityManagerInterface $entity_manager) {
30     $this->entityManager = $entity_manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function enhance(array $defaults, Request $request) {
37     if (!$this->applies($defaults[RouteObjectInterface::ROUTE_OBJECT])) {
38       return $defaults;
39     }
40
41     if (($bundle = $this->entityManager->getDefinition($defaults['entity_type_id'])->getBundleEntityType()) && isset($defaults[$bundle])) {
42       // Field UI forms only need the actual name of the bundle they're dealing
43       // with, not an upcasted entity object, so provide a simple way for them
44       // to get it.
45       $defaults['bundle'] = $defaults['_raw_variables']->get($bundle);
46     }
47
48     return $defaults;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function applies(Route $route) {
55     return ($route->hasOption('_field_ui'));
56   }
57
58 }