You are here

SmartlingTranslatorUi.php in TMGMT Translator Smartling 8

File

src/SmartlingTranslatorUi.php
View source
<?php

/**
 * @file
 * Contains \Drupal\tmgmt_smartling\SmartlingTranslatorUi.
 */
namespace Drupal\tmgmt_smartling;

use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\TranslatorPluginUiBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Smartling translator UI.
 */
class SmartlingTranslatorUi extends TranslatorPluginUiBase {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    /** @var \Drupal\tmgmt\TranslatorInterface $translator */
    $translator = $form_state
      ->getFormObject()
      ->getEntity();
    $form['api_url'] = [
      '#type' => 'textfield',
      '#title' => t('API URL'),
      '#default_value' => $translator
        ->getSetting('api_url'),
      '#size' => 25,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => t('Set api url. Default: @api_url', [
        '@api_url' => $translator
          ->getSetting('api_url'),
      ]),
    ];
    $form['project_id'] = [
      '#type' => 'textfield',
      '#title' => t('Project Id'),
      '#default_value' => $translator
        ->getSetting('project_id'),
      '#size' => 25,
      '#maxlength' => 25,
      '#required' => TRUE,
    ];
    $form['key'] = [
      '#type' => 'textfield',
      '#title' => t('Key'),
      '#default_value' => $translator
        ->getSetting('key'),
      '#size' => 40,
      '#maxlength' => 40,
      '#required' => TRUE,
    ];
    $form['orgID'] = [
      '#type' => 'textfield',
      '#title' => t('orgID'),
      '#size' => 40,
      '#maxlength' => 40,
      '#default_value' => $translator
        ->getSetting('orgID'),
      '#required' => FALSE,
    ];
    $form['contextUsername'] = [
      '#type' => 'textfield',
      '#title' => t('Username for context retrieval'),
      '#size' => 40,
      '#maxlength' => 40,
      '#default_value' => $translator
        ->getSetting('contextUsername'),
      '#required' => FALSE,
    ];
    $form['context_silent_user_switching'] = [
      '#type' => 'checkbox',
      '#title' => t('Context silent user authentication'),
      '#description' => t('If checked, Smartling won\'t trigger hook_login and hook_logout during user authentication for retrieving context.'),
      '#default_value' => $translator
        ->getSetting('context_silent_user_switching'),
      '#required' => FALSE,
    ];
    $form['context_skip_host_verifying'] = [
      '#type' => 'checkbox',
      '#title' => t('Skip host verification'),
      '#description' => t('If checked, curl won\'t verify host during retrieving context (CURLOPT_SSL_VERIFYHOST = 0). Use only for developing and testing purposes on NON PRODUCTION environments.'),
      '#default_value' => $translator
        ->getSetting('context_skip_host_verifying'),
      '#required' => FALSE,
    ];
    $form['retrieval_type'] = [
      '#type' => 'select',
      '#title' => t('The desired format for download'),
      '#default_value' => $translator
        ->getSetting('retrieval_type'),
      '#options' => [
        'pending' => t('Smartling returns any translations (including non-published translations)'),
        'published' => t('Smartling returns only published/pre-published translations'),
        'pseudo' => t('Smartling returns a modified version of the original text'),
      ],
      '#required' => FALSE,
    ];
    $form['auto_authorize_locales'] = [
      '#type' => 'checkbox',
      '#title' => t('Automatically authorize content for translation in Smartling'),
      // @todo Add description to display full URL.
      '#default_value' => $translator
        ->getSetting('auto_authorize_locales'),
      '#required' => FALSE,
    ];
    $form['callback_url_use'] = [
      '#type' => 'checkbox',
      '#title' => t('Use Smartling callback: /smartling/callback/%cron_key'),
      // @todo Add description to display full URL.
      '#default_value' => $translator
        ->getSetting('callback_url_use'),
      '#required' => FALSE,
    ];

    // Any visible, writable wrapper can potentially be used for the files
    // directory, including a remote file system that integrates with a CDN.
    foreach (\Drupal::service('stream_wrapper_manager')
      ->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE) as $scheme => $description) {
      $options[$scheme] = $description;
    }
    if (!empty($options)) {
      $form['scheme'] = [
        '#type' => 'radios',
        '#title' => t('Download method'),
        '#default_value' => $translator
          ->getSetting('scheme'),
        '#options' => $options,
        '#description' => t('Choose the location where exported files should be stored. The usage of a protected location (e.g. private://) is recommended to prevent unauthorized access.'),
      ];
    }
    $form['custom_regexp_placeholder'] = [
      '#type' => 'textfield',
      '#title' => t('Custom placeholder (regular expression)'),
      '#description' => t('The content matching this regular expression will not be editable by translators in Smartling.'),
      '#size' => 40,
      '#maxlength' => 80,
      '#default_value' => $translator
        ->getSetting('custom_regexp_placeholder'),
      '#required' => FALSE,
    ];
    $form['export_format'] = [
      '#type' => 'select',
      '#title' => t('File type'),
      '#description' => t('The file type format that is used to upload/download from Smartling'),
      '#default_value' => $translator
        ->getSetting('export_format'),
      '#options' => [
        'html' => t('HTML'),
        'xlf' => t('XLIFF'),
        'xml' => t('XML'),
      ],
      '#required' => FALSE,
    ];

    // TODO: identical filename task.
    // $form['identical_file_name'] = [
    //   '#type' => 'checkbox',
    //   '#title' => t('Use identical file names for jobs that contain identical content'),
    //   '#description' => t('Generated file will have identical name for jobs that have identical content.'),
    //   '#default_value' => $translator->getSetting('identical_file_name'),
    //   '#required' => FALSE,
    // ];
    $basic_auth_defaults = $translator
      ->getSetting('basic_auth');
    $form['enable_basic_auth'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable basic auth for context.'),
      '#default_value' => $translator
        ->getSetting('enable_basic_auth'),
    ];
    $form['basic_auth'] = [
      '#type' => 'details',
      '#title' => t('Basic auth'),
      '#open' => TRUE,
      '#states' => [
        'visible' => [
          'input[name="settings[enable_basic_auth]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['basic_auth']['login'] = [
      '#type' => 'textfield',
      '#title' => t('Login'),
      '#default_value' => $basic_auth_defaults['login'],
      '#states' => [
        'required' => [
          'input[name="settings[enable_basic_auth]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['basic_auth']['password'] = [
      '#type' => 'textfield',
      '#title' => t('Password'),
      '#default_value' => $basic_auth_defaults['password'],
      '#states' => [
        'required' => [
          'input[name="settings[enable_basic_auth]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form, $form_state);

    /** @var \Drupal\tmgmt\TranslatorInterface $translator */
    $translator = $form_state
      ->getFormObject()
      ->getEntity();
    $supported_remote_languages = $translator
      ->getPlugin()
      ->getSupportedRemoteLanguages($translator);
    if (empty($supported_remote_languages)) {
      $form_state
        ->setErrorByName('settings][project_id', t('The "Project ID", the "Client key" or both are not correct.'));
      $form_state
        ->setErrorByName('settings][key', t('The "Project ID", the "Client key" or both are not correct.'));
    }
    if ($translator
      ->getSetting('enable_basic_auth')) {
      $auth_settings = $translator
        ->getSetting('basic_auth');
      if (empty($auth_settings['login']) || empty($auth_settings['password'])) {
        $form_state
          ->setErrorByName('settings][basic_auth', t('Please fill in both login and password (HTTP basic authentication credentials).'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function checkoutInfo(JobInterface $job) {

    // If the job is finished, it's not possible to import translations anymore.
    if ($job
      ->isFinished()) {
      return parent::checkoutInfo($job);
    }
    $output = [];
    try {
      $output = array(
        '#type' => 'fieldset',
        '#title' => t('Import translated file'),
      );
      $output['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Download'),
        '#submit' => [
          'tmgmt_smartling_download_file_submit',
        ],
      );
      $output = $this
        ->checkoutInfoWrapper($job, $output);
    } catch (\Exception $e) {
    }
    return $output;
  }

}

Classes

Namesort descending Description
SmartlingTranslatorUi Smartling translator UI.