Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ParamConverter;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
Chris@0
|
6 use Drupal\Core\Routing\AdminContext;
|
Chris@0
|
7 use Symfony\Component\Routing\Route;
|
Chris@0
|
8 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Makes sure the unmodified ConfigEntity is loaded on admin pages.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Converts entity route arguments to unmodified entities as opposed to
|
Chris@0
|
15 * converting to entities with overrides, such as the negotiated language.
|
Chris@0
|
16 *
|
Chris@0
|
17 * This converter applies only if the path is an admin path, the entity is
|
Chris@0
|
18 * a config entity, and the "with_config_overrides" element is not set to TRUE
|
Chris@0
|
19 * on the parameter definition.
|
Chris@0
|
20 *
|
Chris@0
|
21 * Due to this converter having a higher weight than the default
|
Chris@0
|
22 * EntityConverter, every time this applies, it takes over the conversion duty
|
Chris@0
|
23 * from EntityConverter. As we only allow a single converter per route
|
Chris@0
|
24 * argument, EntityConverter is ignored when this converter applies.
|
Chris@0
|
25 */
|
Chris@0
|
26 class AdminPathConfigEntityConverter extends EntityConverter {
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * The config factory.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $configFactory;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * The route admin context to determine whether a route is an admin one.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var \Drupal\Core\Routing\AdminContext
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $adminContext;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Constructs a new EntityConverter.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
46 * The entity manager.
|
Chris@0
|
47 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
48 * The config factory.
|
Chris@0
|
49 * @param \Drupal\Core\Routing\AdminContext $admin_context
|
Chris@0
|
50 * The route admin context service.
|
Chris@0
|
51 */
|
Chris@0
|
52 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, AdminContext $admin_context) {
|
Chris@0
|
53 parent::__construct($entity_manager);
|
Chris@0
|
54
|
Chris@0
|
55 $this->configFactory = $config_factory;
|
Chris@0
|
56 $this->adminContext = $admin_context;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * {@inheritdoc}
|
Chris@0
|
61 */
|
Chris@0
|
62 public function convert($value, $definition, $name, array $defaults) {
|
Chris@0
|
63 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
|
Chris@0
|
64
|
Chris@0
|
65 // If the entity type is dynamic, confirm it to be a config entity. Static
|
Chris@0
|
66 // entity types will have performed this check in self::applies().
|
Chris@0
|
67 if (strpos($definition['type'], 'entity:{') === 0) {
|
Chris@0
|
68 $entity_type = $this->entityManager->getDefinition($entity_type_id);
|
Chris@0
|
69 if (!$entity_type->entityClassImplements(ConfigEntityInterface::class)) {
|
Chris@0
|
70 return parent::convert($value, $definition, $name, $defaults);
|
Chris@0
|
71 }
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 if ($storage = $this->entityManager->getStorage($entity_type_id)) {
|
Chris@0
|
75 // Make sure no overrides are loaded.
|
Chris@0
|
76 return $storage->loadOverrideFree($value);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * {@inheritdoc}
|
Chris@0
|
82 */
|
Chris@0
|
83 public function applies($definition, $name, Route $route) {
|
Chris@0
|
84 if (isset($definition['with_config_overrides']) && $definition['with_config_overrides']) {
|
Chris@0
|
85 return FALSE;
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 if (parent::applies($definition, $name, $route)) {
|
Chris@0
|
89 $entity_type_id = substr($definition['type'], strlen('entity:'));
|
Chris@0
|
90 // If the entity type is dynamic, defer checking to self::convert().
|
Chris@0
|
91 if (strpos($entity_type_id, '{') === 0) {
|
Chris@0
|
92 return TRUE;
|
Chris@0
|
93 }
|
Chris@0
|
94 // As we only want to override EntityConverter for ConfigEntities, find
|
Chris@0
|
95 // out whether the current entity is a ConfigEntity.
|
Chris@0
|
96 $entity_type = $this->entityManager->getDefinition($entity_type_id);
|
Chris@0
|
97 if ($entity_type->entityClassImplements(ConfigEntityInterface::class)) {
|
Chris@0
|
98 return $this->adminContext->isAdminRoute($route);
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 return FALSE;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 }
|