0

There are fields in content type called URL and Image.

Suppose, A node has "service" value of "URL" field and I'm trying to show views block where current path (service) and "URL" field path (service) are same. The "URL" value can also be "content/services".

I tried with

  • adding "Alias" field as contextual filter in views.
  • adding URL field

Updated:

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

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

It works 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.

Theme

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

How would I do that?

2 Answers 2

0

If you want to filter nodes in a Views list that have the same expression in their "Alias" field as the path where the Views Block is shown then you can do it by adding the "Alias" field as a contextual filter and configured like this:

When the filter value is NOT available
Provide default value
Raw value from URL
Path component 1

Adjust the path component to the place where your service appears in the path.

You can also adjust the validation and other options that are relevant to you, but that first bit is what is the most important to match the path to your field.


The field value content/services might match only on pages where the path starts with content/services. I haven't tested this, I expect the field expression will be treated as a one string, but the path will only take the first part before the / so in the end it won't match.

If you find that it doesn't work, then you might make it work with the help of Views Argument Substitutions and Views Contextual Filters OR.

You could also do it with the Context module and Context entity field but it's a fairly complex to configure.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.