You are here

globallink.module in GlobalLink Connect for Drupal 8

File

globallink.module
View source
<?php

use Drupal\tmgmt\Entity\Job;

/**
 * Implements hook_theme().
 */
function globallink_theme() {
  return [
    'globallink_comments' => [
      'variables' => [
        'comments' => NULL,
      ],
      'template' => 'globallink_comments',
    ],
  ];
}

/**
 * Implements hook_cron().
 */
function globallink_cron() {
  $globallink_translators = \Drupal::entityTypeManager()
    ->getStorage('tmgmt_translator')
    ->loadByProperties([
    'plugin' => 'globallink',
  ]);

  /** @var \Drupal\tmgmt\TranslatorInterface $translator */
  foreach ($globallink_translators as $translator) {

    /** @var \Drupal\globallink\Plugin\tmgmt\Translator\GlobalLinkTranslator $translator_plugin */
    $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)) {

          /** @var \Drupal\tmgmt\JobInterface $job */
          $job = Job::load(array_shift($job_ids));
          $translator_plugin
            ->retrieveTranslation($completed_translation->ticket, $job);
        }
      }
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_save().
 */
function globallink_tmgmt_message_insert(\Drupal\tmgmt\MessageInterface $entity) {
  $langcode = \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId();

  /** @var \Drupal\tmgmt\JobInterface $job */
  $job = $entity
    ->getJob();

  /** @var \Drupal\tmgmt\TranslatorInterface $translator */

  // Abort if the job has no translator.
  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']);

        /** @var \Drupal\Core\Mail\MailManagerInterface $mail_manager */
        $mail_manager = \Drupal::service('plugin.manager.mail');
        $mail_manager
          ->mail('globallink', 'log', $emails, $langcode, [
          'message' => $entity,
        ]);
      }
    }
  }
}

/**
 * Implements hook_mail().
 */
function globallink_mail($key, &$message, $params) {

  /** @var \Drupal\tmgmt\MessageInterface $tmgmt_message */
  $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;
  }
}

Functions

Namesort descending Description
globallink_cron Implements hook_cron().
globallink_mail Implements hook_mail().
globallink_theme Implements hook_theme().
globallink_tmgmt_message_insert Implements hook_ENTITY_TYPE_save().