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

Anonymous’s picture

Status: Active » Closed (fixed)

Never mind, was able to achieve what I wanted via Views PHP default argument...

awolfey’s picture

Is it possible to have access to the row data or other view data?

Anonymous’s picture

Title: How to access view fields from PHP filter » Access view data from PHP filter
Status: Closed (fixed) » Active

Back 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:

One should take caution in that the PHP code will be eval'd within the context of Drupal and Views. This is a useful thing for power users, but can cause trouble if you're not expecting it. For example, your code should not use variable names like $query unless you are actually intending to clobber the preexisting $query object.

I tried outputting $query, but it came up NULL (as with the other variables I mentioned above)...

Any ideas?

gnassar’s picture

Not 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.

gnassar’s picture

Status: Active » Closed (fixed)
Anonymous’s picture

For anyone interested, I eventually got this working by using the following code in the filter:

<?php
// Enter your view details here
$view_name = [view_name];
$view_display = [display_id];
$view_arguments = array([arguments]);

$this_view = views_get_view($view_name);
$this_view->set_item($view_display, 'filter', 'nid_php', NULL);
$this_view->set_display($view_display);
$this_view->set_arguments($view_arguments);
$this_view->pre_execute();
$this_view->execute();

$results = $this_view->result;
$this_view->destroy();

// Now do whatever you want with $results...
?>

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.

gnassar’s picture

That 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...