Same name and namespace in other branches
  1. 2.x src/EntityClone/Content/ContentEntityCloneBase.php \Drupal\entity_clone\EntityClone\Content\ContentEntityCloneBase::setTranslationModerationState()

Create moderation_state translations for the cloned entities.

When a new translation is saved, content moderation creates a corresponding translation to the moderation_state entity as well. However, for this to happen, the translation itself needs to be saved. When we clone, this doesn't happen as the original entity gets cloned together with the translations and a save is called on the original language being cloned. So we have to do this manually.

This is doing essentially what Drupal\content_moderation\EntityOperations::updateOrCreateFromEntity but we had to replicate it because if a user clones a node translation directly, updateOrCreateFromEntity() would not create a translation for the original language but would override the language when passing the original entity translation.

1 call to ContentEntityCloneBase::setTranslationModerationState()
ContentEntityCloneBase::cloneEntity in src/EntityClone/Content/ContentEntityCloneBase.php
Clone an entity.

File

src/EntityClone/Content/ContentEntityCloneBase.php, line 284

Class

ContentEntityCloneBase
Class Content Entity Clone Base.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

protected function setTranslationModerationState(ContentEntityInterface $entity, ContentEntityInterface $cloned_entity) {
    $languages = $cloned_entity->getTranslationLanguages();
    // Load the existing moderation state entity for the cloned entity. This
    // should exist and have only 1 translation.
    $needs_save = FALSE;
    $moderation_state = ContentModerationState::loadFromModeratedEntity($cloned_entity);
    $original_translation = $cloned_entity->getUntranslated();
    if ($moderation_state && $moderation_state->language()
        ->getId() !== $original_translation->language()
        ->getId()) {
        // If we are cloning a node while not being in the original translation
        // language, Drupal core will set the default language of the moderation
        // state to that language whereas the node is simply duplicated and will
        // keep the original default language. So we need to change it to that
        // also in the moderation state to keep things consistent.
        $moderation_state->set($moderation_state->getEntityType()
            ->getKey('langcode'), $original_translation->language()
            ->getId());
        $needs_save = TRUE;
    }
    foreach ($languages as $language) {
        $translation = $cloned_entity->getTranslation($language->getId());
        if ($moderation_state && !$moderation_state->hasTranslation($translation->language()
            ->getId())) {
            // We make a 1 to 1 copy of the moderation state entity from the
            // original created already by the content_moderation module. This is ok
            // because even if translations can be in different moderation states,
            // when cloning, the moderation state is reset to whatever the workflow
            // default is configured to be. So we anyway should end up with the
            // same state across all languages.
            $moderation_state->addTranslation($translation->language()
                ->getId(), $moderation_state->toArray());
            $needs_save = TRUE;
        }
    }
    if ($needs_save) {
        ContentModerationState::updateOrCreateFromEntity($moderation_state);
    }
}