function lingotek_entity_insert in Lingotek Translation 3.4.x
Same name and namespace in other branches
- 8 lingotek.module \lingotek_entity_insert()
- 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.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 79 - Implements Drupal-related hooks for the Lingotek Translation module.
Code
function lingotek_entity_insert(EntityInterface $entity) {
/** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $configuration_service */
$configuration_service = \Drupal::service('lingotek.configuration');
if ($entity instanceof 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 FieldConfig) {
$entity_type_id = $entity
->getTargetEntityTypeId() . '_fields';
}
if ($translation_service
->isEnabled($entity_type_id) && !in_array($entity
->language()
->getId(), [
LanguageInterface::LANGCODE_NOT_SPECIFIED,
LanguageInterface::LANGCODE_NOT_APPLICABLE,
])) {
$profile = $configuration_service
->getConfigEntityProfile($entity);
if ($profile === NULL) {
// If for whatever reason there is no profile, we better be cautious and
// do nothing. This can happen e.g. if there are modules implementing
// hook_lingotek_content_entity_get_profile().
return;
}
if ($profile
->id() === Lingotek::PROFILE_DISABLED) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
return;
}
elseif ($translation_service
->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
return;
}
elseif ($translation_service
->getSourceStatus($entity) === Lingotek::STATUS_DISABLED) {
if ($translation_service
->getDocumentId($entity) !== NULL) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
else {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
$translation_service
->checkTargetStatuses($entity);
return;
}
$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 (LingotekPaymentRequiredException $exception) {
\Drupal::messenger()
->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
} catch (LingotekApiException $exception) {
\Drupal::messenger()
->addError(t('The upload for @entity_type %title failed. Please try again.', [
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
]));
}
}
}
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);
}
if ($profile
->id() == "disabled") {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
}
}
}
}
elseif ($entity instanceof ContentEntityInterface) {
if ($configuration_service
->isEnabled($entity
->getEntityTypeId(), $entity
->bundle())) {
if ($entity instanceof ContentEntityInterface && $configuration_service
->isEnabled($entity
->getEntityTypeId(), $entity
->bundle())) {
if ($entity->lingotek_metadata->entity !== NULL) {
if (!$entity->lingotek_metadata->entity
->getContentEntityId()) {
$entity->lingotek_metadata->entity
->setEntity($entity);
$entity->lingotek_metadata->entity
->save();
}
}
}
// If workbench moderation is enabled, we need to prevent that content is
// processed if not the right state.
/** @var \Drupal\lingotek\Moderation\LingotekModerationFactoryInterface $moderation_factory */
$moderation_factory = \Drupal::service('lingotek.moderation_factory');
$moderation_handler = $moderation_factory
->getModerationHandler();
$prevent = $moderation_handler
->shouldModerationPreventUpload($entity);
if ($prevent) {
return;
}
$profile = $configuration_service
->getEntityProfile($entity);
if ($profile === NULL) {
// If for whatever reason there is no profile, we better be cautious and
// do nothing. This can happen e.g. if there are modules implementing
// hook_lingotek_content_entity_get_profile().
return;
}
$has_autoupload = $profile
->hasAutomaticUpload();
/** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
$translation_service = \Drupal::service('lingotek.content_translation');
$source_status = $translation_service
->getSourceStatus($entity);
if ($profile
->id() == "disabled") {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
$translation_service
->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
}
elseif ($source_status === Lingotek::STATUS_DISABLED) {
if ($translation_service
->getDocumentId($entity) !== NULL) {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
else {
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
$translation_service
->checkTargetStatuses($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::messenger()
->addStatus(t('<em>@title</em> sent to Lingotek successfully.', [
'@title' => $entity
->label(),
]));
} catch (LingotekPaymentRequiredException $exception) {
\Drupal::messenger()
->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
} catch (LingotekApiException $exception) {
\Drupal::messenger()
->addError(t('The upload for @entity_type %title failed. Please try again.', [
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
]));
}
}
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::messenger()
->addStatus(t('<em>@title</em> was updated and sent to Lingotek successfully.', [
'@title' => $entity
->label(),
]));
} catch (LingotekPaymentRequiredException $exception) {
\Drupal::messenger()
->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
} catch (LingotekApiException $exception) {
\Drupal::messenger()
->addError(t('The update for @entity_type %title failed. Please try again.', [
'@entity_type' => $entity
->getEntityTypeId(),
'%title' => $entity
->label(),
]));
}
}
}
// When inserting we never should set the status as EDITED.
}
}
}