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 and Taxonomy Included in Results

Started by Claud Rusnac 7 years ago · 5 replies · 902 views
7 years ago

I am attempting to add add a taxonomy value inside of TNTSearch result. Specifically, my search results show the Title and a Content snippet. I want to include a taxonomy in the search results. My frontmatter:

YAML
---
title: TEST Document
taxonomy:
    id: ['dp-04']
---

I want to include in my TNT Search Result: {{ val.taxonomy.id }}

<a href="{{ base_url ~ val.link }}">{{ val.taxonomy.id }} {{ val.title }}</a>

👍 1
7 years ago

Hi @crusnac, you might find this thread helpful: /forum/plugins/indexing-taxonomies-with-tntsearch-t4555

7 years ago

I was able to include my taxonomy in the index, but I want to display it in the search output as well. Any ideas?

👍 1
7 years ago

Sorry, I do not know how you would do that myself.

7 years ago

I got this to work. See code for reference below. Note - I am only including the Taxonomy->ID field in the results. Code adjustment may be necessary to include other taxonomy.

PHP
  public function onTNTSearchQuery(Event $e){
        $query = $e['query'];
        $page = $e['page'];
        $options = $e['options'];
        $fields = $e['fields'];
        $gtnt = $e['gtnt'];

        $tax = $page->taxonomy();

        if (!isset($tax['id'][0])){
            $id = $tax['id'][0] = ''; // set default value
        }else{
            $id = $tax['id'][0];
        }

        $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]),
            'id' =>  $gtnt->tnt->highlight($id, $query, 'em', ['wholeWord' => false]),
        ];

    $e->stopPropagation();
}

Also, don't for get to call the onTNTSearchQuery function in the beginning.

JS
 public static function getSubscribedEvents()
    {
        return [
            'onTNTSearchIndex' => ['onTNTSearchIndex', 0],
            'onTNTSearchQuery' => ['onTNTSearchQuery', 1000]
        ];
    }
👍 1
last edited 04/12/19 by Claud Rusnac

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago