You are here

public static function DisableLanguageCallback::preRender in Disable language 8

File

src/DisableLanguageCallback.php, line 13

Class

DisableLanguageCallback

Namespace

Drupal\disable_language

Code

public static function preRender($element) {
  if (\Drupal::currentUser()
    ->hasPermission('view disabled languages')) {
    return $element;
  }
  $enabled_languages = \Drupal::service('disable_language.disable_language_manager')
    ->getEnabledLanguages();
  $enabled_langcodes = array_keys($enabled_languages);
  $langcode_options = array_keys($element['#options']);

  // Only keep the enabled languages.
  foreach (array_diff($langcode_options, $enabled_langcodes) as $langcode) {
    unset($element['#options'][$langcode]);
  }
  return $element;
}