/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $value->getEntity(); // Ignore entities that are not subject to moderation anyway. if (!$this->moderationInformation->isModeratedEntity($entity)) { return; } // Ignore entities that are being created for the first time. if ($entity->isNew()) { return; } // Ignore entities that are being moderated for the first time, such as // when they existed before moderation was enabled for this entity type. if ($this->isFirstTimeModeration($entity)) { return; } $original_entity = $this->moderationInformation->getLatestRevision($entity->getEntityTypeId(), $entity->id()); if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { $original_entity = $original_entity->getTranslation($entity->language()->getId()); } if ($entity->moderation_state->target_id) { $new_state_id = $entity->moderation_state->target_id; } else { $new_state_id = $default = $this->moderationInformation->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())->getThirdPartySetting('content_moderation', 'default_moderation_state'); } if ($new_state_id) { $new_state = ModerationStateEntity::load($new_state_id); } // @todo - what if $new_state_id references something that does not exist or // is null. if (!$this->validation->isTransitionAllowed($original_entity->moderation_state->entity, $new_state)) { $this->context->addViolation($constraint->message, ['%from' => $original_entity->moderation_state->entity->label(), '%to' => $new_state->label()]); } }
/** * Creates or updates the moderation state of an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to update or create a moderation state for. */ protected function updateOrCreateFromEntity(EntityInterface $entity) { $moderation_state = $entity->moderation_state->target_id; /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if (!$moderation_state) { $moderation_state = $this->moderationInfo->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())->getThirdPartySetting('content_moderation', 'default_moderation_state'); } // @todo what if $entity->moderation_state->target_id is null at this point? $entity_type_id = $entity->getEntityTypeId(); $entity_id = $entity->id(); $entity_revision_id = $entity->getRevisionId(); $entity_langcode = $entity->language()->getId(); $storage = $this->entityTypeManager->getStorage('content_moderation_state'); $entities = $storage->loadByProperties(['content_entity_type_id' => $entity_type_id, 'content_entity_id' => $entity_id]); /** @var \Drupal\content_moderation\ContentModerationStateInterface $content_moderation_state */ $content_moderation_state = reset($entities); if (!$content_moderation_state instanceof ContentModerationStateInterface) { $content_moderation_state = $storage->create(['content_entity_type_id' => $entity_type_id, 'content_entity_id' => $entity_id]); } else { // Create a new revision. $content_moderation_state->setNewRevision(TRUE); } // Sync translations. if (!$content_moderation_state->hasTranslation($entity_langcode)) { $content_moderation_state->addTranslation($entity_langcode); } if ($content_moderation_state->language()->getId() !== $entity_langcode) { $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); } // Create the ContentModerationState entity for the inserted entity. $content_moderation_state->set('content_entity_revision_id', $entity_revision_id); $content_moderation_state->set('moderation_state', $moderation_state); ContentModerationState::updateOrCreateFromEntity($content_moderation_state); }