LimitLanguageOptionsCallback.php in Language access 8
File
src/LimitLanguageOptionsCallback.php
View source
<?php
namespace Drupal\language_access;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class LimitLanguageOptionsCallback implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRender',
];
}
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;
}
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;
}
}