function tmgmt_extension_suit_entity_update in TMGMT Extension Suite 8.3
Same name and namespace in other branches
- 8 tmgmt_extension_suit.module \tmgmt_extension_suit_entity_update()
- 8.2 tmgmt_extension_suit.module \tmgmt_extension_suit_entity_update()
Implements hook_entity_update().
File
- ./
tmgmt_extension_suit.module, line 93 - Main module file for the TMGMT Smartling module.
Code
function tmgmt_extension_suit_entity_update(EntityInterface $entity) {
if ($entity instanceof ContentEntityInterface && $entity
->isTranslatable() && Drupal::config('tmgmt_extension_suit.settings')
->get('do_track_changes')) {
$do_track_changes_by_provider_and_locales = \Drupal::state()
->get('tmgmt_extension_suit.settings.do_track_changes_by_provider_and_locales');
$entity = $entity
->getUntranslated();
// Get job ids that contains current entity.
$select = Drupal::database()
->select('tmgmt_job', 'tj');
$select
->join('tmgmt_job_item', 'tji', 'tji.tjid = tj.tjid');
$select
->addField('tj', 'tjid');
$select
->condition('tj.state', [
JobInterface::STATE_ACTIVE,
JobInterface::STATE_REJECTED,
JobInterface::STATE_FINISHED,
], 'IN');
$select
->condition('tji.state', [
JobItemInterface::STATE_ABORTED,
], 'NOT IN');
$select
->condition('tji.item_type', $entity
->getEntityTypeId());
$select
->condition('tji.item_id', $entity
->id());
$job_ids = $select
->execute()
->fetchCol();
// Set job state as active. Set all child job items state as active.
// Reset all child data. Also we need to update job item hash because
// source entity is updated.
$upload_entities = [];
foreach ($job_ids as $job_id) {
if ($job = Job::load($job_id)) {
$translator = $job
->getTranslator();
$translator_plugin = $translator
->getPlugin();
if (!$translator_plugin instanceof ExtendedTranslatorPluginInterface) {
continue;
}
if (empty($do_track_changes_by_provider_and_locales[$translator
->id() . '_' . $job
->getTargetLangcode()])) {
continue;
}
$is_reopen_needed = FALSE;
foreach ($job
->getItems() as $item) {
$old_hash = $item
->get('tes_source_content_hash')
->getValue();
$old_hash = isset($old_hash[0]['value']) ? $old_hash[0]['value'] : '';
$hash = _tmgmt_extension_suit_get_job_item_hash($item);
// Source entity is updated. Mark job item as active.
if ($old_hash !== $hash) {
// Reset an old data array with old entity field values.
try {
$item
->resetData();
// Update current job item hash.
$item
->set('tes_source_content_hash', $hash);
// We don't use JobItem::setState() because it doesn't always call
// JobItem::save(), and so the behaviour might be inconsistent.
$item
->set('state', JobItemInterface::STATE_ACTIVE);
$item
->save();
$is_reopen_needed = TRUE;
} catch (TMGMTException $e) {
// It's possible that content entity doesn't exist at this point
// so $item->resetData() may throw an exception. Do not interrupt
// flow other job items in this case.
}
}
}
// Reopen parent job.
if ($is_reopen_needed) {
// Job::save() method invokes inside of Job::setState()
// method. So there is no need to call save() directly.
$job
->setState(JobInterface::STATE_ACTIVE);
// Put job into upload queue.
$upload_entities[$translator
->id()][] = $job
->id();
}
}
}
if (!empty($upload_entities)) {
foreach ($upload_entities as $translator_id => $job_ids_to_upload) {
$data = \Drupal::moduleHandler()
->invokeAll('tmgmt_extension_suit_updated_entity_jobs', [
$job_ids_to_upload,
$translator_id,
]);
foreach ($job_ids_to_upload as $job_id_to_upload) {
Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
->addItem('tmgmt_extension_suit_upload', [
'id' => (int) $job_id_to_upload,
] + $data, TRUE);
}
}
}
}
}