Add posts API filter for tags

Add the means of filtering post queries by the tags of the posts' parent discussions. See https://github.com/flarum/core/issues/990.
This commit is contained in:
Stephen M. Coakley 2016-08-01 12:33:57 -05:00
parent e4a4bad601
commit bf490d19a2
2 changed files with 39 additions and 0 deletions

View File

@ -20,6 +20,7 @@ return function (Dispatcher $events) {
$events->subscribe(Listener\AddTagsApi::class);
$events->subscribe(Listener\CreatePostWhenTagsAreChanged::class);
$events->subscribe(Listener\FilterDiscussionListByTags::class);
$events->subscribe(Listener\FilterPostsQueryByTags::class);
$events->subscribe(Listener\SaveTagsToDatabase::class);
$events->subscribe(Listener\UpdateTagMetadata::class);

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tags\Listener;
use Flarum\Event\ConfigurePostsQuery;
use Illuminate\Contracts\Events\Dispatcher;
class FilterPostsQueryByTag
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigurePostsQuery::class, [$this, 'filterQuery']);
}
/**
* @param ConfigurePostsQuery $event
*/
public function filterQuery(ConfigurePostsQuery $event)
{
if ($tagId = array_get($event->filter, 'tag')) {
$event->query
->join('discussions_tags', 'discussions_tags.discussion_id', '=', 'posts.discussion_id')
->where('discussions_tags.tag_id', $tagId);
}
}
}