You are here

class SmartlingTranslatorUi in TMGMT Translator Smartling 8

Same name and namespace in other branches
  1. 8.4 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi
  2. 8.2 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi
  3. 8.3 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi

Smartling translator UI.

Hierarchy

Expanded class hierarchy of SmartlingTranslatorUi

File

src/SmartlingTranslatorUi.php, line 18
Contains \Drupal\tmgmt_smartling\SmartlingTranslatorUi.

Namespace

Drupal\tmgmt_smartling
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
SmartlingTranslatorUi::buildConfigurationForm public function Form constructor. Overrides TranslatorPluginUiBase::buildConfigurationForm
SmartlingTranslatorUi::checkoutInfo public function Retrieves information about a translation job. Overrides TranslatorPluginUiBase::checkoutInfo
SmartlingTranslatorUi::validateConfigurationForm public function Form validation handler. Overrides TranslatorPluginUiBase::validateConfigurationForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TranslatorPluginUiBase::addConnectButton public function Adds a "Connect" button to a form.
TranslatorPluginUiBase::checkoutInfoWrapper public function Provides a simple wrapper for the checkout info fieldset.
TranslatorPluginUiBase::checkoutSettingsForm public function Form callback for the checkout settings form. Overrides TranslatorPluginUiInterface::checkoutSettingsForm 3
TranslatorPluginUiBase::reviewDataItemElement public function Form callback for the data item element form. Overrides TranslatorPluginUiInterface::reviewDataItemElement 1
TranslatorPluginUiBase::reviewForm public function Form callback for the job item review form. Overrides TranslatorPluginUiInterface::reviewForm
TranslatorPluginUiBase::reviewFormSubmit public function Submit callback for the job item review form. Overrides TranslatorPluginUiInterface::reviewFormSubmit
TranslatorPluginUiBase::reviewFormValidate public function Validation callback for the job item review form. Overrides TranslatorPluginUiInterface::reviewFormValidate
TranslatorPluginUiBase::submitConfigurationForm public function Form submission handler. Overrides TranslatorPluginUiInterface::submitConfigurationForm
TranslatorPluginUiBase::submitConnect public function Handles submit call of "Connect" button.