CMS Made Simple Forums
https://forum.cmsmadesimple.org/

Adding {$entry->content} to search results.
https://forum.cmsmadesimple.org/viewtopic.php?f=8&t=79013
Page 1 of 2

Author:  Sendlingur [ Wed Sep 05, 2018 2:36 pm ]
Post subject:  Adding {$entry->content} to search results.

Im trying to add {$entry->content} to the search results of my page but with out any luck.


\$1:
 <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)
     
</li>
{$entry->content}


I know there is a 7 years old post here about the the same issue.
The solution presented in that post doesn't seems to be working with cms 2.x....

Can someone experienced explain to me if it it is possible or not?

thanks in advance.

Author:  DIGI3 [ Wed Sep 05, 2018 2:45 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

You could add <pre>{$entry|print_r}</pre> to see what options are available.

If content isn't one of them, you might be able to do something with {page_attr} to obtain the content.

Author:  Sendlingur [ Wed Sep 05, 2018 2:54 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

I tried <pre>{$entry|print_r}</pre> content is not one of the options.

I'm to much of a newbie to know how to play with the {page_attr} tag :)

Author:  DIGI3 [ Wed Sep 05, 2018 3:16 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

I just did a bit of testing, and it doesn't look like it's going to be possible to reliably get the page alias from the $entry array.

You could use javascript/ajax to pull in the content from the url, which might actually be nicer anyway as if there's lots of results you could lazy load them as the user scrolls. The url you use in js would be something like {$entry->url}&showtemplate=false

Author:  Sendlingur [ Wed Sep 05, 2018 5:11 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

I will try the javascript approach, but honestly I think it's a bit strange that the Search Module is so unflexible. Somehow I thought it would be rather easy to just implement $entry -> content to the foreach loop and then the magic would happen :)

Author:  DIGI3 [ Wed Sep 05, 2018 5:14 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

Agreed, ideally the alias should be returned to make it easier to obtain the content. You can always put in a feature request at http://dev.cmsmadesimple.org/projects/cmsmadesimple

Author:  Rolf [ Wed Sep 05, 2018 5:38 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

From default Search result template:
\$1:
{*
You can also instantiate custom behaviour on a module by module basis by looking at
the $entry->module and $entry->modulerecord fields in $entry
ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'}
*}
This way you can show any News content you want...

Author:  Rolf [ Wed Sep 05, 2018 5:40 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

Sendlingur \Sendlingur:
\$1:
 <li><a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)
     
</li>
{$entry->content}

Note:
{$entry->url}, {$entry->urltxt} and {$entry->weight} are Search module Smarty tags
{$entry->content} is from the News module...
You can't mix them here.

Author:  Sendlingur [ Thu Sep 06, 2018 8:43 am ]
Post subject:  Re: Adding {$entry->content} to search results.

Rolf \Rolf:
From default Search result template:
Quote:
{*
You can also instantiate custom behaviour on a module by module basis by looking at
the $entry->module and $entry->modulerecord fields in $entry
ie: {if $entry->module == 'News'}{News action='detail' article_id=$entry->modulerecord detailpage='News'}
*}
This way you can show any News content you want...


But what if I want to show the first senteces from a page in content manager? would it work the same way?

And Rolf... I'm using the autocomplete search snippet from your website. It is working like a charm but as you know it is searching through titles and obviously it returns both article titles from the news module and page titles from the content manager.

So my problem is basically to show content text, from any possible module.

Author:  Rolf [ Thu Sep 06, 2018 4:22 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

Working example

At https://beeldbank.zeijen.nu I use the Uploads module for photo's.
The important part of the Search Result template is:
\$1:
    {foreach from=$results item=entry}
        {if $entry->module == 'Uploads'}
            <div class="grid_2 m_bottom_15">
                {Uploads action='detail' detailtemplate='search' upload_id=$entry->modulerecord}
            </div>
        {/if}
    {/foreach}

I created a detail template in the Uploads module with my needed content:
\$1:
<a href="{$entry->canonical}" title="#{$entry->id} - {$entry->summary|escape:'htmlall'|default:''}">
    {CGSmartImage src="{$entry->origfile_url}" class="uploads_summary_img" filter_croptofit="500,500" filter_watermark=true alt="#{$entry->id} - {$entry->summary|escape:'htmlall'|default:''}"}
</a>

As far as I understand your question, this is what you need. This is for Uploads, but it also works for News, CGBlog, LISE, etc.

This link displays the search content: https://beeldbank.zeijen.nu?zoekwoord=rolf
Five times a result, five times displaying an Uploads detail template

Author:  velden [ Sat Sep 08, 2018 2:09 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

I just checked and it seems IF the 'module' is a content page, neither '$entry->module' nor the '$entry->modulerecord' are provided in the results.

IMO it would make sense to implement this (and probably easy to implement).

Feature request submitted: http://dev.cmsmadesimple.org/feature_request/view/11886

Author:  Sendlingur [ Wed Dec 05, 2018 4:29 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

I've been trying to make this work so I can display few lines from the content in the search results. I found this old post with a great answer, but it is not working for cmsms 2.x. It seems to be exactly what I need.

I've been trying to rewrite the answer for cmsms 2.x but with out any luck. I believe I need more experience to be able to rewrite it so it works.

here is a UDT that have to be made (called 'set_alias')

\$1:
/** Get page alias from title
* @params string $params['title']
*/
$gCms = cmsms();
$cntnt = cmsms()->GetContentOperations();

foreach ($cntnt->GetAllContent() as $page) {
 if ($page->mName == $params['title']) {
   $return = ($page->mAlias);
   break;
 }
}
if(!empty($params['assign'])){
        $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}



Here is the code for the search template

\$1:
<h3>{$searchresultsfor} &quot;{$phrase}&quot;</h3>
{if $itemcount > 0}
<ul>
  {foreach from=$results item=entry}
  {set_alias title=$entry->title assign='get_alias'} {* call set_alias udt and assign as get_alias *}
  <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
   {$cgsimple->get_page_content($get_alias,'content_en','result_content')}{$result_content|truncate:'160'|strip_tags} {* cgsimplesmarty looks through our var get_alias and content and assigns var result_content, then we output found content with result_content and truncate it to desired number of character *}
</li>
  {/foreach}
</ul>

<p>{$timetaken}: {$timetook}</p>
{else}
  <p><strong>{$noresultsfound}</strong></p>
{/if}


Can anyone help me to make this work for CMSMS 2.x?

here is a link to the old post https://forum.cmsmadesimple.org/viewtopic.php?f=7&t=54145

the answer is from a user called uniqu3, maybe he is still active and sees this :)

Author:  DIGI3 [ Wed Dec 05, 2018 7:10 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

2.3 will allow for accessing the page content in search results
(I don't have an answer for your UDT request, but for reference it won't be needed once 2.3 is out)

Author:  velden [ Thu Dec 06, 2018 7:19 pm ]
Post subject:  Re: Adding {$entry->content} to search results.  Topic is solved

UDT:

\$1:
/** Get page alias from title
* @params string $params['title']
*/
$gCms = cmsms();
$cntnt = cmsms()->GetContentOperations();

$return = false;

$title = strip_tags($params['title']); // search might add span to title to highlite specific searched words

foreach ($cntnt->GetAllContent(false) as $page) {
 if (strip_tags($page->Name()) == $title) {
   $return = ($page->Alias());
   break;
 }
}

if(!empty($params['assign'])){
       // $smarty = cmsms()->GetSmarty();
        $smarty->assign(trim($params['assign']), $return);
}
else{
        return $return;
}


Sample search result template:
\$1:
<h3>{$searchresultsfor} &quot;{$phrase}&quot;</h3>
{if $itemcount > 0}
<ul>
  {foreach from=$results item=entry}
  <li>{$entry->title} - <a href="{$entry->url}">{$entry->urltxt}</a> ({$entry->weight}%)<br />
   {if empty($entry->module)}
     {set_alias title=$entry->title assign='get_alias'} {* call set_alias udt and assign as get_alias *}
     {page_attr key=content_en page=$get_alias assign=result_content}{$result_content|truncate:'160'|strip_tags} {* page_attr looks through our var get_alias and content and assigns var result_content, then we output found content with result_content and truncate it to desired number of character *}
  {/if}
</li>
  {/foreach}
</ul>

<p>{$timetaken}: {$timetook}</p>
{else}
  <p><strong>{$noresultsfound}</strong></p>
{/if}


Please test thoroughly before implementing in production environment

Further note that if in future versions the 'module' property of a search result entry is provided for 'content' too - as DIGI3 posted - that this template will fail. You then might need to check for {if $entry->module == 'content' } or something, in stead of {if empty($entry->module)}

Author:  Sendlingur [ Mon Dec 10, 2018 2:12 pm ]
Post subject:  Re: Adding {$entry->content} to search results.

Thank you all for your help, I'll test the solution from Velden.

Is there a release date for 2.3?

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/