Search code examples
drupaldrupal-7drupal-views

Set views contextual filter as request_path()


Theme

I have a content type with image and URL fields, I need to show image as banner on the path where URL field is matched with requested path using views.

I tried with

1-> adding "Alias" field as contextual filter in views.

2-> adding URL field

3-> I also tried with URL field with PHP Code in contextual filter:

if(drupal_is_front_page()) {
    return '<front>';
}else{
    return request_path();
}

3rd point works partially for only one path argument, like if current requested path is services/one and views contextual filter only takes first path component as you can see in attached image enter image description here However, I need to set contextual filter with whole path no matter how many components are requested.

How would I do that?


Solution

  • I have done this by embedding the views block in tlp and passing the URL field filter by code. See code below:

    $path = request_path();
    
    $query = 'SELECT field_url_url FROM {field_data_field_url}
              WHERE bundle = :bundle AND entity_type = :entity_type AND field_url_url =  :field_url_url';
    
    $path = db_query($query, array(
                                   ':bundle'=>'page_banner',
                                   ':entity_type'=>'node',
                                   ':field_url_url'=> $path
                     ))->fetchField();
    
    if (!empty($path)):
      print views_embed_view('page_banner','block', $path);
    endif;
    

    I hope this will help someone who needs to add contextual filter as request URL.