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 * @todo: If the parent class is ever cleaned up to use EntityTypeManager
33 * instead of Entity manager, this method will also need to be adjusted.
35 public function __construct(EntityManagerInterface $entity_manager, ModerationInformationInterface $moderation_info) {
36 parent::__construct($entity_manager);
37 $this->moderationInformation = $moderation_info;
43 public function applies($definition, $name, Route $route) {
44 return $this->hasForwardRevisionFlag($definition) || $this->isEditFormPage($route);
48 * Determines if the route definition includes a forward-revision flag.
50 * This is a custom flag defined by the Content Moderation module to load
51 * forward revisions rather than the default revision on a given route.
53 * @param array $definition
54 * The parameter definition provided in the route options.
57 * TRUE if the forward revision flag is set, FALSE otherwise.
59 protected function hasForwardRevisionFlag(array $definition) {
60 return (isset($definition['load_forward_revision']) && $definition['load_forward_revision']);
64 * Determines if a given route is the edit-form for an entity.
66 * @param \Symfony\Component\Routing\Route $route
67 * The route definition.
70 * Returns TRUE if the route is the edit form of an entity, FALSE otherwise.
72 protected function isEditFormPage(Route $route) {
73 if ($default = $route->getDefault('_entity_form')) {
74 // If no operation is provided, use 'default'.
75 $default .= '.default';
76 list($entity_type_id, $operation) = explode('.', $default);
77 if (!$this->entityManager->hasDefinition($entity_type_id)) {
80 $entity_type = $this->entityManager->getDefinition($entity_type_id);
81 return $operation == 'edit' && $entity_type && $entity_type->isRevisionable();
88 public function convert($value, $definition, $name, array $defaults) {
89 $entity = parent::convert($value, $definition, $name, $defaults);
91 if ($entity && $this->moderationInformation->isModeratedEntity($entity) && !$this->moderationInformation->isLatestRevision($entity)) {
92 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
93 $latest_revision = $this->moderationInformation->getLatestRevision($entity_type_id, $value);
95 // If the entity type is translatable, ensure we return the proper
96 // translation object for the current context.
97 if ($latest_revision instanceof EntityInterface && $entity instanceof TranslatableInterface) {
98 $latest_revision = $this->entityManager->getTranslationFromContext($latest_revision, NULL, ['operation' => 'entity_upcast']);
101 if ($latest_revision instanceof EntityInterface && $latest_revision->isRevisionTranslationAffected()) {
102 $entity = $latest_revision;