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