View source
<?php
namespace Drupal\lingotek\Element;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Lingotek;
class LingotekTargetStatuses extends RenderElement {
public function getInfo() {
return [
'#pre_render' => [
[
$this,
'preRender',
],
],
'#theme' => 'lingotek_target_statuses',
'#attached' => [
'library' => [
'lingotek/lingotek',
],
],
'#cache' => [
'max-age' => 0,
],
];
}
public function preRender(array $element) {
if (isset($element['#entity'])) {
$statuses = $this
->getTranslationsStatuses($element['#entity'], $element['#source_langcode'], $element['#statuses']);
}
elseif (isset($element['#ui_component'])) {
$statuses = $this
->getTranslationsStatusesForUI($element['#ui_component'], $element['#source_langcode'], $element['#statuses']);
}
$element['#statuses'] = $statuses;
return $element;
}
protected function getTranslationsStatuses(ContentEntityInterface &$entity, $source_langcode, array $statuses) {
$translations = [];
$languages = \Drupal::languageManager()
->getLanguages();
$languages = array_filter($languages, function (LanguageInterface $language) {
$configLanguage = ConfigurableLanguage::load($language
->getId());
return \Drupal::service('lingotek.configuration')
->isLanguageEnabled($configLanguage);
});
foreach ($statuses as $langcode => $status) {
if ($langcode !== $source_langcode && array_key_exists($langcode, $languages)) {
if ($entity
->hasTranslation($langcode) && $status == Lingotek::STATUS_REQUEST) {
$translations[$langcode] = [
'status' => Lingotek::STATUS_UNTRACKED,
'url' => $this
->getTargetActionUrl($entity, Lingotek::STATUS_UNTRACKED, $langcode),
'new_window' => FALSE,
];
}
else {
$translations[$langcode] = [
'status' => $status,
'url' => $this
->getTargetActionUrl($entity, $status, $langcode),
'new_window' => in_array($status, [
Lingotek::STATUS_CURRENT,
Lingotek::STATUS_INTERMEDIATE,
Lingotek::STATUS_EDITED,
]),
];
}
}
array_walk($languages, function ($language, $langcode) use ($entity, &$translations) {
if (!isset($translations[$langcode]) && $langcode !== $entity
->getUntranslated()
->language()
->getId()) {
$translations[$langcode] = [
'status' => Lingotek::STATUS_REQUEST,
'url' => $this
->getTargetActionUrl($entity, Lingotek::STATUS_REQUEST, $langcode),
'new_window' => FALSE,
];
}
});
}
foreach ($languages as $langcode => $language) {
if (!isset($translations[$langcode]) && $entity
->hasTranslation($langcode) && $source_langcode !== $langcode) {
$translations[$langcode] = [
'status' => Lingotek::STATUS_UNTRACKED,
'url' => NULL,
'new_window' => FALSE,
];
}
}
ksort($translations);
foreach ($translations as $langcode => &$translation) {
$translation['status_text'] = $this
->getTargetStatusText($translation['status'], $langcode);
$translation['language'] = $langcode;
}
return $translations;
}
protected function getTranslationsStatusesForUI($component, $source_langcode, array $statuses) {
$translations = [];
$languages = \Drupal::languageManager()
->getLanguages();
$languages = array_filter($languages, function (LanguageInterface $language) {
$configLanguage = ConfigurableLanguage::load($language
->getId());
return \Drupal::service('lingotek.configuration')
->isLanguageEnabled($configLanguage);
});
foreach ($statuses as $langcode => $status) {
if ($langcode !== $source_langcode && array_key_exists($langcode, $languages)) {
$translations[$langcode] = [
'status' => $status,
'url' => $this
->getTargetActionUrlForUI($component, $status, $langcode),
'new_window' => in_array($status, [
Lingotek::STATUS_CURRENT,
Lingotek::STATUS_INTERMEDIATE,
Lingotek::STATUS_EDITED,
]),
];
}
array_walk($languages, function ($language, $langcode) use ($component, &$translations) {
if (!isset($translations[$langcode]) && $langcode !== 'en') {
$translations[$langcode] = [
'status' => Lingotek::STATUS_REQUEST,
'url' => $this
->getTargetActionUrlForUI($component, Lingotek::STATUS_REQUEST, $langcode),
'new_window' => FALSE,
];
}
});
}
ksort($translations);
foreach ($translations as $langcode => &$translation) {
$translation['status_text'] = $this
->getTargetStatusText($translation['status'], $langcode);
$translation['language'] = $langcode;
}
return $translations;
}
protected function getTargetStatusText($status, $langcode) {
$language = ConfigurableLanguage::load($langcode);
switch ($status) {
case Lingotek::STATUS_UNTRACKED:
return $language
->label() . ' - ' . $this
->t('Translation exists, but it is not being tracked by Lingotek');
case Lingotek::STATUS_REQUEST:
return $language
->label() . ' - ' . $this
->t('Request translation');
case Lingotek::STATUS_PENDING:
return $language
->label() . ' - ' . $this
->t('In-progress');
case Lingotek::STATUS_READY:
return $language
->label() . ' - ' . $this
->t('Ready for Download');
case Lingotek::STATUS_INTERMEDIATE:
return $language
->label() . ' - ' . $this
->t('In-progress (interim translation downloaded)');
case Lingotek::STATUS_CURRENT:
return $language
->label() . ' - ' . $this
->t('Current');
case Lingotek::STATUS_EDITED:
return $language
->label() . ' - ' . $this
->t('Not current');
case Lingotek::STATUS_ERROR:
return $language
->label() . ' - ' . $this
->t('Error');
case Lingotek::STATUS_CANCELLED:
return $language
->label() . ' - ' . $this
->t('Cancelled by user');
default:
return $language
->label() . ' - ' . ucfirst(strtolower($status));
}
}
protected function getTargetActionUrl(ContentEntityInterface &$entity, $target_status, $langcode) {
$url = NULL;
$document_id = \Drupal::service('lingotek.content_translation')
->getDocumentId($entity);
$locale = \Drupal::service('lingotek.language_locale_mapper')
->getLocaleForLangcode($langcode);
if ($document_id) {
if ($target_status == Lingotek::STATUS_REQUEST) {
$url = Url::fromRoute('lingotek.entity.request_translation', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_PENDING) {
$url = Url::fromRoute('lingotek.entity.check_target', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_READY || $target_status == Lingotek::STATUS_ERROR) {
$url = Url::fromRoute('lingotek.entity.download', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_CURRENT || $target_status == Lingotek::STATUS_INTERMEDIATE || $target_status == Lingotek::STATUS_EDITED) {
$url = Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]);
}
if ($target_status == Lingotek::STATUS_UNTRACKED) {
$url = Url::fromRoute('lingotek.entity.request_translation', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]);
}
}
return $url;
}
protected function getTargetActionUrlForUI($component, $target_status, $langcode) {
$url = NULL;
$document_id = \Drupal::service('lingotek.interface_translation')
->getDocumentId($component);
$locale = \Drupal::service('lingotek.language_locale_mapper')
->getLocaleForLangcode($langcode);
if ($document_id) {
if ($target_status == Lingotek::STATUS_REQUEST) {
$url = Url::fromRoute('lingotek.interface_translation.request_translation', [], [
'query' => [
'component' => $component,
'locale' => $locale,
] + $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_PENDING) {
$url = Url::fromRoute('lingotek.interface_translation.check_translation', [], [
'query' => [
'component' => $component,
'locale' => $locale,
] + $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_READY || $target_status == Lingotek::STATUS_ERROR) {
$url = Url::fromRoute('lingotek.interface_translation.download', [], [
'query' => [
'component' => $component,
'locale' => $locale,
] + $this
->getDestinationWithQueryArray(),
]);
}
if ($target_status == Lingotek::STATUS_CURRENT || $target_status == Lingotek::STATUS_INTERMEDIATE || $target_status == Lingotek::STATUS_EDITED) {
$url = Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]);
}
if ($target_status == Lingotek::STATUS_UNTRACKED) {
$url = Url::fromRoute('lingotek.interface_translation.request_translation', [], [
'query' => [
'component' => $component,
'locale' => $locale,
] + $this
->getDestinationWithQueryArray(),
]);
}
}
return $url;
}
protected function getDestinationWithQueryArray() {
return [
'destination' => \Drupal::request()
->getRequestUri(),
];
}
}