561c0c047cf0a491fd9d135f6d81abae8e6d660c
[yaffs-website] / web / core / modules / content_moderation / src / ParamConverter / EntityRevisionConverter.php
1 <?php
2
3 namespace Drupal\content_moderation\ParamConverter;
4
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;
11
12 /**
13  * Defines a class for making sure the edit-route loads the current draft.
14  */
15 class EntityRevisionConverter extends EntityConverter {
16
17   /**
18    * Moderation information service.
19    *
20    * @var \Drupal\content_moderation\ModerationInformationInterface
21    */
22   protected $moderationInformation;
23
24   /**
25    * EntityRevisionConverter constructor.
26    *
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.
31    */
32   public function __construct(EntityManagerInterface $entity_manager, ModerationInformationInterface $moderation_info) {
33     parent::__construct($entity_manager);
34     $this->moderationInformation = $moderation_info;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function applies($definition, $name, Route $route) {
41     return $this->hasPendingRevisionFlag($definition) || $this->isEditFormPage($route);
42   }
43
44   /**
45    * Determines if the route definition includes a pending revision flag.
46    *
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.
49    *
50    * @param array $definition
51    *   The parameter definition provided in the route options.
52    *
53    * @return bool
54    *   TRUE if the pending revision flag is set, FALSE otherwise.
55    */
56   protected function hasPendingRevisionFlag(array $definition) {
57     return (isset($definition['load_pending_revision']) && $definition['load_pending_revision']);
58   }
59
60   /**
61    * Determines if a given route is the edit-form for an entity.
62    *
63    * @param \Symfony\Component\Routing\Route $route
64    *   The route definition.
65    *
66    * @return bool
67    *   Returns TRUE if the route is the edit form of an entity, FALSE otherwise.
68    */
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)) {
75         return FALSE;
76       }
77       $entity_type = $this->entityManager->getDefinition($entity_type_id);
78       return $operation == 'edit' && $entity_type && $entity_type->isRevisionable();
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function convert($value, $definition, $name, array $defaults) {
86     $entity = parent::convert($value, $definition, $name, $defaults);
87
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);
91
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']);
97         }
98         $entity = $latest_revision;
99       }
100     }
101
102     return $entity;
103   }
104
105 }