3 namespace Drupal\content_moderation\ParamConverter;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\ParamConverter\EntityConverter;
8 use Drupal\Core\TypedData\TranslatableInterface;
9 use Drupal\content_moderation\ModerationInformationInterface;
10 use Symfony\Component\Routing\Route;
13 * Defines a class for making sure the edit-route loads the current draft.
15 class EntityRevisionConverter extends EntityConverter {
18 * Moderation information service.
20 * @var \Drupal\content_moderation\ModerationInformationInterface
22 protected $moderationInformation;
25 * EntityRevisionConverter constructor.
27 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
28 * The entity manager, needed by the parent class.
29 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
30 * The moderation info utility service.
32 public function __construct(EntityManagerInterface $entity_manager, ModerationInformationInterface $moderation_info) {
33 parent::__construct($entity_manager);
34 $this->moderationInformation = $moderation_info;
40 public function applies($definition, $name, Route $route) {
41 return $this->hasPendingRevisionFlag($definition) || $this->isEditFormPage($route);
45 * Determines if the route definition includes a pending revision flag.
47 * This is a custom flag defined by the Content Moderation module to load
48 * pending revisions rather than the default revision on a given route.
50 * @param array $definition
51 * The parameter definition provided in the route options.
54 * TRUE if the pending revision flag is set, FALSE otherwise.
56 protected function hasPendingRevisionFlag(array $definition) {
57 return (isset($definition['load_pending_revision']) && $definition['load_pending_revision']);
61 * Determines if a given route is the edit-form for an entity.
63 * @param \Symfony\Component\Routing\Route $route
64 * The route definition.
67 * Returns TRUE if the route is the edit form of an entity, FALSE otherwise.
69 protected function isEditFormPage(Route $route) {
70 if ($default = $route->getDefault('_entity_form')) {
71 // If no operation is provided, use 'default'.
72 $default .= '.default';
73 list($entity_type_id, $operation) = explode('.', $default);
74 if (!$this->entityManager->hasDefinition($entity_type_id)) {
77 $entity_type = $this->entityManager->getDefinition($entity_type_id);
78 return $operation == 'edit' && $entity_type && $entity_type->isRevisionable();
85 public function convert($value, $definition, $name, array $defaults) {
86 $entity = parent::convert($value, $definition, $name, $defaults);
88 if ($entity && $this->moderationInformation->isModeratedEntity($entity) && !$this->moderationInformation->isLatestRevision($entity)) {
89 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
90 $latest_revision = $this->moderationInformation->getLatestRevision($entity_type_id, $value);
92 if ($latest_revision instanceof EntityInterface) {
93 // If the entity type is translatable, ensure we return the proper
94 // translation object for the current context.
95 if ($entity instanceof TranslatableInterface) {
96 $latest_revision = $this->entityManager->getTranslationFromContext($latest_revision, NULL, ['operation' => 'entity_upcast']);
98 $entity = $latest_revision;