Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / content_moderation / src / EntityOperations.php
1 <?php
2
3 namespace Drupal\content_moderation;
4
5 use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity;
6 use Drupal\content_moderation\Entity\ContentModerationStateInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Entity\ContentEntityInterface;
9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Entity\EntityPublishedInterface;
12 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14 use Drupal\Core\Form\FormBuilderInterface;
15 use Drupal\content_moderation\Form\EntityModerationForm;
16 use Drupal\Core\Routing\RouteBuilderInterface;
17 use Drupal\workflows\Entity\Workflow;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19
20 /**
21  * Defines a class for reacting to entity events.
22  *
23  * @internal
24  */
25 class EntityOperations implements ContainerInjectionInterface {
26
27   /**
28    * The Moderation Information service.
29    *
30    * @var \Drupal\content_moderation\ModerationInformationInterface
31    */
32   protected $moderationInfo;
33
34   /**
35    * The Entity Type Manager service.
36    *
37    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
38    */
39   protected $entityTypeManager;
40
41   /**
42    * The Form Builder service.
43    *
44    * @var \Drupal\Core\Form\FormBuilderInterface
45    */
46   protected $formBuilder;
47
48   /**
49    * The entity bundle information service.
50    *
51    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
52    */
53   protected $bundleInfo;
54
55   /**
56    * The router builder service.
57    *
58    * @var \Drupal\Core\Routing\RouteBuilderInterface
59    */
60   protected $routerBuilder;
61
62   /**
63    * Constructs a new EntityOperations object.
64    *
65    * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
66    *   Moderation information service.
67    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
68    *   Entity type manager service.
69    * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
70    *   The form builder.
71    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
72    *   The entity bundle information service.
73    * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
74    *   The router builder service.
75    */
76   public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityTypeBundleInfoInterface $bundle_info, RouteBuilderInterface $router_builder) {
77     $this->moderationInfo = $moderation_info;
78     $this->entityTypeManager = $entity_type_manager;
79     $this->formBuilder = $form_builder;
80     $this->bundleInfo = $bundle_info;
81     $this->routerBuilder = $router_builder;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public static function create(ContainerInterface $container) {
88     return new static(
89       $container->get('content_moderation.moderation_information'),
90       $container->get('entity_type.manager'),
91       $container->get('form_builder'),
92       $container->get('entity_type.bundle.info'),
93       $container->get('router.builder')
94     );
95   }
96
97   /**
98    * Acts on an entity and set published status based on the moderation state.
99    *
100    * @param \Drupal\Core\Entity\EntityInterface $entity
101    *   The entity being saved.
102    *
103    * @see hook_entity_presave()
104    */
105   public function entityPresave(EntityInterface $entity) {
106     if (!$this->moderationInfo->isModeratedEntity($entity)) {
107       return;
108     }
109
110     if ($entity->moderation_state->value) {
111       $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
112       /** @var \Drupal\content_moderation\ContentModerationState $current_state */
113       $current_state = $workflow->getTypePlugin()
114         ->getState($entity->moderation_state->value);
115
116       // This entity is default if it is new, the default revision, or the
117       // default revision is not published.
118       $update_default_revision = $entity->isNew()
119         || $current_state->isDefaultRevisionState()
120         || !$this->moderationInfo->isDefaultRevisionPublished($entity);
121
122       // Fire per-entity-type logic for handling the save process.
123       $this->entityTypeManager
124         ->getHandler($entity->getEntityTypeId(), 'moderation')
125         ->onPresave($entity, $update_default_revision, $current_state->isPublishedState());
126     }
127   }
128
129   /**
130    * @param \Drupal\Core\Entity\EntityInterface $entity
131    *   The entity that was just saved.
132    *
133    * @see hook_entity_insert()
134    */
135   public function entityInsert(EntityInterface $entity) {
136     if ($this->moderationInfo->isModeratedEntity($entity)) {
137       $this->updateOrCreateFromEntity($entity);
138     }
139   }
140
141   /**
142    * @param \Drupal\Core\Entity\EntityInterface $entity
143    *   The entity that was just saved.
144    *
145    * @see hook_entity_update()
146    */
147   public function entityUpdate(EntityInterface $entity) {
148     if ($this->moderationInfo->isModeratedEntity($entity)) {
149       $this->updateOrCreateFromEntity($entity);
150     }
151     // When updating workflow settings for Content Moderation, we need to
152     // rebuild routes as we may be enabling new entity types and the related
153     // entity forms.
154     elseif ($entity instanceof Workflow && $entity->getTypePlugin()->getPluginId() == 'content_moderation') {
155       $this->routerBuilder->setRebuildNeeded();
156     }
157   }
158
159   /**
160    * Creates or updates the moderation state of an entity.
161    *
162    * @param \Drupal\Core\Entity\EntityInterface $entity
163    *   The entity to update or create a moderation state for.
164    */
165   protected function updateOrCreateFromEntity(EntityInterface $entity) {
166     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
167     $entity_revision_id = $entity->getRevisionId();
168     $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
169     $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
170     /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
171     $storage = $this->entityTypeManager->getStorage('content_moderation_state');
172
173     if (!($content_moderation_state instanceof ContentModerationStateInterface)) {
174       $content_moderation_state = $storage->create([
175         'content_entity_type_id' => $entity->getEntityTypeId(),
176         'content_entity_id' => $entity->id(),
177         // Make sure that the moderation state entity has the same language code
178         // as the moderated entity.
179         'langcode' => $entity->language()->getId(),
180       ]);
181       $content_moderation_state->workflow->target_id = $workflow->id();
182     }
183
184     // Sync translations.
185     if ($entity->getEntityType()->hasKey('langcode')) {
186       $entity_langcode = $entity->language()->getId();
187       if (!$content_moderation_state->hasTranslation($entity_langcode)) {
188         $content_moderation_state->addTranslation($entity_langcode);
189       }
190       if ($content_moderation_state->language()->getId() !== $entity_langcode) {
191         $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
192       }
193     }
194
195     // If a new revision of the content has been created, add a new content
196     // moderation state revision.
197     if (!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
198       $content_moderation_state = $storage->createRevision($content_moderation_state, $entity->isDefaultRevision());
199     }
200
201     // Create the ContentModerationState entity for the inserted entity.
202     $moderation_state = $entity->moderation_state->value;
203     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
204     if (!$moderation_state) {
205       $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id();
206     }
207
208     $content_moderation_state->set('content_entity_revision_id', $entity_revision_id);
209     $content_moderation_state->set('moderation_state', $moderation_state);
210     ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
211   }
212
213   /**
214    * @param \Drupal\Core\Entity\EntityInterface $entity
215    *   The entity being deleted.
216    *
217    * @see hook_entity_delete()
218    */
219   public function entityDelete(EntityInterface $entity) {
220     $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
221     if ($content_moderation_state) {
222       $content_moderation_state->delete();
223     }
224   }
225
226   /**
227    * @param \Drupal\Core\Entity\EntityInterface $entity
228    *   The entity revision being deleted.
229    *
230    * @see hook_entity_revision_delete()
231    */
232   public function entityRevisionDelete(EntityInterface $entity) {
233     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
234     if (!$entity->isDefaultRevision()) {
235       $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
236       if ($content_moderation_state) {
237         $this->entityTypeManager
238           ->getStorage('content_moderation_state')
239           ->deleteRevision($content_moderation_state->getRevisionId());
240       }
241     }
242   }
243
244   /**
245    * @param \Drupal\Core\Entity\EntityInterface $translation
246    *   The entity translation being deleted.
247    *
248    * @see hook_entity_translation_delete()
249    */
250   public function entityTranslationDelete(EntityInterface $translation) {
251     /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
252     if (!$translation->isDefaultTranslation()) {
253       $langcode = $translation->language()->getId();
254       $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($translation);
255       if ($content_moderation_state && $content_moderation_state->hasTranslation($langcode)) {
256         $content_moderation_state->removeTranslation($langcode);
257         ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
258       }
259     }
260   }
261
262   /**
263    * Act on entities being assembled before rendering.
264    *
265    * @see hook_entity_view()
266    * @see EntityFieldManagerInterface::getExtraFields()
267    */
268   public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
269     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
270     if (!$this->moderationInfo->isModeratedEntity($entity)) {
271       return;
272     }
273     if (isset($entity->in_preview) && $entity->in_preview) {
274       return;
275     }
276     // If the component is not defined for this display, we have nothing to do.
277     if (!$display->getComponent('content_moderation_control')) {
278       return;
279     }
280     // The moderation form should be displayed only when viewing the latest
281     // (translation-affecting) revision, unless it was created as published
282     // default revision.
283     if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && $this->isPublished($entity)) {
284       return;
285     }
286     if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
287       return;
288     }
289
290     $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
291   }
292
293   /**
294    * Checks if the entity is published.
295    *
296    * This method is optimized to not have to unnecessarily load the moderation
297    * state and workflow if it is not required.
298    *
299    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
300    *   The entity to check.
301    *
302    * @return bool
303    *   TRUE if the entity is published, FALSE otherwise.
304    */
305   protected function isPublished(ContentEntityInterface $entity) {
306     // If the entity implements EntityPublishedInterface directly, check that
307     // first, otherwise fall back to check through the workflow state.
308     if ($entity instanceof EntityPublishedInterface) {
309       return $entity->isPublished();
310     }
311     if ($moderation_state = $entity->get('moderation_state')->value) {
312       $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
313       return $workflow->getTypePlugin()->getState($moderation_state)->isPublishedState();
314     }
315     return FALSE;
316   }
317
318 }