View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\TMGMTException;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;
use Drupal\views\ViewExecutable;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
function tmgmt_extension_suit_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
if ($view
->id() != 'tmgmt_job_overview') {
return;
}
$handlers = $view
->getHandlers('field');
foreach ($handlers as $name => $value) {
$view
->removeHandler($view->current_display, 'field', $name);
}
$view
->addHandler($view->current_display, 'field', 'tmgmt_job', 'tmgmt_job_bulk_form', [
'id' => 'tmgmt_job_bulk_form',
'table' => 'tmgmt_job',
'field' => 'tmgmt_job_bulk_form',
'group_type' => 'group',
'label' => 'Bulk update',
'hide_alter_empty' => 1,
'action_title' => 'With selection',
'include_exclude' => 'exclude',
'selected_actions' => [],
'entity_type' => 'tmgmt_job',
'plugin_id' => 'bulk_form',
'weight' => -10,
]);
foreach ($handlers as $name => $value) {
$view
->addHandler($view->current_display, 'field', 'tmgmt_job', $name, $value);
}
}
function tmgmt_extension_suit_add_to_check_status() {
$query = Drupal::database()
->select('tmgmt_job', 'tj');
$query
->join('tmgmt_job_item', 'tji', 'tji.tjid = tj.tjid');
$query
->fields('tj', [
'tjid',
])
->distinct()
->condition('tj.state', [
JobInterface::STATE_ACTIVE,
JobInterface::STATE_FINISHED,
], 'IN')
->condition('tj.target_language', 'und', '!=')
->condition('tji.state', JobItemInterface::STATE_REVIEW, '!=');
$entities = $query
->execute()
->fetchCol();
foreach ($entities as $eid) {
Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
->addItem('tmgmt_extension_suit_check_status', [
'id' => (int) $eid,
]);
}
}
function tmgmt_extension_suit_is_add_to_check_status_cron_run_needed($check_status_cron_last, $request_time) {
$interval = Drupal::config('tmgmt_extension_suit.settings')
->get('interval');
return $request_time >= $check_status_cron_last + $interval;
}
function tmgmt_extension_suit_cron() {
$key = 'tmgmt_extension_suit.check_status_cron_last';
$check_status_cron_last = \Drupal::state()
->get($key, 0);
$request_time = time();
if (tmgmt_extension_suit_is_add_to_check_status_cron_run_needed($check_status_cron_last, $request_time)) {
tmgmt_extension_suit_add_to_check_status();
\Drupal::state()
->set($key, $request_time);
}
}
function _tmgmt_extension_suit_get_job_item_hash(JobItemInterface $job_item) {
if ($job_item
->getJobId() == 0) {
return '';
}
try {
$entity = Drupal::entityTypeManager()
->getStorage($job_item
->getItemType())
->load($job_item
->getItemId());
} catch (\Exception $e) {
return '';
}
if (!($entity instanceof ContentEntityInterface && $entity
->isTranslatable())) {
return '';
}
$hash_data = Drupal::service('tmgmt.data')
->filterTranslatable($job_item
->getSourceData());
return md5(json_encode($hash_data));
}
function tmgmt_extension_suit_entity_update(EntityInterface $entity) {
if ($entity instanceof ContentEntityInterface && $entity
->isTranslatable() && Drupal::config('tmgmt_extension_suit.settings')
->get('do_track_changes')) {
$entity = $entity
->getUntranslated();
$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();
$upload_entities = [];
foreach ($job_ids as $job_id) {
if ($job = Job::load($job_id)) {
$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);
if ($old_hash !== $hash) {
$item
->resetData();
$item
->set('tes_source_content_hash', $hash);
$item
->set('state', JobItemInterface::STATE_ACTIVE);
$item
->save();
$is_reopen_needed = TRUE;
}
}
if ($is_reopen_needed) {
$job
->setState(JobInterface::STATE_ACTIVE);
$upload_entities[] = $job
->id();
}
}
}
if (!empty($upload_entities)) {
$data = \Drupal::moduleHandler()
->invokeAll('tmgmt_extension_suit_updated_entity_jobs', [
$upload_entities,
]);
foreach ($upload_entities as $upload_entity) {
Drupal::service('tmgmt_extension_suit.utils.queue_unique_item')
->addItem('tmgmt_extension_suit_upload', [
'id' => (int) $upload_entity,
] + $data, TRUE);
}
}
}
}
function tmgmt_extension_suit_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type
->id() === 'tmgmt_job_item') {
$fields['tes_source_content_hash'] = BaseFieldDefinition::create('string')
->setLabel(t('Source content hash (md5)'))
->setSetting('max_length', 32)
->setTranslatable(FALSE);
return $fields;
}
}
function tmgmt_extension_suit_tmgmt_job_item_presave(EntityInterface $job_item) {
$old_hash = $job_item
->get('tes_source_content_hash')
->getValue();
$old_hash = isset($old_hash[0]['value']) ? $old_hash[0]['value'] : '';
if ('' != $old_hash) {
return;
}
$hash = _tmgmt_extension_suit_get_job_item_hash($job_item);
if ($hash !== '') {
$job_item
->set('tes_source_content_hash', $hash);
}
}
function tmgmt_extension_suit_tmgmt_translatable_fields_alter(ContentEntityInterface $entity, array &$translatable_fields) {
unset($translatable_fields['moderation_state']);
}
function tmgmt_extension_suit_entity_base_field_info_alter(array &$fields, EntityTypeInterface $entity_type) {
if ($entity_type
->id() === 'tmgmt_job' && empty($fields['job_file_name'])) {
$fields['job_file_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Generated file name'))
->setSetting('max_length', 1024)
->setTranslatable(FALSE)
->setProvider('tmgmt_extension_suit')
->setName('job_file_name')
->setTargetEntityTypeId($entity_type
->id())
->setTargetBundle(NULL);
}
}
function tmgmt_extension_suit_tmgmt_job_presave(Drupal\Core\Entity\EntityInterface $entity) {
try {
if ($entity
->hasTranslator()) {
$plugin = $entity
->getTranslatorPlugin();
if ($plugin instanceof ExtendedTranslatorPluginInterface) {
$entity
->set('job_file_name', $plugin
->getFileName($entity));
}
}
} catch (TMGMTException $e) {
watchdog_exception('tmgmt_smartling', $e);
}
}