Bishwas-py unfortunately there's no way to access the Flarum\Discussion\Discussion
model instance from the content extender. Additionally, because of the order the content callbacks are called, you don't have access to the values added by the Flarum\Forum\Content\Discussion
class either (which would have included the JSON serialization of the discussion including most public attributes and ID) This priority issue was discussed here flarum/issue-archive199
I would have suggested looking at how the SEO extension did it, but the code is awful with tons of hard-coded values that are not compatible with Flarum's new slugger API, so I'm not sure of any up to date code that could be used as an example.
Basically, you'll have to retrieve your own copy of the discussion instance using router parameters, something like this:
It does cause a slight performance hit due to the additional database request to retrieve the discussion.
<?php
use Flarum\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Frontend\Document;
use Flarum\Http\RequestUtil;
use Flarum\Http\SlugManager;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
return [
(new Extend\Frontend('forum'))
->content(function (Document $document, ServerRequestInterface $request) {
// Check if current request is for the discussion page
if ($request->getAttribute('routeName') === 'discussion') {
try {
// Retrieve discussion by its slug value, this will work even if the user changes the slug strategy in the settings
$discussion = resolve(SlugManager::class)->forResource(Discussion::class)->fromSlug(
Arr::get($request->getQueryParams(), 'id'),
RequestUtil::getActor($request)
);
var_dump($discussion->title);
} catch (ModelNotFoundException $exception) {
// You should catch this exception and silently do nothing
// The code in Flarum\Forum\Content\Discussion that runs later will throw its own RouteNotFoundException if the discussion does not exist or is not visible to the current user
}
}
}),
];