function admin_user_language_entity_presave in Admin User Language 8
Implements hook_entity_presave().
On user creation/edit makes sure that a preferred admin language is selected.
Parameters
\Drupal\Core\Entity\EntityInterface $entity:
File
- ./
admin_user_language.module, line 39 - Contains admin_user_language.module.
Code
function admin_user_language_entity_presave(EntityInterface $entity) {
if ($entity instanceof User) {
/** @var Drupal\Core\Config\ConfigFactory $config */
$config = \Drupal::service('config.factory')
->get('admin_user_language.settings');
$default_language = $config
->get('default_language_to_assign');
$languages = (array) \Drupal::service('language_manager')
->getLanguages();
// Building an array of language code => language name.
$active_languages = [];
/** @var \Drupal\Core\Language\Language $lang */
foreach ($languages as $lang) {
$active_languages[$lang
->getId()] = $lang
->getName();
}
$prevent_user_override = $config
->get('prevent_user_override');
if ($default_language && array_key_exists($default_language, $active_languages)) {
if (TRUE === $prevent_user_override || $entity
->isNew()) {
$entity
->set('preferred_admin_langcode', $default_language);
}
}
}
}