Language selection upon registration

In order to make the system really multilingual, according to everyone's preference, I've decided to add the language selection element directly to the initial registration form. Otherwise available in the user settings. This was done through an experimental plugin, not overriding the core code.
However, it does not function.

Views modified —
views/default/forms/register.php
– adding the select–option element:

[
 '#type' => 'select',
'name' => 'language',
'value' => $language,
'required' => true,
'options_values' => $options,
'#label' => elgg_echo('user:language:label'),
],

Actions modified —
actions/register.php

getting the language as a selected option:

$language = $request->getParam('language');

As the $new_user object is created here,
I tried to add these lines to register the selection:

$new_user->language = $language;
$new_user->save();

Outcome:
The new element of the registration form – selection of options of available languages – displays correctly. Picking data from this element seems not to be an obstacle because the same effect can be emulated by inserting a language code directly to the code, assigning $new_user->language = ...
But there is no change in the data of the newly registered user – the selected language is not saved, the default one remains. → no result
 

  • By default, register() use the system current language:

    $user->language = $this->translator->getCurrentLanguage();

    You may want to use 'create', user' event hook or 'register', 'user' plugin hook to override the language parameter.

    Look at this fork also.

  • the problem is probably related to not having access to save the data to the user.

    if you wrap you code in an elgg_call() like this:

    elgg_call(ELGG_IGNORE_ACCESS, function() use ($new_user, $language) {
        $new_user->language = $language;
        $new_user->save();
    }
    

    In order for you not having to overrule the registration action you could listen to the 'register', 'user' hook like Nikolai suggested.

    PS: this also pointed out a probable bug in the registration action ;)

  • Well. In the end, it appeared the fault was a missing registration of the action in elgg-plugin.php, therefore not inaccessible in the system. It was found out that other fields like the name were not modifiable from inside the code either. The solution was to add the action with ['access' => 'public'] to elgg-plugin.php. The elgg_call function is even not necessary. Now it's functioning.
     

  • The original Language Selector plugin
    https://elgg.org/plugins/384977
    is incompatible with new versions. It still contains the outdated start.php instead of elgg-plugin.php.
    And the GitHub/RiverVanRain variant
    https://github.com/RiverVanRain/language_selector/tree/3.3
    couldn't be activated. It throws a fatal error:

     

    Call to undefined function ColdTrick\LanguageSelector\elgg_unset_plugin_setting()
    
    Log at time 2022-02-22T23:26:56+00:00 may have more data.
    
    #0 [internal function]: ColdTrick\LanguageSelector\Settings::invalidateSetting(Object(Elgg\HooksRegistrationService\Hook))
    #1 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/HandlersService.php(62): call_user_func(Array, Object(Elgg\HooksRegistrationService\Hook))
    #2 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/PluginHooksService.php(69): Elgg\HandlersService->call(Array, Object(Elgg\HooksRegistrationService\Hook), Array)
    #3 /var/www/elgg/vendor/elgg/elgg/engine/lib/elgglib.php(513): Elgg\PluginHooksService->trigger('action_button', 'plugin', Array, '<a title="Deakt...')
    #4 /var/www/elgg/vendor/elgg/elgg/views/default/object/plugin/full.php(80): elgg_trigger_plugin_hook('action_button', 'plugin', Array, '<a title="Deakt...')
    #5 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(515): include('/var/www/elgg/v...')
    #6 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(448): Elgg\ViewsService->renderViewFile('object/plugin/f...', Array, 'default', true)
    #7 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(222): Elgg\ViewsService->renderView('object/plugin/f...', Array, 'default')
    #8 /var/www/elgg/vendor/elgg/elgg/views/default/object/plugin.php(24): elgg_view('object/plugin/f...', Array)
    #9 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(515): include('/var/www/elgg/v...')
    #10 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(448): Elgg\ViewsService->renderViewFile('object/plugin', Array, 'default', true)
    #11 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(222): Elgg\ViewsService->renderView('object/plugin', Array, 'default')
    #12 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(613): elgg_view('object/plugin', Array)
    #13 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(1331): elgg_view_entity(Object(ElggPlugin), Array)
    #14 /var/www/elgg/vendor/elgg/elgg/views/default/page/components/list.php(77): elgg_view_list_item(Object(ElggPlugin), Array)
    #15 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(515): include('/var/www/elgg/v...')
    #16 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(448): Elgg\ViewsService->renderViewFile('page/components...', Array, 'default', true)
    #17 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(222): Elgg\ViewsService->renderView('page/components...', Array, 'default')
    #18 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(758): elgg_view('page/components...', Array)
    #19 /var/www/elgg/vendor/elgg/elgg/views/default/admin/plugins.php(28): elgg_view_entity_list(Array, Array)
    #20 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(515): include('/var/www/elgg/v...')
    #21 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(448): Elgg\ViewsService->renderViewFile('admin/plugins', Array, 'default', true)
    #22 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(222): Elgg\ViewsService->renderView('admin/plugins', Array, 'default')
    #23 /var/www/elgg/vendor/elgg/elgg/views/default/resources/admin.php(29): elgg_view('admin/plugins', Array)
    #24 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(515): include('/var/www/elgg/v...')
    #25 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/ViewsService.php(448): Elgg\ViewsService->renderViewFile('admin/plugins', Array, 'default', true)
    #26 /var/www/elgg/vendor/elgg/elgg/engine/lib/views.php(377): Elgg\ViewsService->renderView('resources/admin', Array)
    #27 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Router.php(211): elgg_view_resource('admin', Array)
    #28 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Router.php(122): Elgg\Router->prepareResponse(Object(Elgg\Http\Request))
    #29 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Router.php(102): Elgg\Router->getResponse(Object(Elgg\Http\Request))
    #30 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Application.php(419): Elgg\Router->route(Object(Elgg\Http\Request))
    #31 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Application.php(310): Elgg\Application->run()
    #32 /var/www/elgg/vendor/elgg/elgg/engine/classes/Elgg/Application.php(360): Elgg\Application::route(Object(Elgg\Http\Request))
    #33 /var/www/elgg/index.php(8): Elgg\Application::index()
    #34 {main}
  • This plugin isn't compitable with Elgg 4.

    You can use it as an example for development.

    Any contributions are welcome ;)

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking