function lingotek_entity_insert in Lingotek Translation 8
Same name and namespace in other branches
- 8.2 lingotek.module \lingotek_entity_insert()
- 7.7 lingotek.module \lingotek_entity_insert()
- 7.5 lingotek.module \lingotek_entity_insert()
- 7.6 lingotek.module \lingotek_entity_insert()
- 4.0.x lingotek.module \lingotek_entity_insert()
- 3.0.x lingotek.module \lingotek_entity_insert()
- 3.1.x lingotek.module \lingotek_entity_insert()
- 3.2.x lingotek.module \lingotek_entity_insert()
- 3.3.x lingotek.module \lingotek_entity_insert()
- 3.4.x lingotek.module \lingotek_entity_insert()
- 3.5.x lingotek.module \lingotek_entity_insert()
- 3.6.x lingotek.module \lingotek_entity_insert()
- 3.7.x lingotek.module \lingotek_entity_insert()
- 3.8.x lingotek.module \lingotek_entity_insert()
Implements hook_entity_insert().
File
- ./
lingotek.module, line 63 - lingotek.module
Code
function lingotek_entity_insert(EntityInterface $entity) {
/** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $configuration_service */
$configuration_service = \Drupal::service('lingotek.configuration');
if ($entity instanceof \Drupal\Core\Config\Entity\ConfigEntityInterface) {
if (\Drupal::isConfigSyncing()) {
// We don't want to react to configuration imports.
return;
}
/** @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $translation_service */
$translation_service = \Drupal::service('lingotek.config_translation');
$entity_type_id = $entity
->getEntityTypeId();
if ($entity instanceof \Drupal\field\Entity\FieldConfig) {
$entity_type_id = $entity
->getTargetEntityTypeId() . '_fields';
}
if ($translation_service
->isEnabled($entity_type_id)) {
$profile = $configuration_service
->getConfigEntityProfile($entity);
$has_autoupload = $profile
->hasAutomaticUpload();
$source_status = $translation_service
->getSourceStatus($entity);
$entity_has_changed = $translation_service
->hasEntityChanged($entity);
if ($has_autoupload) {
// New entity with auto-upload
if ($source_status == NULL || $source_status == Lingotek::STATUS_UNTRACKED) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_EDITED);
try {
$document_id = $translation_service
->uploadDocument($entity);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
} catch (LingotekApiException $exception) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_ERROR);
drupal_set_message(t('The upload for @entity_type %title failed. Please try again.', array(
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
)), 'error');
}
}
}
elseif (!$has_autoupload) {
if ($entity_has_changed) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_EDITED);
$translation_service
->markTranslationsAsDirty($entity);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
}
}
}
}
elseif ($entity instanceof ContentEntityInterface) {
if ($configuration_service
->isEnabled($entity
->getEntityTypeId(), $entity
->bundle())) {
$profile = $configuration_service
->getEntityProfile($entity);
$has_autoupload = $profile
->hasAutomaticUpload();
/** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
$translation_service = \Drupal::service('lingotek.content_translation');
$source_status = $translation_service
->getSourceStatus($entity);
// Entity inserts are always changes.
$entity_has_changed = TRUE;
if ($has_autoupload) {
// New entity with auto-upload
if ($source_status == NULL || $source_status == Lingotek::STATUS_UNTRACKED) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_EDITED);
try {
$document_id = $translation_service
->uploadDocument($entity);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
drupal_set_message(t('<em>@title</em> sent to Lingotek successfully.', array(
'@title' => $entity
->label(),
)));
} catch (LingotekApiException $exception) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_ERROR);
drupal_set_message(t('The upload for @entity_type %title failed. Please try again.', array(
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
)), 'error');
}
}
elseif ($entity_has_changed) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_EDITED);
try {
$response = $translation_service
->updateDocument($entity);
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
$translation_service
->markTranslationsAsDirty($entity);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_PENDING);
drupal_set_message(t('<em>@title</em> was updated and sent to Lingotek successfully.', array(
'@title' => $entity
->label(),
)));
} catch (LingotekApiException $exception) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_ERROR);
drupal_set_message(t('The update for @entity_type %title failed. Please try again.', array(
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
)), 'error');
}
}
}
elseif (!$has_autoupload) {
if ($entity_has_changed) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_EDITED);
$translation_service
->markTranslationsAsDirty($entity);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
}
}
}
}
}