Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ParamConverter;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Routing\AdminContext;
|
Chris@0
|
9 use Symfony\Component\Routing\Route;
|
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@18
|
45 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
46 * The entity type 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@18
|
51 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@18
|
52 * The entity repository.
|
Chris@0
|
53 */
|
Chris@18
|
54 public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, AdminContext $admin_context, $entity_repository = NULL) {
|
Chris@18
|
55 parent::__construct($entity_type_manager, $entity_repository);
|
Chris@0
|
56
|
Chris@0
|
57 $this->configFactory = $config_factory;
|
Chris@0
|
58 $this->adminContext = $admin_context;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function convert($value, $definition, $name, array $defaults) {
|
Chris@0
|
65 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
|
Chris@0
|
66
|
Chris@0
|
67 // If the entity type is dynamic, confirm it to be a config entity. Static
|
Chris@0
|
68 // entity types will have performed this check in self::applies().
|
Chris@0
|
69 if (strpos($definition['type'], 'entity:{') === 0) {
|
Chris@18
|
70 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
|
Chris@0
|
71 if (!$entity_type->entityClassImplements(ConfigEntityInterface::class)) {
|
Chris@0
|
72 return parent::convert($value, $definition, $name, $defaults);
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@18
|
76 if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
|
Chris@0
|
77 // Make sure no overrides are loaded.
|
Chris@0
|
78 return $storage->loadOverrideFree($value);
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function applies($definition, $name, Route $route) {
|
Chris@0
|
86 if (isset($definition['with_config_overrides']) && $definition['with_config_overrides']) {
|
Chris@0
|
87 return FALSE;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 if (parent::applies($definition, $name, $route)) {
|
Chris@0
|
91 $entity_type_id = substr($definition['type'], strlen('entity:'));
|
Chris@0
|
92 // If the entity type is dynamic, defer checking to self::convert().
|
Chris@0
|
93 if (strpos($entity_type_id, '{') === 0) {
|
Chris@0
|
94 return TRUE;
|
Chris@0
|
95 }
|
Chris@0
|
96 // As we only want to override EntityConverter for ConfigEntities, find
|
Chris@0
|
97 // out whether the current entity is a ConfigEntity.
|
Chris@18
|
98 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
|
Chris@0
|
99 if ($entity_type->entityClassImplements(ConfigEntityInterface::class)) {
|
Chris@0
|
100 return $this->adminContext->isAdminRoute($route);
|
Chris@0
|
101 }
|
Chris@0
|
102 }
|
Chris@0
|
103 return FALSE;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 }
|