You are here

tmgmt_smartling.module in TMGMT Translator Smartling 8

Contains

File

tmgmt_smartling.module
View source
<?php

/**
 * @file
 * Contains
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\views\ViewExecutable;
function tmgmt_smartling_download_file_submit(array &$form, FormStateInterface $form_state) {

  /* @var \Drupal\tmgmt\Entity\Job $job */
  $job = $form_state
    ->getFormObject()
    ->getEntity();

  /* @var \Drupal\tmgmt_smartling\Smartling\SmartlingApi $smartlingApi */
  tmgmt_smartling_download_file($job);
}
function tmgmt_smartling_download_file(JobInterface $job) {
  try {
    $smartlingApi = $job
      ->getTranslatorPlugin()
      ->getSmartlingApi($job
      ->getTranslator());
    $retrieval_type = $job
      ->getTranslator()
      ->getSetting('retrieval_type');
    $filename = $job
      ->getTranslatorPlugin()
      ->getFileName($job);
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $xml = $smartlingApi
      ->downloadFile($filename, $job
      ->getRemoteTargetLanguage(), [
      'retrievalType' => $retrieval_type,
    ]);
  } catch (\Exception $e) {
    Drupal::logger('smartling')
      ->error($e
      ->getMessage());
    return FALSE;
  }
  $path = $job
    ->getSetting('scheme') . '://tmgmt_smartling_translations/' . $job
    ->getTranslatorPlugin()
    ->getFileName($job);
  $dirname = dirname($path);
  if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && ($file = file_save_data($xml, $path, FILE_EXISTS_REPLACE))) {
    $plugin = \Drupal::service('plugin.manager.tmgmt_file.format')
      ->createInstance($extension);
    if ($plugin) {

      // Validate the file on job.
      if (!$plugin
        ->validateImport($file
        ->getFileUri(), $job)) {
        $job
          ->addMessage('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ], 'error');
        \Drupal::logger('tmgmt_smartling')
          ->error('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ]);
        return FALSE;
      }
      else {
        try {

          // Set active state for all job items of a job in order to be able
          // force translation. It allows to override existing translations
          // that might be in an "Accepted" state.
          // @see JobItem::addTranslatedData() method.
          foreach ($job
            ->getItems() as $item) {
            $item
              ->setState(JobItemInterface::STATE_ACTIVE);
          }

          // Validation successful, start import.
          $job
            ->addTranslatedData($plugin
            ->import($file
            ->getFileUri(), $job));
          $job
            ->addMessage('Successfully imported file.');
        } catch (Exception $e) {
          $job
            ->addMessage('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ], 'error');
          \Drupal::logger('tmgmt_smartling')
            ->error('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ]);
          return FALSE;
        }
      }
    }
  }
  \Drupal::logger('tmgmt_smartling')
    ->info('Translation for "@filename" was successfully downloaded.', [
    '@filename' => $filename,
  ]);
  return TRUE;
}

/**
 * Implements hook_theme().
 */
function tmgmt_smartling_theme() {
  return [
    'smartling_dashboard_link' => [
      'variables' => [
        'proj_id' => '',
        'file_name' => '',
      ],
    ],
    'tmgmt_smartling_xml_template' => [
      'path' => drupal_get_path('module', 'tmgmt_smartling') . '/templates',
      'template' => 'tmgmt-smartling-xml-template',
      'variables' => [
        'items' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_views_data_alter().
 */
function tmgmt_smartling_views_data_alter(array &$data) {
  $data['tmgmt_job']['smartling_dashboard'] = array(
    'title' => t('Link to Smartling Dashboard'),
    'field' => array(
      'title' => t('Link to Smartling Dashboard'),
      //'help' => t('Flags a specific node type.'),
      'id' => 'tmgmt_smartling_dashboard_link',
    ),
  );
}

/**
 * Implements hook_views_pre_view().
 */
function tmgmt_smartling_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view
    ->id() === 'tmgmt_translation_all_job_items') {
    $handlers = $view
      ->getHandlers('field');
    foreach ($handlers as $name => $value) {
      $view
        ->removeHandler($view->current_display, 'field', $name);
    }
    $view
      ->addHandler($view->current_display, 'field', 'tmgmt_job_item', 'tmgmt_job_item_bulk_form', array(
      'id' => 'tmgmt_job_item_bulk_form',
      'table' => 'tmgmt_job_item',
      'field' => 'tmgmt_job_item_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_item',
      'plugin_id' => 'bulk_form',
      'weight' => -10,
    ));
    foreach ($handlers as $name => $value) {
      $view
        ->addHandler($view->current_display, 'field', 'tmgmt_job_item', $name, $value);
    }
  }
  if ($view
    ->id() === 'tmgmt_job_overview') {
    $handlers = $view
      ->getHandlers('field');
    $view
      ->removeHandler($view->current_display, 'field', 'operations');
    $view
      ->addHandler($view->current_display, 'field', 'tmgmt_job', 'smartling_dashboard', array(
      'id' => 'smartling_dashboard',
      'table' => 'tmgmt_job',
      'field' => 'smartling_dashboard',
      'group_type' => 'group',
      'label' => 'Smartling',
      'hide_alter_empty' => 1,
      'selected_actions' => [],
      'entity_type' => 'tmgmt_job',
      'plugin_id' => 'tmgmt_smartling_dashboard_link',
      'weight' => -10,
    ));
    $view
      ->addHandler($view->current_display, 'field', 'tmgmt_job_item', 'operations', $handlers['operations']);
  }
}