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.

Plugins

TNT search plugin - image in search result

Solved by domena.online View solution

Started by Marcin 3 years ago · 3 replies · 459 views
3 years ago

Hi!

Does anyone have idea how to display images in search results of tnt search plugin...??

Title we can get by using

{{ val.title|raw }}

What Im looking for is something like

{{ val.media.images|first.url|e }}

to use as img src on each search result... any ideas what I should overwrite in plugin .php file..?

3 years ago

Try with:

TWIG
{% set banner = page.media.images|first %}

    {% if banner %}
        <div class="search-image">
            <a href="{{ page.url }}">{{ banner.cropZoom(100,100).html|raw }}</a>
        </div>
    {% endif %}

Look at Simple search plugin

3 years ago

Thanks, but this way will not work with TNT Search plugin (tried before multiple combinations like yours) - I think tntsearch.php need to be overwritten...

PHP
    public function onTNTSearchIndex(Event $e): void
    {
        $page = $e['page'];
        $fields = $e['fields'];

        if (isset($page->header()->parameters_id) || isset($page->header()->parameters_ean)) {
        $fields->parameters_id = $page->header()->parameters_id;
        $fields->parameters_ean = $page->header()->parameters_ean;
        };

    }

    public function onTNTSearchQuery(Event $e): void
    {
        $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]),
        ];
    }
    protected function getFormValue($val)
    {
        $uri = $this->grav['uri'];

        return $uri->param($val) ?: $uri->query($val) ?: filter_input(INPUT_POST, $val, FILTER_SANITIZE_ENCODED);
    }

I modify onTNTSearchIndex function to search header.parameters_id and header.parameters_ean on my product page and it works fine, but I am struggling with showing picture on search results...

I guesses that onTNTSearchQuery $fields->hits[] need to contain info about media but cant this get working as it should be... any ideas?

3 years ago Solution

Add

'media' => $page->media(),

to function onTNTSearchQuery $fields->hits[]:

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

after that in tntquery-ajax.html.twig add something like:

TWIG
<img src="{{val.media.images|first.resize(800, 533).url|e}} width="800" height="533"
"title="{{ val.title|raw }}" alt="{{ val.title|raw }} search result">
👍 2

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 41 1 week ago
Plugins · by Xavier, 4 weeks ago
2 50 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1178 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 45 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 68 2 months ago