3 namespace Drupal\Core\ParamConverter;
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\Routing\Route;
10 * Parameter converter for upcasting entity revision IDs to full objects.
12 * This is useful for pages which want to show a specific revision, like
13 * "/entity_example/{entity_example}/revision/{entity_example_revision}".
16 * In order to use it you should specify some additional options in your route:
19 * path: /foo/{entity_example_revision}
22 * entity_example_revision:
23 * type: entity_revision:entity_example
26 class EntityRevisionParamConverter implements ParamConverterInterface {
29 * The entity type manager.
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
33 protected $entityTypeManager;
36 * The entity repository.
38 * @var \Drupal\Core\Entity\EntityRepositoryInterface
40 protected $entityRepository;
43 * Creates a new EntityRevisionParamConverter instance.
45 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
46 * The entity type manager.
47 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
48 * The entity repository.
50 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
51 $this->entityTypeManager = $entity_type_manager;
52 $this->entityRepository = $entity_repository;
58 public function convert($value, $definition, $name, array $defaults) {
59 list (, $entity_type_id) = explode(':', $definition['type'], 2);
60 $entity = $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($value);
61 return $this->entityRepository->getTranslationFromContext($entity);
67 public function applies($definition, $name, Route $route) {
68 return isset($definition['type']) && strpos($definition['type'], 'entity_revision:') !== FALSE;