comparison core/modules/content_moderation/src/EntityOperations.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
3 namespace Drupal\content_moderation; 3 namespace Drupal\content_moderation;
4 4
5 use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity; 5 use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity;
6 use Drupal\content_moderation\Entity\ContentModerationStateInterface; 6 use Drupal\content_moderation\Entity\ContentModerationStateInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Entity\ContentEntityInterface;
8 use Drupal\Core\Entity\Display\EntityViewDisplayInterface; 9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
9 use Drupal\Core\Entity\EntityInterface; 10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Entity\EntityPublishedInterface;
10 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; 12 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
11 use Drupal\Core\Entity\EntityTypeManagerInterface; 13 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 use Drupal\Core\Form\FormBuilderInterface; 14 use Drupal\Core\Form\FormBuilderInterface;
13 use Drupal\content_moderation\Form\EntityModerationForm; 15 use Drupal\content_moderation\Form\EntityModerationForm;
14 use Drupal\Core\Routing\RouteBuilderInterface; 16 use Drupal\Core\Routing\RouteBuilderInterface;
180 } 182 }
181 183
182 // Sync translations. 184 // Sync translations.
183 if ($entity->getEntityType()->hasKey('langcode')) { 185 if ($entity->getEntityType()->hasKey('langcode')) {
184 $entity_langcode = $entity->language()->getId(); 186 $entity_langcode = $entity->language()->getId();
185 if (!$content_moderation_state->hasTranslation($entity_langcode)) { 187 if ($entity->isDefaultTranslation()) {
186 $content_moderation_state->addTranslation($entity_langcode); 188 $content_moderation_state->langcode = $entity_langcode;
187 } 189 }
188 if ($content_moderation_state->language()->getId() !== $entity_langcode) { 190 else {
189 $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); 191 if (!$content_moderation_state->hasTranslation($entity_langcode)) {
192 $content_moderation_state->addTranslation($entity_langcode);
193 }
194 if ($content_moderation_state->language()->getId() !== $entity_langcode) {
195 $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
196 }
190 } 197 }
191 } 198 }
192 199
193 // If a new revision of the content has been created, add a new content 200 // If a new revision of the content has been created, add a new content
194 // moderation state revision. 201 // moderation state revision.
276 return; 283 return;
277 } 284 }
278 // The moderation form should be displayed only when viewing the latest 285 // The moderation form should be displayed only when viewing the latest
279 // (translation-affecting) revision, unless it was created as published 286 // (translation-affecting) revision, unless it was created as published
280 // default revision. 287 // default revision.
288 if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && $this->isPublished($entity)) {
289 return;
290 }
281 if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) { 291 if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
282 return; 292 return;
283 } 293 }
284 if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && ($moderation_state = $entity->get('moderation_state')->value)) { 294
295 $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
296 }
297
298 /**
299 * Checks if the entity is published.
300 *
301 * This method is optimized to not have to unnecessarily load the moderation
302 * state and workflow if it is not required.
303 *
304 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
305 * The entity to check.
306 *
307 * @return bool
308 * TRUE if the entity is published, FALSE otherwise.
309 */
310 protected function isPublished(ContentEntityInterface $entity) {
311 // If the entity implements EntityPublishedInterface directly, check that
312 // first, otherwise fall back to check through the workflow state.
313 if ($entity instanceof EntityPublishedInterface) {
314 return $entity->isPublished();
315 }
316 if ($moderation_state = $entity->get('moderation_state')->value) {
285 $workflow = $this->moderationInfo->getWorkflowForEntity($entity); 317 $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
286 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) { 318 return $workflow->getTypePlugin()->getState($moderation_state)->isPublishedState();
287 return; 319 }
288 } 320 return FALSE;
289 }
290
291 $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
292 } 321 }
293 322
294 } 323 }