tmgmt_xtm-8.x-5.x-dev/tmgmt_xtm.module
tmgmt_xtm.module
<?php
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\TMGMTException;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt_xtm\Plugin\tmgmt\Translator\AbortItem;
use Drupal\tmgmt_xtm\Plugin\tmgmt\Translator\Cart;
use Drupal\tmgmt_xtm\Plugin\tmgmt\Translator\Connector;
use Drupal\tmgmt_xtm\Plugin\tmgmt\Translator\JobEdit;
use Drupal\tmgmt_xtm\XtmTranslatorUi;
use Drupal\tmgmt_xtm\Plugin\tmgmt\Requirement\SoapRequirement;
/**
* @param array $form
* @param FormStateInterface $formState
*/
function tmgmt_xtm_form_tmgmt_job_edit_form_alter(&$form, FormStateInterface $formState)
{
$jobEdit = new JobEdit();
$jobEdit->editForm($form, $formState);
}
/**
* @param $form
* @param FormStateInterface $form_state
*
* @return mixed
*/
function tmgmt_xtm_form_tmgmt_job_abort_form_alter(&$form, FormStateInterface $form_state) {
$form['description'] = ['#markup' => t('This will send a request to the translator to abort the job. After the action the job translation process will be aborted and only remaining action will be resubmitting it.<br>
The task being aborted may be related to other tasks within the project, if you continue all related tasks will also be aborted.')];
return $form;
}
/**
* Form id tmgmt-job-item-abort-form
* @param array $form
* @param FormStateInterface $formState
*/
function tmgmt_xtm_form_tmgmt_job_item_abort_form_alter(&$form, FormStateInterface $formState)
{
$abortItem = new AbortItem();
$abortItem->getForm($form);
}
/**
* Form id tmgmt-job-item-abort-form
* @param array $form
* @param FormStateInterface $formState
*/
function tmgmt_xtm_form_tmgmt_job_item_abort_form_submit(&$form, FormStateInterface $formState)
{
$abortItem = new AbortItem();
$abortItem->submitForm($formState);
}
/**
* Implements hook_form_FORM_ID_alter().
* tmgmt-cart-form
*
* @param $form
* @param FormStateInterface $formState
*/
function tmgmt_xtm_form_tmgmt_cart_form_alter(&$form, FormStateInterface $formState)
{
$cart = new Cart();
$cart->getForm($form);
}
/**
* @param $form
* @param FormState $formState
*/
function tmgmt_xtm_cart_request_translation_form_submit($form, FormState &$formState)
{
$cart = new Cart();
$cart->formSubmit($formState);
}
/**
* Ajax request for available translator's templates.
*
* @param $form
* @param FormState $formState
* @return array
*/
function ajax_tmgmt_xtm_cart_content_templates($form, FormState $formState)
{
$cart = new Cart();
return $cart->getCartContentTemplates($form, $formState);
}
/**
* Implements hook_install().
*/
function tmgmt_xtm_install()
{
$link = Url::fromRoute('entity.tmgmt_translator.collection')->toString();
\Drupal::messenger()
->addWarning(t(
'Go to <a href=":link">Manage > Translation > Providers</a> and add a XTM provider',
[':link' => $link])
);
$settings = \Drupal::configFactory()->getEditable('automated_cron.settings');
$settings->set('interval', 900)->save();
}
/**
* Runs during module update.
*/
function tmgmt_xtm_update_9601()
{
$translators = [];
foreach (tmgmt_translator_load_available(null) as $translator) {
/** @var Translator $translator */
if ($translator->getPluginId() == 'xtm') {
$translators[] = $translator;
}
}
foreach ($translators as $xtm_translator) {
$endpoint = parse_url($xtm_translator->getSetting(Connector::XTM_API_URL));
if ((strpos($endpoint['host'],'xtm-cloud.com') !== false) && ($endpoint['host'] !== 'api.xtm-cloud.com' || $endpoint['host'] !== 'www.api.xtm-cloud.com')) {
$endpoint['host'] = 'api.xtm-cloud.com';
$endpoint = XtmTranslatorUi::unparseUrl($endpoint);
$xtm_translator->setSetting(Connector::XTM_API_URL, $endpoint);
$xtm_translator->save();
}
}
}
/**
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* @return null|string
*/
function tmgmt_xtm_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match)
{
switch ($route_name) {
case 'help.page.tmgmt_xtm':
$text = file_get_contents(dirname(__FILE__) . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
} else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return null;
}
/**
* @param $form
* @param FormState $formState
*/
function tmgmt_xtm_cart_source_overview_validate($form, FormState &$formState)
{
$multiLang = $formState->getValue('multi_lang');
if (empty($multiLang['xtm_translator'])) {
$formState->setError($form['multi_lang']['xtm_translator'], t('Please choose XTM translator.'));
}
if (empty($formState->getValue('target_language'))) {
$formState->setError($form['target_language'], t('Please choose target language.'));
}
}
/**
* Single Batch operation.
*
* @param array $job
* Job informations from XTM cart form.
*
* @param array $job_items
* Job items informations from cart form.
*
* @param array &$context
* Information about the status of the batch.
*/
function tmgmt_xtm_cart_request_operation($job, $job_items, &$context)
{
$cart = new Cart();
$cart->requestOperation($job, $job_items, $context);
}
/**
* @param $job
* @param $job_items
* @param $context
*/
function tmgmt_xtm_cart_request_operation_multiple($job, $job_items, &$context)
{
$cart = new Cart();
$cart->requestOperationMultiple($job, $job_items, $context);
}
/**
* @param Job $job
*/
function tmgmt_xtm_tmgmt_job_delete(Job $job)
{
try {
$translator = $job->getTranslator();
} catch (\Drupal\tmgmt\TMGMTException $exception) {
\Drupal::logger(Connector::XTM_LOGGER)
->notice(t('Could not fetch the translator. Project ID: #@project_id and Job ID: #@job_id.', [
'@project_id' => $job->getReference(),
'@job_id' => $job->id(),
]));
return;
}
if (!$translator || $translator->getPluginId() != 'xtm') {
return;
}
$connector = new Connector();
try {
$response = $connector->checkProjectStatus($job);
} catch (Exception $exception) {
\Drupal::logger(Connector::XTM_LOGGER)
->notice(t('Could not check project status. Project ID: #@project_id and Job ID: #@job_id.', [
'@project_id' => $job->getReference(),
'@job_id' => $job->id(),
]));
return;
}
if ($response->activity == XtmTranslatorUi::XTM_STATE_DELETED) {
\Drupal::messenger()->addMessage(t('Could not delete project in XTM. It has been already deleted.'),
'error');
return;
}
if ($connector->updateProjectActivity($job)) {
} else {
\Drupal::messenger()->addMessage(t('Could not delete project in XTM. Please check settings and/or delete project manually.'),
'error');
}
}
/**
* Check requirements
*
* @param $phase
*/
function tmgmt_xtm_requirements($phase)
{
$requirements = new SoapRequirement();
return $requirements->check($phase);
}
/**
* @param $form
* @param FormStateInterface $formState
*/
function tmgmt_xtm_form_system_cron_settings_alter(&$form, FormStateInterface $formState)
{
$options = [900, 1800, 3600, 10800, 21600, 43200, 86400, 604800];
$form['cron']['interval']['#options'] = [0 => t('Never')] + array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options));
}
