First of all, thanks for the tips. After learning the basics of PHP and JS, I could implement the custom field and store the input info in a database column.
However, I'm still struggling to display this data in the UserCard. My code is as follows:
src/UserSerializer.php
<?php
namespace Indigo\CustomField;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;
class UserAttributes
{
public function __invoke(UserSerializer $serializer, User $user): array
{
return [ 'empresa' => $user->empresa ];
}
}
js/src/forum/index.ts:
import UserCard from 'flarum/forum/components/UserCard';
extend(UserCard.prototype, 'infoItems', function(items) {
// const empresa = this.props.user.attribute('empresa');
items.add('empresa', m('span', [
icon('fas fa-address-card'), ' ', this.props.user.attribute('empresa')
]))
})
extend.php
<?php
namespace Indigo\CustomField;
use Indigo\CustomField\SaveUserAttr;
use Flarum\Extend;
use Flarum\User\Event\Saving;
return [
(new Extend\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/less/forum.less'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/less/admin.less'),
new Extend\Locales(__DIR__.'/locale'),
(new Extend\ApiSerializer(UserSerializer::class))->attributes(UserAttributes::class),
(new Extend\Event())->listen(Saving::class, SaveEmpresaToDatabase::class),
];
I tried to reproduce what Clark does in his Poke extension, but with no success – Console yields "TypeError: Cannot read properties of undefined (reading 'user')" and "TypeError: Cannot read properties of undefined (reading 'onbeforeupdate')"
I also tried to use this.attrs.user.empresa, and this yields no error, but it also doesn't return me anything. I wasn't able to learn what to do by looking the code of other extensions as well.
It seems like a trivial matter, but I'm really grinding with this one. If anyone could lend me a hand, I'd be very glad.