I'm trying to write an extension which allows users to send some additional info while registering to Flarum. It should attach a new role to the user based on this info.
Here I'm adding some additional fields with extending SignUpModal:
app.initializers.add('ky-adds', () => {
extend(SignUpModal.prototype, 'init', function () {
this.additionalField = m.prop('');
});
extend(SignUpModal.prototype, 'fields', function(items) {
// add fields here
items.add();
}
extend(SignUpModal.prototype, 'submitData', function (data) {
data['additional'] = this.additionalField();
});
});
Then I need to use this data['additional'] on Flarum\User\Event\Registered event, so I can manipulate user as soon as they registered. But that event doesn't hold any form data as you can see here. So I'm thinking that somehow I need to get this data by listening Flarum\User\Event\Saving event then pass it into Flarum\User\Event\Registered event. It's working flawless if I use static variable to hold that data but I'm not happy with this dirty solution:
<?php
use Flarum\User\Event\Saving as UserSaving;
use Flarum\User\Event\Registered;
use Illuminate\Support\Arr;
class InterruptRegisterProcess
{
/**
* @var string $additionalData
*/
private static $additionalData = '';
/**
* Subscribes to the Flarum events
*
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(UserSaving::class, [$this, 'whenSavingUser']);
$events->listen(Registered::class, [$this, 'registeredUser']);
}
/**
* @param UserSaving $event
*/
public function whenSavingUser(UserSaving $event)
{
$data = $event->data;
self::$additionalData = Arr::get($data, 'attributes.additional');
}
/**
* @param Registered $event
*/
public function registeredUser(Registered $event)
{
$user = $event->user;
$additional = self::$additionalData;
// manipulate $user here using $additional
// ...
}
}
What's the proper way of doing this?