You are here

class AllowedLanguagesTrustedCallbacks in Allowed Languages 2.x

Trusted callbacks for allowed languages form alters.

Hierarchy

Expanded class hierarchy of AllowedLanguagesTrustedCallbacks

1 file declares its use of AllowedLanguagesTrustedCallbacks
allowed_languages.module in ./allowed_languages.module
Contains allowed_languages.module.

File

src/Form/AllowedLanguagesTrustedCallbacks.php, line 11

Namespace

Drupal\allowed_languages\Form
View source
class AllowedLanguagesTrustedCallbacks implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'languageSelectWidgetPreRender',
    ];
  }

  /**
   * Removes any languages that the user is not allowed to create content for.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AllowedLanguagesTrustedCallbacks::languageSelectWidgetPreRender public static function Removes any languages that the user is not allowed to create content for.
AllowedLanguagesTrustedCallbacks::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.