Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add several models of a plugin to search? #110

Open
impactfactory opened this issue Mar 28, 2021 · 2 comments
Open

How to add several models of a plugin to search? #110

impactfactory opened this issue Mar 28, 2021 · 2 comments

Comments

@impactfactory
Copy link

I have Blog, with the Models Post, Tag and Category. There is post detail page (succesfully included into the search) and some posts list for articles with a tag and articles in a category.

I would like to let the search list where this tag and category overview pages are.

I did not succeed with repeating the Event Listener and fill in just another model; i guess this needs another approach.

Any tip? :)

Thanx a lot for any help!

@tobias-kuendig
Copy link
Member

I did not succeed with repeating the Event Listener and fill in just another model; i guess this needs another approach.

What went wrong? It is all you need. Maybe share some code so we see where you got stuck.

Example for a Tag model: (change the YourTagModel and your-tag-detal-page values)

\Event::listen('offline.sitesearch.query', function ($query) {
        $controller = \Cms\Classes\Controller::getController() ?? new \Cms\Classes\Controller();

        // Search your plugin's contents
        $items = YourTagModel
            ::where('name', 'like', "%${query}%")
            ->get();

        $results = $items->map(function ($item) use ($query, $controller) {
            return [
                'title'     => $item->name,
                'text'      => 'A tag description'
                'url'       => $controller->pageUrl('your-tag-detail-page', ['slug' => $item->slug]),
            ];
        });

        return [
            'provider' => 'Tags',
            'results'  => $results,
        ];
    });
    

@impactfactory
Copy link
Author

impactfactory commented Mar 31, 2021

I found the error, some minor misspelling. Works like a charm, thank you so much!

So for full clarity for other like me, we just have to repeat the Event Listener for every Model you want a search query for. In case of little blog system it would look like this then.

\Event::listen('offline.sitesearch.query',` function ($query) {.. Plugin Model Post ..}

\Event::listen('offline.sitesearch.query', function ($query) {.. Plugin Model Tag ..}

\Event::listen('offline.sitesearch.query', function ($query) {.. Plugin Model Category ..}

Multilanguage urls and provider buttons can be achieved like this:

'url'       => e(trans('autorname.pluginname::lang.something')).'/'.$item->slug,
 'provider' => e(trans(autorname.pluginname::lang.something')),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants