- Issue created by @WalterP
I want to prevent a field 'field_url_neu' from being translated, depending if the translated field exist and has already a value and a boolean field 'field_xliff_url_neu' is not true.
Using the following code:
function hook_tmgmt_job_checkout_before_alter(&$remaining_jobs, &$jobs) {
foreach ($jobs as $job) {
$target_language = $job->getTargetLangcode();
// set translate to false for field_url_neu, if value exists already in translation
/** @var \Drupal\tmgmt\Data $data_service */
$data_service = \Drupal::service('tmgmt.data');
foreach ($job->getItems() as $job_item) {
$unfiltered_data = $job_item->getData();
$data = $job_item->getData(['field_url_neu']);
if (isset($unfiltered_data['field_url_neu']) && $job_item->getItemType() == 'node') {
$entity_id = $job_item->getItemId();
$node = \Drupal::entityTypeManager()->getStorage('node')->load($entity_id);
if ($node->hasTranslation($target_language)) {
$translated_node = $node->getTranslation($target_language);
if ($node->hasField('field_xliff_url_neu') && !$node->get('field_xliff_url_neu')->isEmpty() && $node->get('field_xliff_url_neu')->getValue()[0]['value'] == true) {
\Drupal::messenger()->addMessage('Override URL in translations for node id: ' . $node->id());
} elseif ($translated_node->hasField('field_url_neu') && !empty($translated_node->get('field_url_neu')->getValue())) {
$data[0]['value']['#translate'] = false;
$job_item->updateData(['field_url_neu'], $data, FALSE);
}
}
}
}
}
}
Translate to several languages as a batch job.
Preventing the field 'field_url_neu' from being translated works for the first job/language and in all other the 'field_url_neu' is translated.
Do I'm missing something in the code or would it be better, to take another way for this functionality?
Active
1.15
Source: Node