Hi there,
I'm following some tutorials but I'm not able to serialize the new field created. My goal is to show this new field in each discussion of the home page.
I created the serialization class
<?php
namespace PeruGuitar\PostView\Listener;
use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\DiscussionSerializer;
class AddPostViewsAttribute
{
public function handle(Serializing $event)
{
if ($event->isSerializer(DiscussionSerializer::class)) {
$event->attributes += [
'views' => $event->model->views,
];
}
}
}
I updated the extends.php to include the serialization
<?php
namespace PeruGuitar\PostView;
use Flarum\Extend;
use Flarum\Api\Event\Serializing;
use PeruGuitar\PostView\Listener\AddPostViewsAttribute;
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\Event)
->listen(Serializing::class, AddPostViewsAttribute::class),
];
And in my js component, I'm extending the object.
app.initializers.add('peruguitar/flarum-ext-post-views', () => {
Discussion.prototype.views = Model.attribute('views');
extend(DiscussionListItem.prototype, 'infoItems', function (items) {
items.add('views', addPostsViewItem(0), 0);
});
});
Please I need a litle help because the documentation is frustrating and the example that I took it is for a User but is not working in my case. https://www.sitepoint.com/writing-a-flarum-extension-building-a-custom-field/