<?php
namespace Drupal\tmgmt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\tmgmt\Form\SourceOverviewForm;
class SourcePluginUiBase extends PluginBase implements SourcePluginUiInterface {
public function reviewForm(array $form, FormStateInterface $form_state, JobItemInterface $item) {
return $form;
}
public function reviewDataItemElement(array $form, FormStateInterface $form_state, $data_item_key, $parent_key, array $data_item, JobItemInterface $item) {
return $form;
}
public function reviewFormValidate(array $form, FormStateInterface $form_state, JobItemInterface $item) {
}
public function reviewFormSubmit(array $form, FormStateInterface $form_state, JobItemInterface $item) {
}
public function overviewForm(array $form, FormStateInterface $form_state, $type) {
$form += $this
->overviewSearchFormPart($form, $form_state, $type);
$form['#attached']['library'][] = 'tmgmt/admin';
$form['items'] = array(
'#type' => 'tableselect',
'#header' => $this
->overviewFormHeader($type),
'#empty' => $this
->t('No source items matching given criteria have been found.'),
);
return $form;
}
public function overviewFormValidate(array $form, FormStateInterface $form_state, $type) {
}
public function overviewFormSubmit(array $form, FormStateInterface $form_state, $type) {
if ($this
->overviewSearchFormRedirect($form, $form_state, $type)) {
return;
}
$target_language = $form_state
->getValue('target_language');
if ($target_language == SourceOverviewForm::ALL) {
$target_languages = array_keys(tmgmt_available_languages());
}
elseif ($target_language == SourceOverviewForm::MULTIPLE) {
$target_languages = array_filter($form_state
->getValue('target_languages'));
}
else {
$target_languages = [
$target_language,
];
}
$enforced_source_language = NULL;
if ($form_state
->getValue('source_language') != SourceOverviewForm::SOURCE) {
$enforced_source_language = $form_state
->getValue('source_language');
}
$skipped_count = 0;
$job_items_by_source_language = [];
foreach (array_filter($form_state
->getValue('items')) as $item_id) {
$job_item = tmgmt_job_item_create($this->pluginId, $type, $item_id);
$source_language = $enforced_source_language ? $enforced_source_language : $job_item
->getSourceLangCode();
if (in_array($source_language, $job_item
->getExistingLangCodes())) {
$job_items_by_source_language[$source_language][$item_id] = $job_item;
}
else {
$skipped_count++;
}
}
$jobs = [];
$remove_job_item_ids = [];
foreach ($target_languages as $target_language) {
foreach ($job_items_by_source_language as $source_language => $job_items) {
if ($source_language == $target_language) {
continue;
}
$job = tmgmt_job_create($source_language, $target_language, \Drupal::currentUser()
->id());
$job_empty = TRUE;
foreach ($job_items as $id => $job_item) {
try {
$job
->addItem($job_item
->getPlugin(), $job_item
->getItemType(), $job_item
->getItemId());
$remove_job_item_ids[$job_item
->id()] = $job_item
->id();
$job_empty = FALSE;
} catch (\Exception $e) {
unset($job_items_by_source_language[$source_language][$id]);
$this
->messenger()
->addError($e
->getMessage());
}
}
if (!$job_empty) {
$jobs[] = $job;
}
}
}
if ($jobs) {
if ($enforced_source_language) {
$this
->messenger()
->addWarning($this
->t('You have enforced the job source language which most likely resulted in having a translation of your original content as the job source text. You should review the job translation received from the translator carefully to prevent the content quality loss.'));
if ($skipped_count) {
$languages = \Drupal::languageManager()
->getLanguages();
$this
->messenger()
->addStatus(\Drupal::translation()
->formatPlural($skipped_count, 'One item skipped as for the language @language it was not possible to retrieve a translation.', '@count items skipped as for the language @language it was not possible to retrieve a translations.', [
'@language' => $languages[$enforced_source_language]
->getName(),
]));
}
}
\Drupal::service('tmgmt.job_checkout_manager')
->checkoutAndRedirect($form_state, $jobs);
}
else {
$this
->messenger()
->addError($this
->t('From the selection you made it was not possible to create any translation job.'));
}
}
public function hook_views_default_views() {
return array();
}
public function overviewSearchFormPart(array $form, FormStateInterface $form_state, $type) {
$form_state
->set('entity_type', $type);
$form_state
->set('plugin_id', $this->pluginId);
$form['#attached']['library'][] = 'tmgmt/source_search_form';
$form['search_wrapper'] = array(
'#prefix' => '<div class="tmgmt-sources-wrapper">',
'#suffix' => '</div>',
'#weight' => -15,
);
$form['search_wrapper']['search'] = array(
'#tree' => TRUE,
);
$form['search_wrapper']['search_submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#weight' => 90,
);
$form['search_wrapper']['search_cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#weight' => 100,
);
return $form;
}
protected function getLanguageHeader() {
$languages = array();
foreach (\Drupal::languageManager()
->getLanguages() as $langcode => $language) {
$languages['langcode-' . $langcode] = array(
'data' => $language
->getName(),
);
}
return $languages;
}
public function overviewSearchFormRedirect(array $form, FormStateInterface $form_state, $type) {
if ($form_state
->getTriggeringElement()['#id'] == 'edit-search-cancel') {
$form_state
->setRedirect('tmgmt.source_overview', array(
'plugin' => $this->pluginId,
'item_type' => $type,
));
return TRUE;
}
elseif ($form_state
->getTriggeringElement()['#id'] == 'edit-search-submit') {
$query = array();
foreach ($form_state
->getValue('search') as $key => $value) {
$query[$key] = $value;
}
$form_state
->setRedirect('tmgmt.source_overview', array(
'plugin' => $this->pluginId,
'item_type' => $type,
), array(
'query' => $query,
));
return TRUE;
}
return FALSE;
}
function buildTranslationStatus($status, JobItemInterface $job_item = NULL) {
switch ($status) {
case 'original':
$label = t('Original language');
$icon = 'core/misc/icons/bebebe/house.svg';
break;
case 'missing':
$label = t('Not translated');
$icon = 'core/misc/icons/bebebe/ex.svg';
break;
case 'outofdate':
$label = t('Translation Outdated');
$icon = \Drupal::service('extension.list.module')
->getPath('tmgmt') . '/icons/outdated.svg';
break;
default:
$label = t('Translation up to date');
$icon = 'core/misc/icons/73b355/check.svg';
}
$build['source'] = [
'#theme' => 'image',
'#uri' => $icon,
'#title' => $label,
'#alt' => $label,
];
if ($job_item) {
$url = $job_item
->toUrl();
if ($job_item
->isActive() && $job_item
->getJob() && $job_item
->getJob()
->isUnprocessed()) {
$url = $job_item
->getJob()
->toUrl();
}
$url
->setOption('query', \Drupal::destination()
->getAsArray());
$item_icon = $job_item
->getStateIcon();
if ($item_icon) {
$item_icon['#title'] = $this
->t('Active job item: @state', [
'@state' => $item_icon['#title'],
]);
$build['job_item'] = [
'#type' => 'link',
'#url' => $url,
'#title' => $item_icon,
];
}
}
return $build;
}
}