function EntityOperations::entityTranslationInsert

Same name in this branch
  1. 11.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityTranslationInsert()
Same name and namespace in other branches
  1. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityTranslationInsert()

Implements hook_entity_translation_insert().

Attributes

#[Hook('entity_translation_insert')]

File

core/modules/workspaces/src/Hook/EntityOperations.php, line 199

Class

EntityOperations
Defines a class for reacting to entity runtime hooks.

Namespace

Drupal\workspaces\Hook

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.
  $default_revision = $this->workspaceManager
    ->executeOutsideWorkspace(function () use ($translation) {
    return $this->entityTypeManager
      ->getStorage($translation->getEntityTypeId())
      ->load($translation->id());
  });
  $langcode = $translation->language()
    ->getId();
  if (!$default_revision->hasTranslation($langcode)) {
    $default_revision_translation = $default_revision->addTranslation($langcode, $translation->toArray());
    assert($default_revision_translation instanceof EntityPublishedInterface);
    $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.