You are here

public static function AllowedLanguagesTrustedCallbacks::languageSelectWidgetPreRender in Allowed Languages 2.x

Removes any languages that the user is not allowed to create content for.

File

src/Form/AllowedLanguagesTrustedCallbacks.php, line 23

Class

AllowedLanguagesTrustedCallbacks
Trusted callbacks for allowed languages form alters.

Namespace

Drupal\allowed_languages\Form

Code

public static function languageSelectWidgetPreRender($build) {
  $allowed_languages = \Drupal::service('allowed_languages.allowed_languages_manager')
    ->assignedLanguages();

  // Remove any languages that the user is not allowed to add content for.
  foreach ($build['value']['#options'] as $language_code => $language_option) {

    // If the language is allowed then continue.
    if (in_array($language_code, $allowed_languages)) {
      continue;
    }

    // Always allow the not specified and not applicable language options.
    if ($language_code === LanguageInterface::LANGCODE_NOT_SPECIFIED || $language_code === LanguageInterface::LANGCODE_NOT_APPLICABLE) {
      continue;
    }
    unset($build['value']['#options'][$language_code]);
  }
  return $build;
}