Merge pull request #29 from coderstephen/master

Add posts API filter for tags
This commit is contained in:
Toby Zerner 2016-08-02 22:06:34 +10:00 committed by GitHub
commit e84788d646
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);
}
}
}