mirror of
https://github.com/flarum/core.git
synced 2025-07-30 13:10:24 +02:00
Make the Request available to the Formatter\Rendering event (#1721)
This is important because extensions may wish to render post content differently depending on Request factors such as the actor. For example, an attachments extension might wish to hide attachments from guests. This solution is a bit of a hack-job for now, but soon when we refactor the API layer to use tobscure/json-api-server, and also refactor the Formatter layer, it can be revised.
This commit is contained in:
committed by
Franz Liedke
parent
9b68bbe44e
commit
0ab9facc4b
@@ -102,7 +102,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
||||
);
|
||||
|
||||
$serializer = static::$container->make($this->serializer);
|
||||
$serializer->setActor($request->getAttribute('actor'));
|
||||
$serializer->setRequest($request);
|
||||
|
||||
$element = $this->createElement($data, $serializer)
|
||||
->with($this->extractInclude($request))
|
||||
|
@@ -20,6 +20,7 @@ use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Tobscure\JsonApi\AbstractSerializer as BaseAbstractSerializer;
|
||||
use Tobscure\JsonApi\Collection;
|
||||
use Tobscure\JsonApi\Relationship;
|
||||
@@ -28,6 +29,11 @@ use Tobscure\JsonApi\SerializerInterface;
|
||||
|
||||
abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
@@ -43,6 +49,23 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
*/
|
||||
protected static $container;
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*/
|
||||
public function setRequest(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->actor = $request->getAttribute('actor');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User
|
||||
*/
|
||||
@@ -51,14 +74,6 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
return $this->actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
*/
|
||||
public function setActor(User $actor)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -231,7 +246,7 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
{
|
||||
$serializer = static::$container->make($class);
|
||||
|
||||
$serializer->setActor($this->actor);
|
||||
$serializer->setRequest($this->request);
|
||||
|
||||
return $serializer;
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ class BasicPostSerializer extends AbstractSerializer
|
||||
];
|
||||
|
||||
if ($post instanceof CommentPost) {
|
||||
$attributes['contentHtml'] = $post->content_html;
|
||||
$attributes['contentHtml'] = $post->formatContent($this->request);
|
||||
} else {
|
||||
$attributes['content'] = $post->content;
|
||||
}
|
||||
|
@@ -43,8 +43,6 @@ class PostSerializer extends BasicPostSerializer
|
||||
$canEdit = $gate->allows('edit', $post);
|
||||
|
||||
if ($post instanceof CommentPost) {
|
||||
$attributes['contentHtml'] = $post->content_html;
|
||||
|
||||
if ($canEdit) {
|
||||
$attributes['content'] = $post->content;
|
||||
}
|
||||
|
Reference in New Issue
Block a user