How do I access fields in the view from the PHP code?
I need to run some PHP code against each row in the view using a specific field to check against. I've tried dsm($field), dsm($fields), dsm($view) and dsm($result) but all come up empty...
Also, does the PHP code run once for the view, or for each row?
Thanks!
Comments
Comment #1
Anonymous (not verified) commentedNever mind, was able to achieve what I wanted via Views PHP default argument...
Comment #2
awolfey commentedIs it possible to have access to the row data or other view data?
Comment #3
Anonymous (not verified) commentedBack again. I'd still like to know if it's possible to access information about the view from this filter.
In the README.txt it says:
I tried outputting $query, but it came up NULL (as with the other variables I mentioned above)...
Any ideas?
Comment #4
gnassar commentedNot exactly. The view itself is actually created after the PHP in this filter runs -- obviously, I suppose, since the set of nodes can't be made until all filters are processed, including this one. So at the time of the execution of your PHP code, the view has no rows.
As far as clobbering $query, a prior issue was caused by someone using that variable in their code. Changing the name solved the problem. So it's been demonstrated that it can't be used within the PHP, at the very least, without a really good idea of what $query is supposed to be within the portion of Views that is running the filters. I wanted to make sure others didn't make that (understandable) mistake, and that is why that entry is in the readme.
Comment #5
gnassar commentedComment #6
Anonymous (not verified) commentedFor anyone interested, I eventually got this working by using the following code in the filter:
Some things to note:
$this_view->set_item($view_display, 'filter', 'nid_php', NULL);removes the PHP fllter from the view before running it, without it you'll end up in an infinite loop.$this_view->destroy();destroys the view after it has run to prevent memory problems (I learnt this the hard way ;).Hope that helps.
Comment #7
gnassar commentedThat is probably about the only way to do that -- re-run the entire view, without the filter installed, inside the filter itself. Not exactly resource-light :) but if you need to do that, that's a pretty clever way to go about it.
Now you've got me thinking about what I could do with *displaying* another view inside a view with this filter... I imagine that *might* work, if whatever page template was calling the view directly, so basically you'd just be injecting the first view ahead of it on the page. Hmm...