Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\ParamConverter;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
7 use Symfony\Component\Routing\Route;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Parameter converter for upcasting entity revision IDs to full objects.
|
Chris@0
|
11 *
|
Chris@0
|
12 * This is useful for pages which want to show a specific revision, like
|
Chris@0
|
13 * "/entity_example/{entity_example}/revision/{entity_example_revision}".
|
Chris@0
|
14 *
|
Chris@0
|
15 *
|
Chris@0
|
16 * In order to use it you should specify some additional options in your route:
|
Chris@0
|
17 * @code
|
Chris@0
|
18 * example.route:
|
Chris@0
|
19 * path: /foo/{entity_example_revision}
|
Chris@0
|
20 * options:
|
Chris@0
|
21 * parameters:
|
Chris@0
|
22 * entity_example_revision:
|
Chris@0
|
23 * type: entity_revision:entity_example
|
Chris@0
|
24 * @endcode
|
Chris@0
|
25 */
|
Chris@0
|
26 class EntityRevisionParamConverter implements ParamConverterInterface {
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * The entity type manager.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $entityTypeManager;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * The entity repository.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var \Drupal\Core\Entity\EntityRepositoryInterface
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $entityRepository;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Creates a new EntityRevisionParamConverter instance.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@0
|
46 * The entity type manager.
|
Chris@0
|
47 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@0
|
48 * The entity repository.
|
Chris@0
|
49 */
|
Chris@0
|
50 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
|
Chris@0
|
51 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
52 $this->entityRepository = $entity_repository;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 public function convert($value, $definition, $name, array $defaults) {
|
Chris@0
|
59 list (, $entity_type_id) = explode(':', $definition['type'], 2);
|
Chris@0
|
60 $entity = $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($value);
|
Chris@0
|
61 return $this->entityRepository->getTranslationFromContext($entity);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public function applies($definition, $name, Route $route) {
|
Chris@0
|
68 return isset($definition['type']) && strpos($definition['type'], 'entity_revision:') !== FALSE;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 }
|