Change icon for search result display

Alan Isaacson asked on June 16, 2016 15:59

I would like too modify the search results displayed within my transformation so that they have a different icon based on there URL or NodeAliasPath. I have the same page type appear in mtuliple part sof the site so I would like to display as

Search results 1st page is in /home/content/dog/disease 2nd page is in /home/content/cat/disease Both pages would use the same page type and template

In the search results I would like to say if the page is in the /dog/ path then display icon A, if in path /cat/ then display icon B and if in neither display icon Z

Any help appreciated as still getting to terms with searches and the relevant results.

Recent Answers


Jan Hermann answered on June 16, 2016 17:18

Well, this is actually a good example for custom transformation method. You can implement one and as its parameter you send the NodeAliasPath of currently listed item. In the code you simply check what section it is and return the icon you need:

https://docs.kentico.com/display/K9/Adding+custom+methods+to+transformations

1 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on June 19, 2016 17:29

Hi Alan,

Also, you can write conditional expression in the transformation:

<%# Eval("NodeAliasPath").ToString().StartsWith("/home/content/dog", StringComparison.CurrentCultureIgnoreCase) ? "dog.jpg" : "cat.jpg" %>
0 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on June 20, 2016 12:01

I will investigate the custom methods (this normally proves harder as my Visual Basic skills are minimal still). I have tried the conditional expression in the transformation and it gives an error of [CMSAbstractTransformation.DataBind]: Object reference not set to an instance of an object.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on June 20, 2016 19:25

Hi Alan,

It's weird, looks like Eval method returns null. Could you check that you select NodeAliasPath column in your datasource? Or you can add only <%# Eval("NodeAliasPath") %> to check what this method returns.

0 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on June 21, 2016 10:59

Hi Anton, I have cleared out the entire transformation and used just the line as suggested and it displays no text at all in the results but shows there are 145 pages worth of results. I then changed the transformation to <%# SearchResultUrl() %><%# Eval("NodeAliasPath") %> and it the does show a list of results URLs as I would expect, /clinical-reference/canis/illustration/parsons-jack-russell-terrier /clinical-reference/canis/illustration/american-staffordshire-terrier Should that mean that rather usinhg the NodeAliasPAth I should be able to use your original code with SearchResultsURL instead?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.