1
0
mirror of https://github.com/flarum/core.git synced 2025-08-14 12:24:33 +02:00

Use Formatter extender for rendering callbacks

This commit is contained in:
Alexander Skvortsov
2020-12-08 11:41:05 -05:00
parent 256e821289
commit 3198de4b9b
5 changed files with 84 additions and 67 deletions

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Formatter;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
/**
* Configure rendering for post mentions.
*
* @param s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @param string|null $xml
* @param Psr\Http\Message\ServerRequestInterface $request
* @return void
*/
public function __invoke(Renderer $renderer, $context, $xml, Request $request)
{
$post = $context;
return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
return $attributes;
});
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Formatter;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
/**
* Configure rendering for user mentions.
*
* @param s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @param string|null $xml
* @param Psr\Http\Message\ServerRequestInterface $request
*/
public function __invoke(Renderer $renderer, $context, $xml, Request $request)
{
$post = $context;
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($post) {
$user = $post->mentionsUsers->find($attributes['id']);
if ($user) {
$attributes['username'] = $user->username;
$attributes['displayname'] = $user->display_name;
}
return $attributes;
});
}
}

View File

@@ -1,30 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Rendering;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
public function handle(Rendering $event)
{
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
return $attributes;
});
}
}

View File

@@ -1,31 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Rendering;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
public function handle(Rendering $event)
{
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'USERMENTION', function ($attributes) use ($post) {
$user = $post->mentionsUsers->find($attributes['id']);
if ($user) {
$attributes['username'] = $user->username;
$attributes['displayname'] = $user->display_name;
}
return $attributes;
});
}
}