9

I'm trying to wrap my head around contextual filters again! I used it a while back but I just got totally stumped again. Here's what I'm trying to do:

  1. Generate a cloned content node page with taxonomy terms inserted into this cloned page.

ex.: EXAMPLE.com/white-shoes/blue

When a user reaches the /white-shoes page it will show the original node page geared toward the white color. Now if the user lands on /white-shoes/blue it will show the same node page, but this time all of the body content will be rewritten replacing the white color with blue color.

In Views I would have to create a Page display with the path of: %/%

Now I would create a contextual filter to retrieve the nid. I add Content: nid as a contextual filter. I pick PROVIDE A DEFAULT VALUE and I put in this PHP code:

$x = drupal_lookup_path('source', arg(0));

$y = explode('/', $x);

return $y1;

That PHP code basically converts the /white-shoes part of the URL into a NID. But it doesnt work. I tested the code via drush ev via the terminal and I indeed get a returned result, so i know it works.

I do the same for the taxonomy term name and add a contextual filter for that aswell. This time I pick PROVIDE A DEFAULT VALUE and select RAW VALUE FROM URL and select the 2nd argument.

enter image description here

As you can see when I input a PREVIEW VALUE for the contextual filter: AAA/BBB/CCC . My default value gets ignored. It's supposed to be grabbing the 2nd argument, but it's getting the 1st only. Look at the SQL statement WHERE and you'll see it's getting AAA when it should be BBB.

enter image description here

So the question is what am I doing wrong or is there a better solution to what I want to accomplish?

2
  • This sounds stupid, but could you provide a bit more detail? I'm having a bit of a time understanding what you're trying, more on how you're trying to do it. Instead of using the Raw value or Provide default, use the Content ID from URL. That will provide the NID. Jul 2, 2013 at 4:17
  • hi kyle, there is no Content ID since your landing on the url: /white-shoes/blue .... the /blue url doesnt exist as its rendered by views. so there is no content ID to grab... it has to parse the 1st argument of the url then convert that url into a NID path. Jul 2, 2013 at 13:13

2 Answers 2

2

Here's a video and its related article that'll explain you how to use them, the example uses Pets (Dogs, Cats) which may be a simple enough example to help understand how to use Views Contextual Filters.

VIDEO: https://www.youtube.com/watch?v=lJFMTDuaxy8 (YouTube video short-url: lJFMTDuaxy8)

ARTICLE: http://www.metaltoad.com/blog/using-drupal-contextual-filters-views

5

Contextual Filter is used to filter ,output of the view by URL arguments, also u can pass the arguments manually in this format:

<?php print views_embed_view($name,$display_id,$arg1,$arg2,$arg3); ?>

where "$name" is the name of your view (ex. 'example_view') and '$display_id' is the display type of your view (ex. 'default') and "$arg1,$arg2,$arg3" are arguments just like in URL.

URL arguments has the following structure:

http://example.com/arg_1/arg_2/arg_3 (arg_2 has the path component 2 (in view setting page)).

When the filter value is not availabe:

it means drupal can't find any filter.

Provide default value:

when the filter value is not available you can use this option to to creat a filter for views output, this filter can be PHP code (if PHP filter module enabled) or it can get it's value from the URL arguments--> (Raw value from url). The Exception textbox allows you to cheat the value and display all results.

When the filter value is available

in this case you do a validation as your needs and some other options, when drupal can find the filter value.

2
  • given the views_embed_view() function. how would I load that up into my sub path of /white-shoes/blue ? keep in mind that /blue doesnt exists as its generated by the views. when a user reaches the /blue path, it should parse the 1st argument and fetch the NID of that alias then load up its content. the problem with contextual filters is that there's no way to provide a PHP value for WHEN THE FILTER VALUE IS AVAILABLE. Jul 2, 2013 at 13:18
  • @duckx, There is an excelent option to provide a PHP value when the filter is available - VALIDATION (type: PHP Code). With this option you can use view object and do everything you need. In your case /blue path generated by views would be used to fetch the node id of that alias as well.
    – Jack-PL
    Jul 2, 2013 at 22:26

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.