View source  
  <?php
use Drupal\tmgmt\Entity\Job;
function globallink_theme() {
  return [
    'globallink_comments' => [
      'variables' => [
        'comments' => NULL,
      ],
      'template' => 'globallink_comments',
    ],
  ];
}
function globallink_cron() {
  $globallink_translators = \Drupal::entityTypeManager()
    ->getStorage('tmgmt_translator')
    ->loadByProperties([
    'plugin' => 'globallink',
  ]);
  
  foreach ($globallink_translators as $translator) {
    
    $translator_plugin = $translator
      ->getPlugin();
    $completed_translations = $translator_plugin
      ->getCompletedTranslations($translator);
    if (!empty($completed_translations)) {
      foreach ($completed_translations as $completed_translation) {
        $query = \Drupal::entityQuery('tmgmt_job')
          ->condition('uuid', $completed_translation->clientIdentifier);
        $job_ids = $query
          ->execute();
        if (!empty($job_ids)) {
          
          $job = Job::load(array_shift($job_ids));
          $translator_plugin
            ->retrieveTranslation($completed_translation->ticket, $job);
        }
      }
    }
  }
}
function globallink_tmgmt_message_insert(\Drupal\tmgmt\MessageInterface $entity) {
  $langcode = \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId();
  
  $job = $entity
    ->getJob();
  
  
  if (!$job
    ->hasTranslator()) {
    return;
  }
  $translator = $job
    ->getTranslator();
  $settings = $translator
    ->getSettings();
  if ($translator
    ->get('plugin') == 'globallink') {
    if (!$entity
      ->get('variables')
      ->isEmpty() && !empty($settings['pd_notify_emails']) && !empty($settings['pd_notify_level'])) {
      if (in_array($entity
        ->getType(), $settings['pd_notify_level'])) {
        $emails = explode(' ', $settings['pd_notify_emails']);
        
        $mail_manager = \Drupal::service('plugin.manager.mail');
        $mail_manager
          ->mail('globallink', 'log', $emails, $langcode, [
          'message' => $entity,
        ]);
      }
    }
  }
}
function globallink_mail($key, &$message, $params) {
  
  $tmgmt_message = $params['message'];
  $langcode = $message['langcode'];
  switch ($key) {
    case 'log':
      $message['subject'] = t('Translations.com: new message from @site_name', [
        '@site_name' => \Drupal::config('system.site')
          ->get('name'),
      ], [
        'langcode' => $langcode,
      ]);
      $message['body'][] = t('New log message from translations.com service: @message.', [
        '@message' => $tmgmt_message
          ->getMessage(),
      ]);
      break;
  }
}