Add images to Drupal Views Search Page

Drupal Views is a powerful tool for all kind of mixed results but how to add images to Views' Page when url have "?". Blocks can't help, but PHP can.

I had a problem how to print big hero images to custom made search page (Drupal Views) only for certain search results. Drupal's blocks do not understand urls with ? so that option was out. The answer was very simple and my saviour was once again PHP. I added Global: PHP field to View's Header section.

I use Drupal's term reference fields as features for Products (Drupal Commerce) and Brand is one of them. First, I wanted to know if the search page result is "brand only". Those images should be visible only when there is only one brand name selected and nothing else.

if(isset($_GET['field_brand_tid'])) {
    if(count($_GET) == 2 AND count($_GET['field_brand_tid']) == 1){
        $i = $_GET['field_brand_tid'][0];
        $term = taxonomy_term_load($i);
        $term = $term->name;
        $term = strtolower($term);
        $term = preg_replace('/\s+/', '', $term);
        echo "<img src='/sites/all/themes/themename/brand/$term.jpg' alt=''/>";

var_dump($_GET) gives something like this:

array(2) { ["field_brand_tid"]=> array(1) { [0]=> string(1) "1" } ["q"]=> string(4) "search" }

First, there has to be at least one brand selected, if none found then this code is not executed. Then I want to know how many $_GET results there is because there can be only two; one for search (q) and one for the brand. After that we can check out the ID and the name of the brand and fix the name for the file name. strtolower makes the name lowecase and preg_replace removes all spaces. Now everything is ready for printing.

I really enjoyed this little challenge and big thanks for Joonas from Comspot for helping me out.