function EntityOperations::entityTranslationInsert

Same name in other branches
  1. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityTranslationInsert()

Acts after an entity translation has been added.

Parameters

\Drupal\Core\Entity\EntityInterface $translation: The translation that was added.

See also

hook_entity_translation_insert()

File

core/modules/workspaces/src/EntityOperations.php, line 237

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

Code

public function entityTranslationInsert(EntityInterface $translation) : void {
    if ($this->shouldSkipOperations($translation) || !$this->workspaceInfo
        ->isEntitySupported($translation) || $translation->isSyncing()) {
        return;
    }
    // When a new translation is added to an existing entity, we need to add
    // that translation to the default revision as well, otherwise the new
    // translation wouldn't show up in entity queries or views which use the
    // field data table as the base table.
    $this->workspaceManager
        ->executeOutsideWorkspace(function () use ($translation) {
        $storage = $this->entityTypeManager
            ->getStorage($translation->getEntityTypeId());
        $default_revision = $storage->load($translation->id());
        $langcode = $translation->language()
            ->getId();
        if (!$default_revision->hasTranslation($langcode)) {
            $default_revision_translation = $default_revision->addTranslation($langcode, $translation->toArray());
            $default_revision_translation->setUnpublished();
            $default_revision_translation->setSyncing(TRUE);
            $default_revision_translation->save();
        }
    });
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.