You are here

class LimitLanguageOptionsCallback in Language access 8

Class that contains callbacks to alter language elements.

Hierarchy

Expanded class hierarchy of LimitLanguageOptionsCallback

1 file declares its use of LimitLanguageOptionsCallback
language_access.module in ./language_access.module
Provide access permissions by language.

File

src/LimitLanguageOptionsCallback.php, line 11

Namespace

Drupal\language_access
View source
class LimitLanguageOptionsCallback implements TrustedCallbackInterface {

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

  /**
   * Alters the language widget options. This method is used in #pre_render.
   *
   * Limits the options to those that the user is allowed to access.
   *
   * @param array $element
   *   The language form element.
   *
   * @return array
   *   The language form element.
   */
  public static function preRender(array $element) : array {
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach (array_keys($element['#options']) as $id) {
      if (isset($languages[$id]) && !$element['#for_user']
        ->hasPermission('access language ' . $id)) {
        unset($element['#options'][$id]);
      }
    }
    unset($element['#for_user']);
    return $element;
  }

  /**
   * Alters the language widget options. This method is used in #after_build.
   *
   * @param array $element
   *   The language form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return array
   *   The language form element.
   */
  public static function afterBuild(array $element, FormStateInterface $form_state) : array {
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach (array_keys($element['value']['#options']) as $id) {
      if (isset($languages[$id]) && !$element['#for_user']
        ->hasPermission('access language ' . $id)) {
        unset($element['value']['#options'][$id]);
      }
    }
    unset($element['#for_user']);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LimitLanguageOptionsCallback::afterBuild public static function Alters the language widget options. This method is used in #after_build.
LimitLanguageOptionsCallback::preRender public static function Alters the language widget options. This method is used in #pre_render.
LimitLanguageOptionsCallback::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.