['entity' => $entity, 'langcode' => $langcode] = $values;
// Validate that the entity is a content entity.
if (!($entity instanceof ContentEntityInterface)) {
return ExecutableResult::failure($this->t('The provided entity must be a content entity.'));
}
// Validate that the language code exists.
$language = $this->languageManager->getLanguage($langcode);
if (!$language) {
return ExecutableResult::failure($this->t('Invalid language code "@langcode".', ['@langcode' => $langcode]));
}
// Check if the entity type supports translation.
if (!$this->contentTranslationManager->isEnabled($entity->getEntityTypeId(), $entity->bundle())) {
return ExecutableResult::failure($this->t('Translation is not enabled for @type @bundle entities.', [
'@type' => $entity->getEntityTypeId(),
'@bundle' => $entity->bundle(),
]));
}
try {
// Check if translation already exists.
if ($entity->hasTranslation($langcode)) {
$translated_entity = $entity->getTranslation($langcode);
return ExecutableResult::success($this->t('Successfully loaded existing @langcode translation of @type entity @id.', [
'@langcode' => $langcode,
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
]), ['translated_entity' => $translated_entity]);
}
// Create a new translation.
$translated_entity = $entity->addTranslation($langcode, $entity->toArray());
// Save the entity to persist the new translation.
$translated_entity->save();
return ExecutableResult::success($this->t('Successfully created and loaded new @langcode translation of @type entity @id.', [
'@langcode' => $langcode,
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
]), ['translated_entity' => $translated_entity]);
} catch (\Exception $e) {
return ExecutableResult::failure($this->t('Error processing translation for @type entity @id: @message', [
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
'@message' => $e->getMessage(),
]));
}
TBD.
Active
1.0
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.