The core language switcher lists all enabled languages even when viewing a node that is not translated to those languages.

The behaviour I would expect is to hide all the languages that specific node isn't translated to, or make it an option from the language switcher block settings.

For the moment, in case anybody stomps on the same behavior and isn't happy with it, a workaround could be implementing something like this in a theme:

  function THEMENAME_preprocess_links__language_block(&$variables) {
    $node = \Drupal::routeMatch()->getParameter('node');
    if(!$node) {
        return;
    }
    foreach(array_keys($variables['links']) as $lang) {
      if(!$node->hasTranslation($lang)) {
        unset($variables['links'][$lang]);
      }
    }
  }

Comments

entuland created an issue. See original summary.

entuland’s picture

Issue summary: View changes
penyaskito’s picture

Version: 8.2.6 » 8.3.x-dev
Priority: Minor » Normal
Issue tags: +D8MI, +language-interface

Hi @entuland, thanks for reporting!

Adding some issue tags, adjusting priority (most issues are normal, see https://www.drupal.org/core/issue-priority), adjusting version (features go against last version; probably this should even be 8.4.x-dev, but I'm not sure)

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

esolitos’s picture

Version: 8.6.x-dev » 8.8.x-dev
leymannx’s picture

Issue summary: View changes

Minor description change, switched double quotes with single quotes for consistent quotes.

esolitos’s picture

For those in need of a solution, look at this module: https://www.drupal.org/project/language_switcher_extended

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

farse’s picture

Thanks, this snippet is just what I was looking for.

Ruuds’s picture

I solved this using the hook_language_switch_links_alter hook, which checks if the entity has a translation, if the user has access and if not, changes the link to the language's page.


/**
 * Implements hook_language_switch_links_alter().
 *
 * Link non-translated links to the language's homepage instead of the
 * non-translated node url (/fr/node/123).
 */
function mymodule_language_switch_links_alter(array &$links, $type, Url $url) {
  $route_match = Drupal::routeMatch();

  $route_match_entity = NULL;
  foreach ($route_match->getParameters() as $route_match_parameter) {
    if ($route_match_parameter instanceof EntityInterface) {
      $route_match_entity = $route_match_parameter;
    }
  }

  foreach ($links as $langcode => $link) {
    if ($link['url']->getRouteName() !== '<current>') {
      continue;
    }

    if ($route_match_entity instanceof TranslationStatusInterface && $route_match_entity instanceof TranslatableInterface) {
      $has_translation = $route_match_entity->getTranslationStatus($langcode);
      $translation = $has_translation ? $route_match_entity->getTranslation($langcode) : FALSE;
      $has_translation_access = $translation ? $route_match_entity->access('view') : FALSE;

      if (!$translation || !$has_translation_access) {
        // Translation does not exist. Link to home instead.
        /**
         * @var \Drupal\Core\Url $url
         */
        $links[$langcode]['url'] = Url::fromRoute('<front>');
      }
    }
  }
}

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kevinvanhove’s picture

@Ruuds, this fixes it for nodes, not for views...

Anybody’s picture

As of my comment here: https://www.drupal.org/forum/support/translations/2016-08-28/language-sw...

I think core needs settings to let the admin decide how to handle untranslated nodes. It should combine the fixed from the listed modules, which are acting as workaround currently, but that should be done in core as combined setting in one single place:

All solve different things for the same need: Define behavior for untranslated nodes.

l-laziz’s picture

Category: Feature request » Bug report

accidental comment.

l-laziz’s picture

Priority: Normal » Major
l-laziz’s picture

Category: Bug report » Feature request
l-laziz’s picture

Priority: Major » Normal

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.