Modification of results items before sending them to the front-end scripts might be hard in SXA 1.7. You would need to:
- override
SearchController
and modify GetResults()
method - this method is building a query, getting items and preparing ResultsSet
which later on is sent to the front-end
- replace controller in DI container
It is possible but I do not recommend going that way.
Another option is to modify the results on the front-end. Search Renderings are using events to notify that e.g.: results have been loaded from the server. You can modify the list of listeners for results-loaded
event and put your handler at the first place. It can modify the list of results loaded form the server and modify them before notifying other renderings. Keep in mind that the HTML (which is present in JSON) will be already processed by Rendering Variants. And this is probably not what you want...
The best option would be to update to SXA 1.9 where you will find new custom pipeline called processSearchItems
. This pipeline is triggered after getting items but before processing them by Rendering Variants and sending to the front-end. This is the perfect place to so some modification to the list of results. You can even bind your results to additional models (basically custom object) and process this later on by Variants Model item.
Arguments of that pipeline contain two properties:
public List<Item> Items { get; set; }
public List<SearchItem> SearchItems { get; set; }
Where SearchItem
contains:
public Item Item { get; set; }
public object Model { get; set; }
The model property gives huge flexibility to transfer additional data to Rendering Variants. You can, later on, use this Model
property in those two Variant Items:
