Page 1 of 1

search results links should not use default template

Posted: Fri Sep 20, 2019 5:08 pm
by Sendlingur
I'm using the search module on my page.

I have a results page that displays the search results.
The search searches through the news module and displays the title as a link to the page.

My problem is that I have different details page for each news category and when the user hits a link on the search results page the user should be able to go to the news item and see it displayed with the corresponding news_detail_template. As it is now the news details opens up in a default template and that is not good enough.

For clarification : News category A --- News detail template A
News category B --- News detail template B
News category C--- News detail template C

Isn't there a way to just skip this default template which opens all the links in the search results in. And make the links to open up like:

News category A --- News detail template A
News category B --- News detail template B
News category C--- News detail template C


Here is a sample of my code in the results page:

Code: Select all

{if $itemcount > 0}
<ul class="search-ul">
  {foreach from=$results item=entry}
    {if (strpos($entry->urltxt, "http://") !== false)}   
{else}
  <div class="search-results-box-item search-results">
    <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
    </li>
  </diV>
    {/if}

  {/foreach}
</ul>

{*<p>{$timetaken}: {$timetook}</p>*}
{else}
 <div class="search-results-box-item search-results">
  <p><strong>No result</strong></p>
</div>
{/if}

Here is the {search tag}

Code: Select all

{search formtemplate='mms_search' resultpage='results'}

here is the search input code:

Code: Select all

{$startform}

<input type="text" class="search-input form-control" id="{$search_actionid}searchinput" name="{$search_actionid}searchinput" size="20" maxlength="50" placeholder="search"/>

<button class="btn btn-search" name="submit" value="{$submittext}" type="submit" />
{if isset($hidden)}{$hidden}{/if}
{$endform}

It would be really nice if someone could clarify this for me?

Re: search results links should not use default template

Posted: Fri Sep 20, 2019 6:11 pm
by velden
I think it can't be done easily.

Functionality of the Search module is limited: you will get an url and title only. Not an item id, object or something like that.

Further, there's no real relation between a category and a template. You probably create the 'relation' in your pages by using something like {news category='category-A' detailtemplate='template-A'}. But in that case you explicitly define the category, something you can't do in a search template.

Only thing I can think of - and I realize it isn't a beautiful solution - would be to first load all the news items and store their title and category in some array.

Then in the search result, get the category from the news array based on title, and then create the url yourself based on that category (https://docs.cmsmadesimple.org/tags/cms ... action_url)

If you will be having tons of news items and/or search requests, this may be a bad idea.

Re: search results links should not use default template

Posted: Sat Sep 21, 2019 9:51 am
by rotezecke
the Search module is limited: you will get an url and title only. Not an item id, object or something like that.
The search module returns a little info about modules (at least it does for me). That alone is not enough, you'd need to write a plugin that checks the category of the given News ID, and return your detail template

Code: Select all

{* in search result template *}
{if $entry->module == "News"} 
  {yourPlugin newsID=$entry->modulerecord assign=myTmplate}
  {News ... }
{/if}

Re: search results links should not use default template

Posted: Sat Sep 21, 2019 3:48 pm
by velden
The search module returns a little info about modules (at least it does for me). That alone is not enough, you'd need to write a plugin that checks the category of the given News ID, and return your detail template
Mmm, missed that one.

In that case it might be possible to do it without plugin. Create a new News detail template and call

Code: Select all

{* in search result template *}
{if $entry->module == "News"}
  {News action=detail articleid=$entry->modulerecord detailtemplate=YOURNEWSDETAILTEMPLATE  }
{/if}
Then in YOURNEWSDETAILTEMPLATE something like

Code: Select all

{$detail_template = ''}
{if $entry->category == 'category-A'}{$detail_template = 'template-A'}
{elseif $entry->category == 'category-B'}{$detail_template = 'template-B'}
}
...
{/if}

<a href="{cms_action_url action=detail articleid=$entry->id detailtemplate=$detail_template}">{$entry->title}</a>
Note: it may be possible that this call to the News module overwrites your search template $entry variable. To prevent that from happening you can change the foreach loop of your search results template:

{foreach $results as $result}

Of course you need to change the code accordingly.

Note 2: if you're using different pages to show the different News detail categories on you probably want to use the returnid parameter of cms_action_url. Then it would make sense to make use of the cms_module_hint tag (https://docs.cmsmadesimple.org/tags/cms ... odule_hint) for those pages. Then you don't need to explicitly provide the detailtemplate parameter to the cms_action_url tag.