class LimitLanguageOptionsCallback in Language access 8
Class that contains callbacks to alter language elements.
Hierarchy
- class \Drupal\language_access\LimitLanguageOptionsCallback implements TrustedCallbackInterface
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_accessView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LimitLanguageOptionsCallback:: |
public static | function | Alters the language widget options. This method is used in #after_build. | |
LimitLanguageOptionsCallback:: |
public static | function | Alters the language widget options. This method is used in #pre_render. | |
LimitLanguageOptionsCallback:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |