View source
<?php
namespace Drupal\lingotek;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Exception\LingotekApiException;
use Drupal\lingotek\Exception\LingotekContentEntityStorageException;
use Drupal\lingotek\Exception\LingotekException;
class LingotekContentTranslationService implements LingotekContentTranslationServiceInterface {
use StringTranslationTrait;
protected $lingotek;
protected $languageLocaleMapper;
protected $lingotekConfiguration;
protected $lingotekConfigTranslation;
protected $entityManager;
protected $languageManager;
public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper, LingotekConfigurationServiceInterface $lingotek_configuration, LingotekConfigTranslationServiceInterface $lingotek_config_translation, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
$this->lingotek = $lingotek;
$this->languageLocaleMapper = $language_locale_mapper;
$this->lingotekConfiguration = $lingotek_configuration;
$this->lingotekConfigTranslation = $lingotek_config_translation;
$this->entityManager = $entity_manager;
$this->languageManager = $language_manager;
}
public function checkSourceStatus(ContentEntityInterface &$entity) {
$document_id = $this
->getDocumentId($entity);
$MAX_IMPORT_TIME = 3600;
$source_status = $this
->getSourceStatus($entity);
if ($document_id) {
if ($this->lingotek
->getDocumentStatus($document_id)) {
$this
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
return TRUE;
}
else {
if ($entity
->getEntityType()
->isSubclassOf(EntityChangedInterface::class)) {
$last_uploaded_time = $entity
->getChangedTime();
if (REQUEST_TIME - $last_uploaded_time > $MAX_IMPORT_TIME) {
$this
->setSourceStatus($entity, Lingotek::STATUS_ERROR);
}
else {
}
}
return FALSE;
}
}
return FALSE;
}
public function getSourceStatus(ContentEntityInterface &$entity) {
$source_language = LanguageInterface::LANGCODE_NOT_SPECIFIED;
if ($entity->lingotek_translation_source && $entity->lingotek_translation_source->value !== NULL) {
$source_language = $entity->lingotek_translation_source->value;
}
if ($source_language == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$source_language = $entity
->getUntranslated()
->language()
->getId();
}
return $this
->getTargetStatus($entity, $source_language);
}
public function setSourceStatus(ContentEntityInterface &$entity, $status) {
$source_language = $entity->lingotek_translation_source->value;
if ($source_language == LanguageInterface::LANGCODE_NOT_SPECIFIED || $source_language == NULL) {
$source_language = $entity
->getUntranslated()
->language()
->getId();
}
return $this
->setTargetStatus($entity, $source_language, $status);
}
public function checkTargetStatuses(ContentEntityInterface &$entity) {
$document_id = $this
->getDocumentId($entity);
$translation_statuses = $this->lingotek
->getDocumentTranslationStatuses($document_id);
foreach ($translation_statuses as $lingotek_locale => $progress) {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($lingotek_locale);
if ($drupal_language == NULL) {
continue;
}
$langcode = $drupal_language
->id();
$current_target_status = $this
->getTargetStatus($entity, $langcode);
if (in_array($current_target_status, [
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_EDITED,
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_NONE,
Lingotek::STATUS_READY,
Lingotek::STATUS_PENDING,
NULL,
])) {
if ($progress === Lingotek::PROGRESS_COMPLETE) {
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_READY);
}
else {
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_PENDING);
}
}
}
}
public function checkTargetStatus(ContentEntityInterface &$entity, $langcode) {
$current_status = $this
->getTargetStatus($entity, $langcode);
$locale = $this->languageLocaleMapper
->getLocaleForLangcode($langcode);
$source_status = $this
->getSourceStatus($entity);
$document_id = $this
->getDocumentId($entity);
if ($langcode !== $entity
->getUntranslated()
->language()
->getId()) {
if (($current_status == Lingotek::STATUS_PENDING || $current_status == Lingotek::STATUS_EDITED) && $source_status !== Lingotek::STATUS_EDITED) {
$translation_status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
if ($translation_status) {
$current_status = Lingotek::STATUS_READY;
$this
->setTargetStatus($entity, $langcode, $current_status);
}
elseif ($this->lingotek
->downloadDocument($document_id, $locale)) {
}
}
elseif ($current_status == Lingotek::STATUS_REQUEST || $current_status == Lingotek::STATUS_UNTRACKED) {
$translation_status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
if ($translation_status === TRUE) {
$current_status = Lingotek::STATUS_READY;
$this
->setTargetStatus($entity, $langcode, $current_status);
}
elseif ($translation_status !== FALSE) {
$current_status = Lingotek::STATUS_PENDING;
$this
->setTargetStatus($entity, $langcode, $current_status);
}
}
}
return $current_status;
}
public function getTargetStatus(ContentEntityInterface &$entity, $langcode) {
$status = Lingotek::STATUS_UNTRACKED;
if (count($entity->lingotek_translation_status) > 0) {
foreach ($entity->lingotek_translation_status
->getIterator() as $delta => $value) {
if ($value->language == $langcode) {
$status = $value->value;
}
}
}
return $status;
}
public function setTargetStatus(ContentEntityInterface &$entity, $langcode, $status, $save = TRUE) {
$set = FALSE;
if ($entity
->hasField('lingotek_translation_status') && count($entity->lingotek_translation_status) > 0) {
foreach ($entity->lingotek_translation_status
->getIterator() as $delta => $value) {
if ($value->language == $langcode) {
$value->value = $status;
$set = true;
}
}
}
if (!$set && $entity
->hasField('lingotek_translation_status')) {
$entity->lingotek_translation_status
->appendItem([
'language' => $langcode,
'value' => $status,
]);
$set = TRUE;
}
if ($set && $save) {
if ($entity
->getEntityType()
->hasKey('revision')) {
$entity
->setNewRevision(FALSE);
}
$entity->lingotek_processed = TRUE;
$entity
->save();
}
return $entity;
}
public function setTargetStatuses(ContentEntityInterface &$entity, $status) {
$target_languages = $this->languageManager
->getLanguages();
$entity_langcode = $entity
->getUntranslated()
->language()
->getId();
foreach ($target_languages as $langcode => $language) {
if ($langcode != $entity_langcode && ($current_status = $this
->getTargetStatus($entity, $langcode))) {
if ($current_status === Lingotek::STATUS_PENDING && $status === Lingotek::STATUS_REQUEST) {
continue;
}
if (in_array($current_status, [
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_DISABLED,
NULL,
]) && $status === Lingotek::STATUS_PENDING) {
continue;
}
if ($current_status == $status) {
continue;
}
if ($current_status != Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_CURRENT) {
$this
->setTargetStatus($entity, $langcode, $status);
}
elseif ($current_status == Lingotek::STATUS_EDITED && in_array($status, [
Lingotek::STATUS_CURRENT,
Lingotek::STATUS_PENDING,
])) {
$this
->setTargetStatus($entity, $langcode, $status);
}
}
}
}
public function markTranslationsAsDirty(ContentEntityInterface &$entity) {
$target_languages = $this->languageManager
->getLanguages();
$entity_langcode = $entity
->getUntranslated()
->language()
->getId();
$to_change = [
Lingotek::STATUS_CURRENT,
Lingotek::STATUS_PENDING,
Lingotek::STATUS_INTERMEDIATE,
Lingotek::STATUS_READY,
];
foreach ($target_languages as $langcode => $language) {
if ($langcode != $entity_langcode && ($current_status = $this
->getTargetStatus($entity, $langcode))) {
if (in_array($current_status, $to_change)) {
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_EDITED);
}
}
}
}
public function getDocumentId(ContentEntityInterface &$entity) {
$doc_id = NULL;
if ($entity->lingotek_document_id) {
$doc_id = $entity->lingotek_document_id->value;
}
return $doc_id;
}
public function setDocumentId(ContentEntityInterface &$entity, $doc_id) {
$entity->lingotek_document_id = $doc_id;
if ($entity
->getEntityType()
->hasKey('revision')) {
$entity
->setNewRevision(FALSE);
}
$entity->lingotek_processed = TRUE;
$entity
->save();
\Drupal::database()
->insert('lingotek_content_metadata')
->fields([
'document_id',
'entity_type',
'entity_id',
])
->values([
'document_id' => $doc_id,
'entity_type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
])
->execute();
return $entity;
}
public function getSourceLocale(ContentEntityInterface &$entity) {
$source_language = $entity
->getUntranslated()
->language()
->getId();
return $this->languageLocaleMapper
->getLocaleForLangcode($source_language);
}
public function getSourceData(ContentEntityInterface &$entity, &$visited = []) {
$visited[$entity
->bundle()][] = $entity
->id();
$entity_type = $entity
->getEntityType();
$field_definitions = $this->entityManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
$storage_definitions = $entity_type instanceof ContentEntityTypeInterface ? $this->entityManager
->getFieldStorageDefinitions($entity_type
->id()) : array();
$translatable_fields = array();
foreach ($entity
->getFields(TRUE) as $field_name => $definition) {
if ($this->lingotekConfiguration
->isFieldLingotekEnabled($entity
->getEntityTypeId(), $entity
->bundle(), $field_name) && $field_name != $entity_type
->getKey('langcode') && $field_name != $entity_type
->getKey('default_langcode')) {
$translatable_fields[$field_name] = $definition;
}
}
$default_display = entity_get_display($entity
->getEntityTypeId(), $entity
->bundle(), 'default');
if ($default_display !== NULL) {
uksort($translatable_fields, function ($a, $b) use ($default_display) {
return SortArray::sortByKeyString($default_display
->getComponent($a), $default_display
->getComponent($b), 'weight');
});
}
$data = array();
$source_entity = $entity
->getUntranslated();
foreach ($translatable_fields as $k => $definition) {
module_load_include('inc', 'content_translation', 'content_translation.admin');
$column_element = content_translation_field_sync_widget($field_definitions[$k]);
$field = $source_entity
->get($k);
$field_type = $field_definitions[$k]
->getType();
foreach ($field as $fkey => $fval) {
if (!$column_element) {
$properties = $fval
->getProperties();
foreach ($properties as $property_name => $property_value) {
if (isset($storage_definitions[$k])) {
$property_definition = $storage_definitions[$k]
->getPropertyDefinition($property_name);
$data_type = $property_definition
->getDataType();
if (($data_type === 'string' || $data_type === 'uri') && !$property_definition
->isComputed()) {
if (isset($fval->{$property_name}) && !empty($fval->{$property_name})) {
$data[$k][$fkey][$property_name] = $fval
->get($property_name)
->getValue();
}
if ($field_type === 'path') {
unset($data[$k][$fkey]['pid']);
}
}
}
}
}
else {
$configured_properties = $this->lingotekConfiguration
->getFieldPropertiesLingotekEnabled($entity
->getEntityTypeId(), $entity
->bundle(), $k);
$properties = $fval
->getProperties();
foreach ($properties as $pkey => $pval) {
if (isset($configured_properties[$pkey]) && $configured_properties[$pkey]) {
$data[$k][$fkey][$pkey] = $pval
->getValue();
}
}
}
}
if ($field_type === 'entity_reference' || $field_type === 'er_viewmode') {
$target_entity_type_id = $field_definitions[$k]
->getFieldStorageDefinition()
->getSetting('target_type');
foreach ($entity->{$k} as $field_item) {
$embedded_entity_id = $field_item
->get('target_id')
->getValue();
$embedded_entity = $this->entityManager
->getStorage($target_entity_type_id)
->load($embedded_entity_id);
if ($embedded_entity !== NULL) {
if ($embedded_entity instanceof ContentEntityInterface) {
if (!isset($visited[$embedded_entity
->bundle()]) || !in_array($embedded_entity
->id(), $visited[$embedded_entity
->bundle()])) {
$embedded_data = $this
->getSourceData($embedded_entity, $visited);
$data[$k][$field_item
->getName()] = $embedded_data;
}
}
else {
if ($embedded_entity instanceof ConfigEntityInterface) {
$embedded_data = $this->lingotekConfigTranslation
->getSourceData($embedded_entity);
$data[$k][$field_item
->getName()] = $embedded_data;
}
}
}
else {
unset($data[$k]);
}
}
}
else {
if ($field_type === 'entity_reference_revisions') {
$target_entity_type_id = $field_definitions[$k]
->getFieldStorageDefinition()
->getSetting('target_type');
foreach ($entity->{$k} as $field_item) {
$embedded_entity_id = $field_item
->get('target_id')
->getValue();
$embedded_entity_revision_id = $field_item
->get('target_revision_id')
->getValue();
$embedded_entity = $this->entityManager
->getStorage($target_entity_type_id)
->loadRevision($embedded_entity_revision_id);
$embedded_data = $this
->getSourceData($embedded_entity);
$data[$k][$field_item
->getName()] = $embedded_data;
}
}
else {
if ($field_type === 'metatag') {
foreach ($entity->{$k} as $field_item) {
$metatag_serialized = $field_item
->get('value')
->getValue();
$metatags = unserialize($metatag_serialized);
if ($metatags) {
$data[$k][$field_item
->getName()] = $metatags;
}
}
}
else {
if ($field_type === 'path') {
if ($entity
->id()) {
$source = '/' . $entity
->toUrl()
->getInternalPath();
$path = \Drupal::service('path.alias_storage')
->load([
'source' => $source,
'langcode' => $entity
->language()
->getId(),
]);
$alias = $path['alias'];
if ($alias !== NULL) {
$data[$k][0]['alias'] = $alias;
}
}
}
}
}
}
}
if ($entity
->id()) {
$data['_lingotek_metadata'] = [
'_entity_type_id' => $entity
->getEntityTypeId(),
'_entity_id' => $entity
->id(),
'_entity_revision' => $entity
->getRevisionId(),
];
}
return $data;
}
public function updateEntityHash(ContentEntityInterface $entity) {
$source_data = json_encode($this
->getSourceData($entity));
$entity->lingotek_hash->value = md5($source_data);
}
public function hasEntityChanged(ContentEntityInterface &$entity) {
if (isset($entity->original)) {
return $entity->lingotek_hash->value !== $entity->original->lingotek_hash->value;
}
$old_hash = $entity->lingotek_hash->value;
if (!$old_hash) {
return TRUE;
}
$source_data = json_encode($this
->getSourceData($entity));
$hash = md5($source_data);
return (bool) strcmp($hash, $old_hash);
}
public function addTarget(ContentEntityInterface &$entity, $locale) {
$source_langcode = $entity
->getUntranslated()
->language()
->getId();
$source_locale = $this->languageLocaleMapper
->getLocaleForLangcode($source_langcode);
if ($locale == $source_locale) {
return FALSE;
}
if ($document_id = $this
->getDocumentId($entity)) {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale);
$source_status = $this
->getSourceStatus($entity);
$current_status = $this
->getTargetStatus($entity, $drupal_language
->id());
$pristine_statuses = [
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_EDITED,
];
if (in_array($current_status, $pristine_statuses)) {
if ($this->lingotek
->addTarget($document_id, $locale, $this->lingotekConfiguration
->getEntityProfile($entity))) {
$this
->setTargetStatus($entity, $drupal_language
->id(), Lingotek::STATUS_PENDING);
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
return TRUE;
}
}
}
return FALSE;
}
public function requestTranslations(ContentEntityInterface &$entity) {
$languages = [];
if ($document_id = $this
->getDocumentId($entity)) {
$target_languages = $this->languageManager
->getLanguages();
$target_languages = array_filter($target_languages, function (LanguageInterface $language) {
$configLanguage = ConfigurableLanguage::load($language
->getId());
return $this->lingotekConfiguration
->isLanguageEnabled($configLanguage);
});
$entity_langcode = $entity
->getUntranslated()
->language()
->getId();
foreach ($target_languages as $langcode => $language) {
$locale = $this->languageLocaleMapper
->getLocaleForLangcode($langcode);
if ($langcode !== $entity_langcode) {
$source_status = $this
->getSourceStatus($entity);
$current_status = $this
->getTargetStatus($entity, $langcode);
if ($current_status !== Lingotek::STATUS_PENDING && $current_status !== Lingotek::STATUS_CURRENT && $current_status !== Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_READY) {
if ($this->lingotek
->addTarget($document_id, $locale, $this->lingotekConfiguration
->getEntityProfile($entity))) {
$languages[] = $langcode;
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_PENDING);
if ($source_status == Lingotek::STATUS_IMPORTING) {
$this
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
}
}
}
}
}
return $languages;
}
public function uploadDocument(ContentEntityInterface $entity) {
if (!empty($entity->lingotek_document_id->value)) {
return $this
->updateDocument($entity);
}
$source_data = $this
->getSourceData($entity);
$document_name = $entity
->bundle() . ' (' . $entity
->getEntityTypeId() . '): ' . $entity
->label();
$url = $entity
->hasLinkTemplate('canonical') ? $entity
->toUrl()
->setAbsolute(TRUE)
->toString() : NULL;
$profile = $this->lingotekConfiguration
->getEntityProfile($entity);
\Drupal::moduleHandler()
->invokeAll('lingotek_content_entity_document_upload', [
&$source_data,
&$entity,
&$url,
]);
$encoded_data = json_encode($source_data);
$document_id = $this->lingotek
->uploadDocument($document_name, $encoded_data, $this
->getSourceLocale($entity), $url, $profile);
if ($document_id) {
$this
->setDocumentId($entity, $document_id);
$this
->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
$this
->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
return $document_id;
}
return FALSE;
}
public function downloadDocument(ContentEntityInterface &$entity, $locale) {
if ($document_id = $this
->getDocumentId($entity)) {
$source_status = $this
->getSourceStatus($entity);
try {
$data = $this->lingotek
->downloadDocument($document_id, $locale);
} catch (LingotekApiException $exception) {
return FALSE;
}
if ($data) {
$status = $this->lingotek
->getDocumentTranslationStatus($document_id, $locale);
$transaction = db_transaction();
try {
$drupal_language = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale);
$langcode = $drupal_language
->id();
$saved = $this
->saveTargetData($entity, $langcode, $data);
if ($saved) {
if ($source_status == Lingotek::STATUS_IMPORTING || $source_status == Lingotek::STATUS_EDITED) {
$this
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
}
if ($status) {
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_CURRENT);
}
else {
$this
->setTargetStatus($entity, $langcode, Lingotek::STATUS_INTERMEDIATE);
}
}
} catch (LingotekContentEntityStorageException $storageException) {
throw $storageException;
} catch (\Exception $exception) {
$transaction
->rollback();
return FALSE;
}
return TRUE;
}
}
return FALSE;
}
public function updateDocument(ContentEntityInterface &$entity) {
$source_data = $this
->getSourceData($entity);
$document_id = $this
->getDocumentId($entity);
$url = $entity
->hasLinkTemplate('canonical') ? $entity
->toUrl()
->setAbsolute(TRUE)
->toString() : NULL;
$document_name = $entity
->bundle() . ' (' . $entity
->getEntityTypeId() . '): ' . $entity
->label();
\Drupal::moduleHandler()
->invokeAll('lingotek_content_entity_document_upload', [
&$source_data,
&$entity,
&$url,
]);
$encoded_data = json_encode($source_data);
if ($this->lingotek
->updateDocument($document_id, $encoded_data, $url, $document_name)) {
$this
->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
$this
->setTargetStatuses($entity, Lingotek::STATUS_PENDING);
return $document_id;
}
return FALSE;
}
public function downloadDocuments() {
try {
$data = $this->lingotek
->downloadDocuments();
} catch (LingotekApiException $exception) {
return FALSE;
}
return $data;
}
public function downloadDocumentContent($document_id) {
try {
$data = $this->lingotek
->downloadDocumentContent($document_id);
} catch (LingotekApiException $exception) {
return FALSE;
}
return $data;
}
public function deleteDocument(ContentEntityInterface &$entity) {
return $this->lingotek
->deleteDocument($this
->getDocumentId($entity));
}
public function deleteMetadata(ContentEntityInterface &$entity) {
if ($this->lingotekConfiguration
->mustDeleteRemoteAfterDisassociation()) {
$this
->deleteDocument($entity);
}
$doc_id = $this
->getDocumentId($entity);
$entity->lingotek_translation_status = NULL;
$entity->lingotek_document_id = NULL;
\Drupal::database()
->delete('lingotek_content_metadata')
->condition('document_id', $doc_id)
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id())
->execute();
if ($entity
->getEntityType()
->hasKey('revision')) {
$entity
->setNewRevision(FALSE);
}
$entity->lingotek_processed = TRUE;
$entity
->save();
}
public function loadByDocumentId($document_id) {
$entity = NULL;
$metadata = \Drupal::database()
->select('lingotek_content_metadata', 'lcm')
->fields('lcm', [
'document_id',
'entity_type',
'entity_id',
])
->condition('document_id', $document_id)
->execute()
->fetchAssoc();
if ($metadata) {
$entity = $this->entityManager
->getStorage($metadata['entity_type'])
->load($metadata['entity_id']);
}
return $entity;
}
public function getAllLocalDocumentIds() {
return $metadata = \Drupal::database()
->select('lingotek_content_metadata', 'lcm')
->fields('lcm', [
'document_id',
])
->execute()
->fetchCol(0);
}
protected function loadUploadedRevision(ContentEntityInterface $entity, $revision = NULL) {
$the_revision = NULL;
$entity_type = $entity
->getEntityType();
$type = $entity
->getEntityTypeId();
if ($entity_type
->isRevisionable()) {
if ($revision !== NULL) {
$the_revision = entity_revision_load($type, $revision);
}
elseif ($revision === NULL && $entity
->hasField('revision_timestamp')) {
$timestamp = $this->lingotek
->getUploadedTimestamp($this
->getDocumentId($entity));
$revision = $this
->getClosestRevisionToTimestamp($entity, $timestamp);
$the_revision = entity_revision_load($type, $revision);
}
else {
$the_revision = entity_revision_load($type, $entity
->getRevisionId());
}
}
else {
$the_revision = entity_load($type, $entity
->id(), TRUE);
}
return $the_revision;
}
protected function getClosestRevisionToTimestamp(ContentEntityInterface &$entity, $timestamp) {
$entity_id = $entity
->id();
$query = \Drupal::database()
->select($entity
->getEntityType()
->getRevisionDataTable(), 'nfr');
$query
->fields('nfr', [
$entity
->getEntityType()
->getKey('revision'),
]);
$query
->addJoin('INNER', $entity
->getEntityType()
->getRevisionTable(), 'nr', 'nfr.vid = nr.vid and nfr.nid = nr.nid and nfr.langcode = nr.langcode');
$query
->condition('nfr.' . $entity
->getEntityType()
->getKey('id'), $entity_id);
$query
->condition('nfr.' . $entity
->getEntityType()
->getKey('langcode'), $entity
->language()
->getId());
$query
->condition('nr.revision_timestamp', $timestamp, '<');
$query
->orderBy('nfr.changed', 'DESC');
$query
->range(0, 1);
$value = $query
->execute();
$vids = $value
->fetchAssoc();
return count($vids) === 1 ? $vids['vid'] : NULL;
}
public function saveTargetData(ContentEntityInterface &$entity, $langcode, $data) {
if (!$langcode) {
return FALSE;
}
$storage_definitions = $this->entityManager
->getFieldStorageDefinitions($entity
->getEntityTypeId());
$lock = \Drupal::lock();
$lock_name = __FUNCTION__ . ':' . $entity
->getEntityTypeId() . ':' . $entity
->id();
$held = $lock
->acquire($lock_name);
if (!$held) {
if ($lock
->wait($lock_name) === FALSE) {
$held = $lock
->acquire($lock_name);
}
}
if (!$held) {
throw new \Exception(new FormattableMarkup('Unable to acquire lock for entity @id of type @type.', [
'@id' => $entity
->id(),
'@type' => $entity
->getEntityTypeId(),
]));
}
try {
$revision = isset($data['_lingotek_metadata']) ? $data['_lingotek_metadata']['_entity_revision'] : NULL;
$revision = $this
->loadUploadedRevision($entity, $revision);
if (!$entity
->hasTranslation($langcode)) {
$entity
->addTranslation($langcode, $revision
->toArray());
}
$translation = $entity
->getTranslation($langcode);
foreach ($data as $name => $field_data) {
if (strpos($name, '_') === 0) {
break;
}
$field_definition = $entity
->getFieldDefinition($name);
if (($field_definition
->isTranslatable() || $field_definition
->getType() === 'entity_reference_revisions') && $this->lingotekConfiguration
->isFieldLingotekEnabled($entity
->getEntityTypeId(), $entity
->bundle(), $name)) {
$field_type = $field_definition
->getType();
if ($field_type === 'entity_reference' || $field_type === 'er_viewmode') {
$target_entity_type_id = $field_definition
->getFieldStorageDefinition()
->getSetting('target_type');
$index = 0;
foreach ($field_data as $field_item) {
if (isset($field_item['_lingotek_metadata'])) {
$target_entity_type_id = $field_item['_lingotek_metadata']['_entity_type_id'];
$embedded_entity_id = $field_item['_lingotek_metadata']['_entity_id'];
$embedded_entity_revision_id = $field_item['_lingotek_metadata']['_entity_revision'];
}
else {
$embedded_entity_id = $revision->{$name}
->get($index)
->get('target_id')
->getValue();
}
$embedded_entity = $this->entityManager
->getStorage($target_entity_type_id)
->load($embedded_entity_id);
if ($embedded_entity !== NULL) {
if ($embedded_entity instanceof ContentEntityInterface) {
if ($this->lingotekConfiguration
->isEnabled($embedded_entity
->getEntityTypeId(), $embedded_entity
->bundle())) {
$this
->saveTargetData($embedded_entity, $langcode, $field_item);
}
else {
\Drupal::logger('lingotek')
->warning($this
->t('Field %field not saved as it\'s referenced entity is not translatable by Lingotek', [
'%field' => $name,
]));
}
}
elseif ($embedded_entity instanceof ConfigEntityInterface) {
$this->lingotekConfigTranslation
->saveTargetData($embedded_entity, $langcode, $field_item);
}
$translation->{$name}
->set($index, $embedded_entity_id);
}
++$index;
}
}
else {
if ($field_type === 'entity_reference_revisions') {
$target_entity_type_id = $field_definition
->getFieldStorageDefinition()
->getSetting('target_type');
$index = 0;
foreach ($field_data as $field_item) {
$embedded_entity_id = $revision->{$name}
->get($index)
->get('target_id')
->getValue();
$embedded_entity = $this->entityManager
->getStorage($target_entity_type_id)
->load($embedded_entity_id);
$this
->saveTargetData($embedded_entity, $langcode, $field_item);
$translation->{$name}
->set($index, [
'target_id' => $embedded_entity_id,
'target_revision_id' => $embedded_entity
->getRevisionId(),
]);
++$index;
}
}
else {
if ($field_type === 'path') {
$pid = NULL;
$source = '/' . $entity
->toUrl()
->getInternalPath();
$alias_storage = \Drupal::service('path.alias_storage');
$path = $alias_storage
->load([
'source' => $source,
'langcode' => $langcode,
]);
$original_path = $alias_storage
->load([
'source' => $source,
'langcode' => $entity
->getUntranslated()
->language()
->getId(),
]);
if ($path) {
$pid = $path['pid'];
}
$alias = $field_data[0]['alias'];
if (!\Drupal::pathValidator()
->isValid($alias)) {
\Drupal::logger('lingotek')
->warning($this
->t('Alias for %type %label in language %langcode not saved, invalid uri "%uri"', [
'%type' => $entity
->getEntityTypeId(),
'%label' => $entity
->label(),
'%langcode' => $langcode,
'%uri' => $alias,
]));
$alias = $original_path ? $original_path['alias'] : $source;
}
if ($alias !== NULL) {
\Drupal::service('path.alias_storage')
->save($source, $alias, $langcode, $pid);
}
}
else {
if ($field_type === 'metatag') {
$index = 0;
foreach ($field_data as $field_item) {
$metatag_value = serialize($field_item);
$translation->{$name}
->set($index, $metatag_value);
++$index;
}
}
else {
$delta = -1;
foreach ($field_data as $delta => $delta_data) {
foreach ($delta_data as $property => $property_data) {
$property_definition = $storage_definitions[$name]
->getPropertyDefinition($property);
$data_type = $property_definition
->getDataType();
if ($data_type === 'uri') {
if (!\Drupal::pathValidator()
->isValid($property_data)) {
\Drupal::logger('lingotek')
->warning($this
->t('Field %field for %type %label in language %langcode not saved, invalid uri "%uri"', [
'%field' => $name,
'%type' => $entity
->getEntityTypeId(),
'%label' => $entity
->label(),
'%langcode' => $langcode,
'%uri' => $property_data,
]));
$property_data = $revision
->get($name)
->offsetGet($delta)->{$property};
}
}
if (method_exists($translation
->get($name)
->offsetGet($delta), "set")) {
$translation
->get($name)
->offsetGet($delta)
->set($property, $property_data);
}
elseif ($translation
->get($name)) {
$translation
->get($name)
->appendItem()
->set($property, $property_data);
}
}
}
$continue = TRUE;
while ($continue) {
if ($translation
->get($name)
->offsetExists($delta + 1)) {
$translation
->get($name)
->removeItem($delta + 1);
}
else {
$continue = FALSE;
}
}
}
}
}
}
}
}
$translation
->set('content_translation_source', $entity
->getUntranslated()
->language()
->getId());
$entity->lingotek_processed = TRUE;
\Drupal::moduleHandler()
->invokeAll('lingotek_content_entity_translation_presave', [
&$translation,
$langcode,
$data,
]);
$status_field = $entity
->getEntityType()
->getKey('status');
$status_field_definition = $entity
->getFieldDefinition($status_field);
if ($status_field_definition !== NULL && $status_field_definition
->isTranslatable()) {
$status_setting = $this->lingotekConfiguration
->getPreference('target_download_status');
if ($status_setting !== "same-as-source") {
$status_value = $status_setting === 'published' ? NODE_PUBLISHED : NODE_NOT_PUBLISHED;
$translation
->set($status_field, $status_value);
}
}
$translation
->save();
return $entity;
} catch (EntityStorageException $storage_exception) {
throw new LingotekContentEntityStorageException($entity, $storage_exception);
} finally {
$lock
->release($lock_name);
}
}
}