AdminPathConfigEntityConverter.php 3.8 KB

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