How to override search results sort order in Plone -


plone search functionality implemented in plone.app.search package; sort_on variable included in request used control sort order of results on search template.

by default, when variable has no value, plone uses relevance sort order.

what's easiest way of changing date (newest first)?

you'll need customize search view set new sorting options, , alter default sort when no sort has been set.

if still need able sort relevance, use non-empty value change in filter_query method:

from plone.app.search.browser import _, search, sortoption  class mycustomsearch(search):     def filter_query(self, query):         query = super(mycustomsearch, self).filter_query(query)          if 'sort_on' not in query:             # explicitly set sort; if no `sort_on` present, catalog sorts relevance             query['sort_on'] = 'effectivedate'             query['sort_order'] = 'reverse'         elif query['sort_on'] == 'relevance':             del query['sort_on']          return query      def sort_options(self):         """ sorting options search results view. """         return (             sortoption(self.request, _(u'date (newest first'),                 'effectivedate', reverse=true             ),             sortoption(self.request, _(u'relevance'), 'relevance'),             sortoption(self.request, _(u'alphabetically'), 'sortable_title'),         ) 

then register view site; if use theme layer that'd easiest:

<configure     xmlns="http://namespaces.zope.org/zope"     xmlns:browser="http://namespaces.zope.org/browser"     i18n_domain="plone">      <browser:page         name="search"         layer=".interfaces.iyourcustomthemelayer"         class=".yourmodule.mycustomsearch"         permission="zope2.view"         for="*"         template="search.pt"         />      <browser:page         name="updated_search"         layer=".interfaces.iyourcustomthemelayer"         class=".yourmodule.mycustomsearch"         permission="zope2.view"         for="products.cmfcore.interfaces.ifolderish"         template="updated_search.pt"     />  </configure> 

you'll need copy on search.pt , updated_search.pt templates plone.app.search package.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -