View source
<?php
namespace Drupal\lingotek\Element;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Lingotek;
trait LingotekTargetTrait {
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 (in_array($target_status, [
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_DELETED,
])) {
$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(),
]);
}
if ($target_status == Lingotek::STATUS_DISABLED) {
$url = NULL;
}
}
return $url;
}
protected function getSecondaryTargetActionUrls(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);
$langcode_upper = strtoupper($langcode);
$actions = [];
if ($document_id) {
if (in_array($target_status, [
Lingotek::STATUS_REQUEST,
Lingotek::STATUS_DELETED,
])) {
$actions[] = [
'title' => $this
->t('Request translation'),
'url' => Url::fromRoute('lingotek.entity.request_translation', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
];
}
if ($target_status == Lingotek::STATUS_PENDING) {
$actions[] = [
'title' => $this
->t('Check translation status'),
'url' => Url::fromRoute('lingotek.entity.check_target', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
'new_window' => FALSE,
];
$actions[] = [
'title' => $this
->t('Open in Lingotek Workbench'),
'url' => Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]),
'new_window' => TRUE,
];
}
if ($target_status == Lingotek::STATUS_READY) {
$actions[] = [
'title' => $this
->t('Download translation'),
'url' => Url::fromRoute('lingotek.entity.download', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
'new_window' => FALSE,
];
$actions[] = [
'title' => $this
->t('Open in Lingotek Workbench'),
'url' => Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]),
'new_window' => TRUE,
];
}
if ($target_status == Lingotek::STATUS_ERROR) {
$actions[] = [
'title' => $this
->t('Retry request'),
'url' => Url::fromRoute('lingotek.entity.request_translation', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
'new_window' => FALSE,
];
$actions[] = [
'title' => $this
->t('Retry download'),
'url' => Url::fromRoute('lingotek.entity.download', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
'new_window' => FALSE,
];
}
if ($target_status == Lingotek::STATUS_CURRENT) {
if ($entity
->hasLinkTemplate('canonical') && $entity
->hasTranslation($langcode)) {
$actions[] = [
'title' => $this
->t('View translation'),
'url' => $entity
->getTranslation($langcode)
->toUrl(),
'new_window' => FALSE,
];
}
$actions[] = [
'title' => $this
->t('Open in Lingotek Workbench'),
'url' => Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]),
'new_window' => TRUE,
];
}
if ($target_status == Lingotek::STATUS_INTERMEDIATE) {
if ($entity
->hasLinkTemplate('canonical') && $entity
->hasTranslation($langcode)) {
$actions[] = [
'title' => $this
->t('View translation'),
'url' => $entity
->getTranslation($langcode)
->toUrl(),
'new_window' => FALSE,
];
}
$actions[] = [
'title' => $this
->t('Open in Lingotek Workbench'),
'url' => Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]),
'new_window' => TRUE,
];
}
if ($target_status == Lingotek::STATUS_EDITED) {
if ($entity
->hasLinkTemplate('canonical') && $entity
->hasTranslation($langcode)) {
$actions[] = [
'title' => $this
->t('View translation'),
'url' => $entity
->getTranslation($langcode)
->toUrl(),
'new_window' => FALSE,
];
}
$actions[] = [
'title' => $this
->t('Open in Lingotek Workbench'),
'url' => Url::fromRoute('lingotek.workbench', [
'doc_id' => $document_id,
'locale' => $locale,
]),
'new_window' => TRUE,
];
}
}
if (in_array($target_status, [
Lingotek::STATUS_UNTRACKED,
Lingotek::STATUS_ARCHIVED,
])) {
if ($entity
->hasLinkTemplate('canonical') && $entity
->hasTranslation($langcode)) {
$actions[] = [
'title' => $this
->t('View translation'),
'url' => $entity
->getTranslation($langcode)
->toUrl(),
'new_window' => FALSE,
];
}
if ($document_id) {
$actions[] = [
'title' => $this
->t('Request translation'),
'url' => Url::fromRoute('lingotek.entity.request_translation', [
'doc_id' => $document_id,
'locale' => $locale,
], [
'query' => $this
->getDestinationWithQueryArray(),
]),
'new_window' => FALSE,
];
}
}
return $actions;
}
protected function getDestinationWithQueryArray() {
return [
'destination' => \Drupal::request()
->getRequestUri(),
];
}
protected function getTargetStatusText($status, $langcode, $translation_exists = NULL) {
$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');
case Lingotek::STATUS_DELETED:
if (!isset($translation_exists)) {
return $language
->label() . ' - ' . $this
->t('This target was deleted in Lingotek.');
}
if ($translation_exists) {
return $language
->label() . ' - ' . $this
->t('This target was deleted in Lingotek and the translation exists.');
}
return $language
->label() . ' - ' . $this
->t('This target was deleted in Lingotek and the translation does not exist.');
case Lingotek::STATUS_ARCHIVED:
if (!isset($translation_exists)) {
return $language
->label() . ' - ' . $this
->t('This target was archived in Lingotek.');
}
if ($translation_exists) {
return $language
->label() . ' - ' . $this
->t('This target was archived in Lingotek and the translation exists.');
}
return $language
->label() . ' - ' . $this
->t('This target was archived in Lingotek and the translation does not exist.');
default:
return $language
->label() . ' - ' . ucfirst(strtolower($status));
}
}
}