comparison core/lib/Drupal/Core/ParamConverter/EntityRevisionParamConverter.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\ParamConverter;
4
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\Routing\Route;
8
9 /**
10 * Parameter converter for upcasting entity revision IDs to full objects.
11 *
12 * This is useful for pages which want to show a specific revision, like
13 * "/entity_example/{entity_example}/revision/{entity_example_revision}".
14 *
15 *
16 * In order to use it you should specify some additional options in your route:
17 * @code
18 * example.route:
19 * path: /foo/{entity_example_revision}
20 * options:
21 * parameters:
22 * entity_example_revision:
23 * type: entity_revision:entity_example
24 * @endcode
25 */
26 class EntityRevisionParamConverter implements ParamConverterInterface {
27
28 /**
29 * The entity type manager.
30 *
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32 */
33 protected $entityTypeManager;
34
35 /**
36 * The entity repository.
37 *
38 * @var \Drupal\Core\Entity\EntityRepositoryInterface
39 */
40 protected $entityRepository;
41
42 /**
43 * Creates a new EntityRevisionParamConverter instance.
44 *
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.
49 */
50 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
51 $this->entityTypeManager = $entity_type_manager;
52 $this->entityRepository = $entity_repository;
53 }
54
55 /**
56 * {@inheritdoc}
57 */
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);
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function applies($definition, $name, Route $route) {
68 return isset($definition['type']) && strpos($definition['type'], 'entity_revision:') !== FALSE;
69 }
70
71 }