Skip to content

Commit

Permalink
Merge pull request #313 from City-of-Helsinki/dev
Browse files Browse the repository at this point in the history
Dev to main
  • Loading branch information
rpnykanen authored Jun 5, 2024
2 parents 6705d7c + 4d9b9b1 commit 9296bf0
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function infofinland_admin_tools_toolbar_alter(&$items) {
* Implements hook_language_switch_links_alter().
*/
function infofinland_admin_tools_language_switch_links_alter(array &$links) {
$route_match = \Drupal::routeMatch();
$route_match = Drupal::routeMatch();
$entity = FALSE;

// Determine if the current route represents an entity.
Expand All @@ -31,30 +31,62 @@ function infofinland_admin_tools_language_switch_links_alter(array &$links) {
($parameters = $route->getOption('parameters'))
) {
foreach ($parameters as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
if (
isset($options['type']) &&
str_starts_with($options['type'], 'entity:')
) {
$parameter = $route_match->getParameter($name);
if ($parameter instanceof ContentEntityInterface && $parameter->hasLinkTemplate('canonical')) {
if (
$parameter instanceof ContentEntityInterface &&
$parameter->hasLinkTemplate('canonical')
) {
$entity = $parameter;
break;
}
}
}
}

$language_resolver = \Drupal::service('helfi_api_base.default_language_resolver');
$primary_languages = $language_resolver->getDefaultLanguages();

// UHF-10175 routematch returns old revision in some cases
if (
$entity instanceof ContentEntityInterface &&
$entity->getEntityType()->isRevisionable() &&
!$entity->isLatestRevision()
) {
try {
$entity_storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
$entity = $entity_storage->load($entity->id());
}
catch(\Exception $e) {
// Do nothing.
}
}

// Compare the links with current entity and check for possible translations.
foreach ($links as $lang_code => &$link) {
$link['#abbreviation'] = $lang_code;

if ($entity && $entity instanceof ContentEntityInterface) {
if (
!$entity->hasTranslation($lang_code) ||
(
method_exists($entity->getTranslation($lang_code), 'isPublished') &&
!$entity->getTranslation($lang_code)->isPublished()
)
) {
$link['#untranslated'] = TRUE;
}
if (in_array($lang_code, $primary_languages)) {
$link['#primary_language'] = TRUE;
}

if (!$entity instanceof ContentEntityInterface) {
continue;
}

if (!$entity->hasTranslation($lang_code)) {
$link['#untranslated'] = TRUE;
continue;
}

if (
method_exists($entity->getTranslation($lang_code), 'isPublished') &&
!$entity->getTranslation($lang_code)->isPublished()
) {
$link['#untranslated'] = TRUE;
}
}
}

0 comments on commit 9296bf0

Please sign in to comment.