Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Customize TntSearchQuery error : Argument 1 passed ... must be an instance of Grav\Theme\Event

Solved by nix View solution

Started by nix 6 years ago · 1 replies · 505 views
6 years ago

Hello everyone,
I am trying to customize the output of The TNT search plugin. I am following the explanations available on the plugin page:
https://github.com/trilbymedia/grav-plugin-tntsearch#customizing-query-data

I have added the following functions in the php file of my theme:

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Theme;

class NixholTemplate extends Theme
{
    public static function getSubscribedEvents() {
        return [
            'onTNTSearchQuery' => ['onTNTSearchQuery', 1000],
        ];
    }

    public function onTNTSearchQuery(Event $e) {
        $query = $this->grav['uri']->param('q');

        if ($query) {

            $page = $e['page'];
            $query = $e['query'];
            $options = $e['options'];
            $fields = $e['fields'];
            $gtnt = $e['gtnt'];

            $content = $gtnt->getCleanContent($page);
            $title = $page->title();

            $relevant = $gtnt->tnt->snippet($query, $content, $options['snippet']);

            if (strlen($relevant) <= 6) {
                $relevant = substr($content, 0, $options['snippet']);
            }

            $fields->hits[] = [
                'link' => $page->route(),
                'title' =>  $gtnt->tnt->highlight($title, $query, 'em', ['wholeWord' => false]),
                'content' =>  $gtnt->tnt->highlight($relevant, $query, 'em', ['wholeWord' => false]),
            ];
            $e->stopPropagation();
        }
    }
}
?>

But when I test the plugin I am running into this error:
Argument 1 passed to Grav\Theme\NixholTemplate::onTNTSearchQuery() must be an instance of Grav\Theme\Event

Do you know what I am doing wrong? Thanks in advance for your help!
Nicolas

6 years ago Solution

Solved :
Adding these definitions at the beginning of the php file work:

PHP
use Grav\Common\Theme;
use RocketTheme\Toolbox\Event\Event;

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago