From bf490d19a2a3d5636560aae9c2c10d4733cbd05c Mon Sep 17 00:00:00 2001 From: "Stephen M. Coakley" Date: Mon, 1 Aug 2016 12:33:57 -0500 Subject: [PATCH] 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. --- extensions/tags/bootstrap.php | 1 + .../src/Listener/FilterPostsQueryByTag.php | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 extensions/tags/src/Listener/FilterPostsQueryByTag.php diff --git a/extensions/tags/bootstrap.php b/extensions/tags/bootstrap.php index 51aef68fd..f66ad8bc4 100644 --- a/extensions/tags/bootstrap.php +++ b/extensions/tags/bootstrap.php @@ -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); diff --git a/extensions/tags/src/Listener/FilterPostsQueryByTag.php b/extensions/tags/src/Listener/FilterPostsQueryByTag.php new file mode 100644 index 000000000..711a67312 --- /dev/null +++ b/extensions/tags/src/Listener/FilterPostsQueryByTag.php @@ -0,0 +1,38 @@ + + * + * 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); + } + } +}