Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language helpers #7

Open
asipple1 opened this issue Nov 1, 2023 · 0 comments
Open

Language helpers #7

asipple1 opened this issue Nov 1, 2023 · 0 comments

Comments

@asipple1
Copy link
Collaborator

asipple1 commented Nov 1, 2023

We need better/easier ways to detect what the current lang is.

  • Detect what the active selected lang
  • Array of supported langs on the site
  • check if the node has any translations

Some code from Mises I've been using in hook_preprocess_node().

  $variables['#cache']['contexts'][] = 'languages:language_interface';

  // TODO: Would like to to get all language variables in a helper function so it can be reused in different hooks and Entities
  // Create a list of all supported languages on Mises.
  $langcodes = \Drupal::languageManager()->getLanguages();
  $langcodesList = array_keys($langcodes);
  $variables['language']['supported_langs'] = $langcodesList;


  // Default language.
  $variables['language']['active'] = 'en';
  if (\Drupal::languageManager()->getCurrentLanguage()->getId()) {
    $variables['language']['active'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
  }

  $translations = $node->getTranslationLanguages();
  // Check to see if node has more than one translation.
  $hasTranslations = count($translations) > 1;
  $variables['language']['has_translations'] = FALSE;
  if ($hasTranslations) {
    $variables['language']['has_translations'] = TRUE;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant