Views Field Views in D7 - Passing arguments using contextual filters bug fix.

Views Field Views in D7 - Passing arguments using contextual filters bug fix.

situation (hypothetically, excuse the periods):
view parent has a list of accounts and their balance:
account 1.......................$balance
account 2.......................$balance
etc ...

view child has details for the account (categories where $ is spent)
account 1.......................$amount.......................travel
account 1.......................$amount.......................strippers
account 1.......................$amount.......................food

account 2.......................$amount.......................travel
account 2.......................$amount.......................strippers
account 2.......................$amount.......................food

i want to pass the argument account name from parent to child so that my final view looks like this:
account 1.......................$balance
......................................$amount.......................travel
......................................$amount.......................strippers
......................................$amount.......................food

account 2.......................$balance
......................................$amount.......................travel
......................................$amount.......................strippers
......................................$amount.......................food

so i want to pass the account name from parent to child so that the child details fall under the correct parent and so on. hopefully this makes sense. any help would be greatly appreciated.

 

 

Solution:

First, build out your views so that, in theory, they are exactly what you want your entire view to look like. Once you are done go to your child view and add a contextual filter for the content field item that you would like it to be filtered by (or the item that the parent view will be passing to the child view, however you prefer to view it). Your preview should look the same after you have applied and saved the contextual filter. Leave the settings just like the pic below:

Next, go to your parent view and add a field and choose Global: View(View). Choose your child view from the drop down and this is where you will see the contextual filters blank. Below is a button to see replacement patterns. Go ahead and try to add in the correct field here. If it works, GREAT! you are done. If it only partially works, keep following me. For some reason, in my circumstance, it was not working for a certain Project Name. I have no idea why, as the child view has the data there and is filtering the rest of the data perfectly but whatever.

go to your child view and add a global php field. in the output code area add this: (you will delete this field later, for debug purposes)

The next step I have already written about in previous guide, and came in handy again. Basically, to summarize, you have to find the raw variable for the particular item you want passed from your parent view to the child view. Then you use a simple print command to print it and use THAT field as the contextual filter, NOT the suggested replacement patterns. So go to your child view and create a global php field and put this in the output code section:

print_r($data); ... make sure to wrap it in php tags. This will spit out a ton of information about what is going on in your view. Do a control F and find the field, in my case it was this:
$data->field_field_project_name_s['0']['raw']['value'];

Now, go to your parent view (I am assuming you have contextual fields set already) and add a global php field with this output code with what we just found out:

print_r($data->field_field_project_name_s['0']['raw']['value']); ... wrap it in php tags

Save it, drag the field to the top of the order (so it can be used in the views field view). Open up your views field view and instead of using the tokens they are suggesting, use the one that correlates to the global php field we just created. in my case:
[%php_2]

And boom, the accounts (project names) that weren't matching up at all are all now working perfectly. Obviously once you are finished you will want to exclude the fields from view to clean up your views.

about the author

David Leary

related posts