public function ModerationSidebarController::translateOverview in Moderation Sidebar 8
Generate a simple list of translations with quick-add buttons.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: An entity.
Return value
array An array as expected by drupal_render().
1 string reference to 'ModerationSidebarController::translateOverview'
File
- src/
Controller/ ModerationSidebarController.php, line 404
Class
- ModerationSidebarController
- Endpoints for the Moderation Sidebar module.
Namespace
Drupal\moderation_sidebar\ControllerCode
public function translateOverview(ContentEntityInterface $entity) {
$entity_type = $entity
->getEntityType();
$entity_type_id = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$account = $this
->currentUser();
if (!\Drupal::moduleHandler()
->moduleExists('content_translation') || !\Drupal::service('content_translation.manager')
->isSupported($entity_type_id)) {
throw new AccessDeniedHttpException();
}
$langcode = $entity
->language()
->getId();
$entity_type_id = $entity
->getEntityTypeId();
/** @var \Drupal\Core\Entity\TranslatableRevisionableStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
if ($storage instanceof TranslatableRevisionableStorageInterface) {
$latest_revision_id = $storage
->getLatestTranslationAffectedRevisionId($entity
->id(), $langcode);
if ($latest_revision_id < $entity
->getRevisionId()) {
$entity = $storage
->load($entity
->id())
->getTranslation($langcode);
}
}
$build = $this
->getBackButton($entity);
$can_create = $account
->hasPermission('translate any entity');
if (!$can_create) {
$granularity = $entity_type
->getPermissionGranularity();
$permission = $granularity === 'bundle' ? "translate {$bundle} {$entity_type_id}" : "translate {$entity_type_id}";
$can_create = $account
->hasPermission($permission);
}
$languages = $this
->languageManager()
->getLanguages();
$translation_languages = $entity
->getTranslationLanguages();
if ($this
->languageManager()
->isMultilingual()) {
// Determine whether the current entity is translatable.
$translatable = FALSE;
foreach ($entity
->getFieldDefinitions() as $instance) {
if ($instance
->isTranslatable()) {
$translatable = TRUE;
break;
}
}
$translations = [];
foreach ($languages as $language) {
$langcode = $language
->getId();
if ($langcode === $entity
->language()
->getId()) {
continue;
}
// Get the latest revision for the current $langcode.
if ($storage instanceof TranslatableRevisionableStorageInterface) {
$latest_revision = NULL;
$latest_revision_id = $storage
->getLatestTranslationAffectedRevisionId($entity
->id(), $langcode);
if ($latest_revision_id && ($latest_revision = $storage
->loadRevision($latest_revision_id))) {
$latest_revision = $latest_revision
->getTranslation($langcode);
}
}
else {
$latest_revision = $storage
->loadRevision($storage
->getLatestRevisionId($entity
->id()))
->getTranslation($langcode);
}
$latest_translation = FALSE;
$entity_has_translation = array_key_exists($langcode, $translation_languages);
// This would happen when a translation only has a draft revision and
// make sure we do not list removed translations.
if (!$entity_has_translation && $latest_revision && !$latest_revision
->wasDefaultRevision()) {
$latest_translation = TRUE;
}
if ($entity_has_translation || $latest_translation) {
$translation = !$latest_translation ? $entity
->getTranslation($langcode) : $latest_revision;
$translation_links = [
'#title' => $this
->t('View'),
'#type' => 'link',
'#url' => $translation
->toUrl($latest_translation ? 'latest-version' : 'canonical'),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
],
'title' => $this
->t('View: @label', [
'@label' => $translation
->label(),
]),
],
];
if ($translation
->access('edit', $account)) {
$translation_links[] = [
'#title' => $this
->t('Edit'),
'#type' => 'link',
'#url' => $translation
->toUrl('edit-form'),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
],
'title' => $this
->t('Edit: @label', [
'@label' => $translation
->label(),
]),
],
];
}
if ($latest_revision
->getRevisionId() != $translation
->getRevisionId() && $latest_revision->moderation_state->value == 'draft') {
$translation_links[] = [
'#title' => $this
->t('View existing draft'),
'#type' => 'link',
'#url' => $translation
->toUrl('latest-version'),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
],
'title' => $this
->t('View: @label', [
'@label' => $latest_revision
->label(),
]),
],
];
}
$translations[] = [
'language' => $language
->getName(),
'state' => $this
->getStateLabel($translation),
'links' => $translation_links,
];
}
elseif ($can_create && $translatable) {
$translations[] = [
'language' => $language
->getName(),
'links' => [
[
'#title' => $this
->t('Create translation'),
'#type' => 'link',
'#url' => Url::fromRoute("entity.{$entity_type_id}.content_translation_add", [
'source' => $entity
->getUntranslated()
->language()
->getId(),
'target' => $language
->getId(),
$entity_type_id => $entity
->id(),
], [
'language' => $language,
]),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
],
],
],
],
];
}
}
$build[] = [
'#theme' => 'moderation_sidebar_translations',
'#current_language' => $entity
->language()
->getName(),
'#translations' => $translations,
'#view_all' => [
'#title' => $this
->t('View all translations'),
'#type' => 'link',
'#url' => Url::fromRoute('entity.node.content_translation_overview', [
'node' => $entity
->id(),
]),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
],
],
],
];
}
return $build;
}